Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-09-2021, 11:31 AM
rotunda rotunda is offline Trying to combine Word documents using Excel macro Windows 10 Trying to combine Word documents using Excel macro Office 2019
Novice
Trying to combine Word documents using Excel macro
 
Join Date: Jun 2021
Posts: 6
rotunda is on a distinguished road
Default Trying to combine Word documents using Excel macro

Hello, I am using the macro shown below to combine 3 Word documents. When I try to run the macro, I get the error "Run-time error '438': Object doesn't support this property or method" and the debug leads me to the line with Paste (highlighted in yellow in the screenshot). Do I need to include references in addition to the Word library that I've already added? Thanks in advance.



Screenshot 2021-06-09 143015.jpg
Reply With Quote
  #2  
Old 06-09-2021, 03:47 PM
Guessed's Avatar
Guessed Guessed is offline Trying to combine Word documents using Excel macro Windows 10 Trying to combine Word documents using Excel macro Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

We normally try to avoid the selection object and copy/paste when they aren't necessary.

Add Dim aRng as Range to the top of the macro

Delete from objDoc.Range.Copy to End With
In that same location, add this code
Code:
Set aRng = objNewDoc.Range
aRng.Collapse Direction:=wdCollapseEnd
aRng.FormattedText = objDoc.Range.FormattedText
As an aside, you can merge multiple files like this without code.
1. Create a new document
2. Go to Insert > Object > Text from File
3. Pick multiple files (Select and Shift+Select)
4. Click OK
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 06-09-2021, 03:58 PM
macropod's Avatar
macropod macropod is offline Trying to combine Word documents using Excel macro Windows 10 Trying to combine Word documents using Excel macro Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

For comprehensive document-combining code, see the Combine Multiple Word Documents 'Sticky' thread: https://www.msofficeforums.com/word-...documents.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 06-09-2021, 08:51 PM
gmayor's Avatar
gmayor gmayor is offline Trying to combine Word documents using Excel macro Windows 10 Trying to combine Word documents using Excel macro Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If you are working from Excel, you are going to have to tell Excel to open or create a Word application or it will think it is working in Excel. You don't need to set a reference to Word. You can use late binding to Word instead thus:


Code:
Option Explicit

Sub MergeFilesInAFolderlntoOneDoc()
Dim dlgFile As FileDialog
Dim wdApp As Object, oRng As Object
Dim objDoc As Object, objNewDoc As Object
Dim StrFolder As String, strFile As String
    Set dlgFile = Application.FileDialog(msoFileDialogFolderPicker)

    With dlgFile
        If .Show = -1 Then
            StrFolder = dlgFile.SelectedItems.Item(1) & Chr(92)
        Else
            MsgBox ("No folder is selected!")
            Exit Sub
        End If
    End With

    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If Err Then
        Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0

    strFile = Dir(StrFolder & "*.docx", vbNormal)
    With wdApp
        .Visible = True
        Set objNewDoc = .Documents.Add
        While strFile <> ""
            Set objDoc = .Documents.Open(FileName:=StrFolder & strFile)
            With objNewDoc
                Set oRng = .Range
                If Len(oRng) > 1 Then
                    oRng.Collapse 0
                    oRng.InsertBreak 7
                End If
                oRng.Collapse 0
                oRng.FormattedText = objDoc.Range.FormattedText
                objDoc.Close 0
            End With
            DoEvents
            strFile = Dir()
        Wend
        objNewDoc.Activate
    End With
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 06-09-2021, 10:30 PM
macropod's Avatar
macropod macropod is offline Trying to combine Word documents using Excel macro Windows 10 Trying to combine Word documents using Excel macro Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by gmayor View Post
You don't need to set a reference to Word. You can use late binding to Word instead
True, but according to MS, early-bound code runs twice as fast as late-bound code.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 06-10-2021, 12:29 AM
gmayor's Avatar
gmayor gmayor is offline Trying to combine Word documents using Excel macro Windows 10 Trying to combine Word documents using Excel macro Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

That may be true, but the speed bottleneck, if any, will be that involved with opening and closing documents.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 06-14-2021, 10:42 AM
rotunda rotunda is offline Trying to combine Word documents using Excel macro Windows 10 Trying to combine Word documents using Excel macro Office 2019
Novice
Trying to combine Word documents using Excel macro
 
Join Date: Jun 2021
Posts: 6
rotunda is on a distinguished road
Default

Thanks everyone, this was very helpful in solving my problem!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to combine Word 2016 documents using VBA? (Mac OS) quanfirem Word VBA 1 10-03-2019 07:21 AM
Trying to combine Word documents using Excel macro how to combine word Documents properly SebastianWien Word 2 09-04-2019 12:10 AM
Trying to combine Word documents using Excel macro Combine Multiple Word Documents macropod Word VBA 0 09-04-2019 12:09 AM
Trying to combine Word documents using Excel macro combine 2 different word documents salimnore Word 6 05-29-2018 09:43 AM
How can a combine three seperate word documents into one? hellno187 Word 0 09-20-2010 02:46 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:00 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft