Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Merge Multiple Word documents in to a new word document

Status
Not open for further replies.

JasonEnsor

Programmer
Sep 14, 2010
193
GB
Hiya,

Wonder if someone could offer a little help. I have the code below that i am running from a userform in Word 2019. it kinda does what i want in the fact that it puts all the documents in to a final document....however it is putting them in to the document that has the userform, however i am wanting it to put them in the new document that i have created. I am probably missing something silly but i can't seem to get the selection to select the new document and then insert the files. Any ideas?

Code:
Private Sub btnCreateFinalReport_Click()
    Dim MyName
    Dim directory As String
    directory = ThisDocument.path & "\Reports\" & Me.uid & "\"
    Application.ScreenUpdating = False

    MyName = Dir(directory & "*.docx")
    
    Dim doc As Word.Application
    Set doc = New Word.Application
    
    Dim wrdDoc As Document
    doc.Documents.Add
    doc.Activate
    Set wrdDoc = doc.Documents(1)
    
    Do While MyName <> ""
        Selection.InsertFile FileName:=directory & MyName
        Selection.InsertBreak Type:=WdBreakType.wdPageBreak
        MyName = Dir
    Loop
    wrdDoc.SaveAs2 ThisDocument.path & "\Reports\" & Me.uid & "\Final.docx"
    wrdDoc.Close
Application.ScreenUpdating = True
End Sub

Regards

J.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top