Search code examples
vb.netms-wordcopy-paste

How to set curser at the end of Word Document from VB.Net?


I am doing Copy/Paste for tables from word document to another Word document via VB.Net, but it either keeps two line in between or merge the table.

I am utilizing VB.Net in automating Word document, I am copying one formatted table from a word document, and then paste it into a different word document.

the problem here is that I have to put a "separater" between the newly pasted table and the one pasted earlier, otherwise word will merge the two (and will keep merging every single newly pasted table).

I tried to put this code before Pasting

oWord.Selection.MoveDown(Word.WdUnits.wdLine, 0)
oWord.Selection.InsertBreak(Word.WdBreakType.wdLineBreak)
oWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault)

it worked fine but it puts two line instead of one.

appreciate if anyone give me a way to keep Pasting (or even adding new paragragh) remain always at the end of the document (with only one line-width separation)


Solution

  • I got a very acceptable solution

    instead of using

    oWord.Selection.MoveDown(Word.WdUnits.wdLine, 0)
    oWord.Selection.InsertBreak(Word.WdBreakType.wdLineBreak)
    oWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault)
    

    to insert a break, I used the following code, and it worked perfectly

    With oWord.Selection
        .Collapse(Direction:=Word.WdCollapseDirection.wdCollapseStart)
        .InsertParagraph()
        .Collapse(Direction:=Word.WdCollapseDirection.wdCollapseEnd)
    End With
    

    hope this be useful for anyone facing same problem