Position cursor at start/end of Word document

后端 未结 9 769
误落风尘
误落风尘 2020-12-11 16:39

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()
    //d         


        
相关标签:
9条回答
  • 2020-12-11 16:51

    @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)
    
    0 讨论(0)
  • 2020-12-11 17:00

    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
    
    0 讨论(0)
  • 2020-12-11 17:01

    Try this :

    int lNumberOfPages = 
      _WordDoc.ComputeStatistics(Word.WdStatistic.wdStatisticPages, false);
    
    WordApp.Selection.GoTo(Word.WdGoToItem.wdGoToPage,WordApp.WdGoToDirection.wdGoToLast, lNumberOfPages);
    
    0 讨论(0)
  • 2020-12-11 17:02

    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.

    0 讨论(0)
  • 2020-12-11 17:06

    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.

    0 讨论(0)
  • 2020-12-11 17:06

    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

    0 讨论(0)
提交回复
热议问题