[ACCEPTED]-Position cursor at start/end of Word document-mergefield
@Alexander Kojevnikov: Thanks for your help 4 because you put me on the right track. However 3 I found I had to apply the .GoTo to the 2 Word Selection object, not the Document. As 1 in:
Dim what As Object = Word.WdGoToItem.wdGoToLine Dim which As Object = Word.WdGoToDirection.wdGoToLast //below line had no effect //d.GoTo(what, which, Nothing, Nothing) w.Selection.GoTo(what, which, Nothing, Nothing)
This is how it looks in C#:
object missing = Missing.Value; object what = Word.WdGoToItem.wdGoToLine; object which = Word.WdGoToDirection.wdGoToLast; doc.GoTo(ref what, ref which, ref missing, ref missing);
I guess it will 2 be even easier in VB.Net as it supports 1 optional parameters.
I'm not sure I'm using the same environment 2 as you, but to go to the start or end of the document 1 here's what works for me:
Private Sub moveCursorToStartOfDocument() w.Selection.HomeKey(WdUnits.wdStory, Nothing) End Sub Private Sub moveCursorToEndOfDocument() w.Selection.EndKey(WdUnits.wdStory, Nothing) End Sub
I use unit Word_TLB in Delphi with Appliction 3 object- Word.Application
generally end of word document is:
Selection.EndKey( WdUnits.wdStory, WdMovementType.wdMove)
When 2 I use
Selection.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToLast, Nothing, Nothing); Selection.InsertFile('documnet.docx');
new content was insert before last 1 line.
The easiest way to figure out an outline 6 for the actual code is to record a macro 5 in Word for that specific action. Then you 4 can modify the generated code to suit different 3 syntax(s) of VB, VB.NET, C# etc.
The code 2 snippet below demonstrates the usage for 1 a VB.NET application:
Imports wordNmSpace = Microsoft.Office.Interop.Word ' Create an object for the application instance objWord = CreateObject("Word.Application") ' Create a reference of the selection object within Word objSelection = objWord.Selection ' Now comes the part where you move selection position to the end of document objSelection.endof(wordNmSpace.WdUnits.wdStory, wordNmSpace.WdMovementType.wdMove)
Hope this helps.
To change cursor position at the end of 2 the current document in a C# Word Add-In 1 VSTO :
this.Application.ActiveDocument.Range( this.Application.ActiveDocument.Content.End-1, this.Application.ActiveDocument.Content.End-1).Select();
See How to: Programmatically Define and Select Ranges in Documents
Try this :
int lNumberOfPages = _WordDoc.ComputeStatistics(Word.WdStatistic.wdStatisticPages, false); WordApp.Selection.GoTo(Word.WdGoToItem.wdGoToPage,WordApp.WdGoToDirection.wdGoToLast, lNumberOfPages);
You can use the predefined bookmark:
Other 1 predefined bookmarks:
for net office:
mydoc.Range(GlobalClass.mydoc.Content.End-1 , mydoc.Content.End - 1).Select();
More Related questions