I am trying to insert one file at the end of another but the first file's formatting gets messed up. It's lines start bleeding into the later pages.

Dim oWord As Word.Application
        Dim oDoc As Word.Document
        Dim oDoc2 As Word.Document
        Dim oDoc3 As Word.Document
        Dim oSelection As Word.Selection
        oWord = CreateObject("Word.Application")
        oWord.Visible = True
        oDoc = oWord.Documents.Open("file1.doc")
        oSelection = oWord.Selection
        oSelection.EndKey()
        oSelection.InsertFile("file2.doc")

Edited 5 Years Ago by Sophiron: n/a

You use plain oSelection.EndKey() which moves to the end of the line. EndKey's first parameter is the units where to move, in your case to the end of the document:

Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oDoc2 As Word.Document
Dim oDoc3 As Word.Document
Dim oSelection As Word.Selection
'oWord = CreateObject("Word.Application")
oWord = CType(CreateObject("Word.Application"), Word.Application) ' Cast the object to proper type
oWord.Visible = True
oDoc = oWord.Documents.Open("file1.doc")
oSelection = oWord.Selection
'oSelection.EndKey()
oSelection.EndKey(Word.WdUnits.wdStory) ' Move to doc's end
oSelection.InsertFile("file2.doc")

You can find the units in here. I also added cast to CreateObject() which returns an object but you use a typed Word.Application.

HTH

This article has been dead for over six months. Start a new discussion instead.