0

I am opening a word document from my VB windows forms application then adding text from several text boxes. I need this text to go to very beginning of the document every time rather than at the very end. Have searched online for hours and cannot seem to find a solution to this! I have managed to find WdGoToDirection.wdGoToFirst but don't know what to do with it. Any guidance would be a help.

        Dim oWordApp As New Word.Application
    oWordApp.Visible = True
    Dim oDoc As Word.Document = oWordApp.Documents.Open("C:\Users\christopherm\Desktop\TestBoard.htm")
    oDoc = oWordApp.ActiveDocument

    Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
    Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
    Dim oBeginning As Object = WdGoToDirection.wdGoToFirst

    oPara1 = oDoc.Content.Paragraphs.Add
    oPara1.Range.Text = "Heading 1234" & vbCrLf
    oPara1.Format.SpaceAfter = 0
    oPara1.Range.Font.Bold = True
    oPara1.Range.Font.Size = 12
    oPara1.Range.Font.Name = "Calibri"
    oPara1.Range.InsertParagraphAfter()
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316

1 Answers1

0

You need to get a Range object that points to the beginning of the document. You can get that by getting the entire range of the document and then going to the beginning of that range:

Dim rng as Range = ActiveDocument.Range
rng.Collapse ' collapses the range to the beginning

rng.Text = "Heading 1234" & vbCrLf

Dim para1 as Paragraph = rng.Paragraphs(1)
para1.Format.SpaceAfter = 0
' continue here ...

If you instead want to use the Document.Paragraphs.Add method to insert a new paragraph at the beginning of the document, you need to pass a range object pointing to that location:

Dim rng As Range = ActiveDocument.Range
rng.Collapse ' range object at the beginning of the document

' pass the range where the paragraph shall be inserted
Dim para1 as Paragraph = ActiveDocument.Paragraphs.Add(rng)
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316