10

We are manipulating our Word 2007 documents from .Net using Word Interop. Mostly doing stuff with fields as in:

For Each f In d.Fields
    f.Select()
    //do stuff with fields here            
Next

This leaves the last field in the document selected.

So, for the sake of neatness we would like to position the cursor at the end of the document (or even the start would be OK).

Googling for the answer doesn't throw up much ... the nearest I can get seems to be suggesting we need to involve ourselves with ranges or bookmarks. There's a GoTo method for the Document object but none of the WdGoToItem options it offers are useful.

Isn't there a simple way to just send the cursor to the end (or start) of document?

Edit

Part of my problem was I didn't like leaving the last field selected. Have now realised that I can do

f.Unlink

to remove the mergefield and just leave the field text there as plain text. Which is neater, whether or not we also reposition the cursor

ivcubr
  • 1,988
  • 9
  • 20
  • 28
hawbsl
  • 15,313
  • 25
  • 73
  • 114

9 Answers9

13

@Alexander Kojevnikov: Thanks for your help because you put me on the right track. However I found I had to apply the .GoTo to the Word Selection object, not the Document. As 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)
hawbsl
  • 15,313
  • 25
  • 73
  • 114
  • 1
    On my VisualStudio 2013 implementation this places the cursor at the **start** of the last line, not quite the end of the document. – Fuhrmanator Feb 21 '14 at 03:33
  • @Fuhrmanator VS 2013 or Office 2013? I am seeing some different behavior moving from Office 2010 (v14) to Office 2013 (v15). Both of them using the VS 2010 IDE, however – Michael Paulukonis Jun 24 '14 at 15:20
  • @MichaelPaulukonis Definitely VS2013. See my [answer](http://stackoverflow.com/a/21924860/1168342) for a solution that worked for me. – Fuhrmanator Jun 24 '14 at 15:31
  • VS2013 is an IDE. How on earth does that have any impact? What .NET runtime are you using? What version of VSTO? What version of Office? – Michael Paulukonis Jun 24 '14 at 18:36
8

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 be even easier in VB.Net as it supports optional parameters.

Alexander Kojevnikov
  • 17,580
  • 5
  • 49
  • 46
3

I'm not sure I'm using the same environment as you, but to go to the start or end of the document 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
Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111
2

I use unit Word_TLB in Delphi with Appliction object- Word.Application

as following:

aWordDoc.Application.Selection.EndKey(wdStory,wdMove);

generally end of word document is:

Selection.EndKey( WdUnits.wdStory, WdMovementType.wdMove)

When I use

Selection.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToLast, Nothing, Nothing);
Selection.InsertFile('documnet.docx');

new content was insert before last line.

1

The easiest way to figure out an outline for the actual code is to record a macro in Word for that specific action. Then you can modify the generated code to suit different syntax(s) of VB, VB.NET, C# etc.

The code snippet below demonstrates the usage for 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.

Jonnus
  • 2,988
  • 2
  • 24
  • 33
0

To change cursor position at the end of the current document in a C# Word Add-In 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

hayj
  • 1,159
  • 13
  • 21
0

Try this :

int lNumberOfPages = 
  _WordDoc.ComputeStatistics(Word.WdStatistic.wdStatisticPages, false);

WordApp.Selection.GoTo(Word.WdGoToItem.wdGoToPage,WordApp.WdGoToDirection.wdGoToLast, lNumberOfPages);
PM.
  • 1,735
  • 1
  • 30
  • 38
0

You can use the predefined bookmark:

EndOfDoc oDoc.Bookmarks.Item("\endofdoc").Range

Other predefined bookmarks:

ActiveDocument.Bookmarks("\Para").Copy "currpara"

https://msdn.microsoft.com/en-us/VBA/Word-VBA/articles/predefined-bookmarks

ivcubr
  • 1,988
  • 9
  • 20
  • 28
0

for net office:

mydoc.Range(GlobalClass.mydoc.Content.End-1 , mydoc.Content.End - 1).Select();
Ehsan.B
  • 1
  • 1