1

I'm trying to paste/consolidate 6 documents ( in a folder) into a new documents, containing all those documents. The VBA code is supposed to run from an Excel Template where all the documents are created and are then supposed to be merged together via a macros.

However, I get the

Run time Error 438: object doesn't support this property or method

every time I try to run the InsertFile line. I guess the problem lies in the transition from Excel to Word VBA(?)

Any ideas or thoughts?

  Sub MergeALL()
       Dim objWord
       Dim objDoc
       Set objWord = CreateObject("Word.Application")
       Set objDoc = objWord.Documents.Add
       objWord.Visible = True

'opens a new word document
Documents.Add
Dir "\\rz_sixt\user\Home\Pictures" 'change to OutputFilePath ?
MyName = Dir("*.docx")
While MyName <> ""
 With Selection
 .InsertFile Filename:=MyName, ConfirmConversions:=False, Link:=False, Attachment:=False
 .InsertParagraphAfter
 .InsertBreak Type:=wdSectionBreakNextPage
 .Collapse Direction:=wdCollapseEnd
 End With
 MyName = Dir()
Wend
End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
Annabelle
  • 13
  • 3
  • `Selection` is completely different in Word and Excel, so try `objWord.Selection` instead. – Asger Mar 18 '19 at 10:27
  • Hi Asger, Thanks for your answer. I changed it to objWord.Selection and now I get the error: Document Path or Name is not valid. – Annabelle Mar 18 '19 at 10:43

1 Answers1

0

When you loop through all found documents by Dir,
you have to add the file path again, when you InsertFile each file:

Sub MergeALL()
    Dim objWord As Object
    Dim objDoc As Object
    Dim myPath As String, myFile As String

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True

    Set objDoc = objWord.Documents.Add
    myPath = "\\rz_sixt\user\Home\Pictures\"
    myFile = Dir(myPath & "*.docx", vbNormal + vbReadOnly + vbHidden)
    While myFile <> ""
        With objDoc.Bookmarks("\StartOfDoc").Range
            .InsertFile Filename:=myPath & myFile, _
                ConfirmConversions:=False, Link:=False, Attachment:=False
            .InsertBreak Type:=2    ' wdSectionBreakNextPage = 2
        End With
        myFile = Dir()
    Wend
    objDoc.Characters(1).Delete
End Sub

If you add a reference to "Microsoft Word x.x Object Library" for early binding, you can use ENUM values like wdCollapseEnd, otherwise use their corresponding values.

Asger
  • 3,822
  • 3
  • 12
  • 37
  • Thanks so much, your code works! However, it now inserts the documents in reversed order: first it inserts 5.docx, then 4.docx ,etc. Do you know how I can reverse the order? And second, I now have an empty page at the end of my document which I would like to avoid (due to page break). Is it possible to avoid this empty page if all the documents have been inserted? – Annabelle Mar 18 '19 at 15:02
  • I edited my answer: Now it adds a pagebreak and the found document always at the beginning. At the end it deletes the first character (which is an unnecessary page break). – Asger Mar 18 '19 at 15:42
  • Thank you. The only thing bothering me is that if the documents are saved under a different name (but in the same folder), the code doesn't recognize them anymore. Is it possible to look for all the docx. documents, independant of their name? – Annabelle Mar 18 '19 at 16:14
  • The [Dir-Function](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dir-function) should find them all, but can also look for readonly or hidden files, so I added an example to find those also. If they are still not found, your file share is somewhat cached. – Asger Mar 18 '19 at 18:46
  • As `Dir` can not guarantee any sorting order, you may sort its output into a list before inserting the files, please see here: https://stackoverflow.com/q/4282940/10908769 – Asger Mar 28 '19 at 13:21