troubleshooting Question

How to combine two documents using word (2000) VBA

Avatar of Natchiket
NatchiketFlag for United Kingdom of Great Britain and Northern Ireland asked on
Programming Languages-Other
14 Comments1 Solution5472 ViewsLast Modified:
Hi I'm an Access VBA programmer trying to combine two word documents.
Trouble is I'm not familliar enough with use of the word object model to know how to do this.

Here's my attempt below

Public Function UniteDocs(strDoc1 As String, strDoc2 As String, strResult As String) As Boolean
'Unites two documents as one
'Doc2 is placed at the end of Doc1
'The new document is saved as strResult

Dim fso As New FileSystemObject
Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim wRng As Word.Range

On Error GoTo proc_err

If fso.FileExists(strResult) Then
    fso.DeleteFile (strResult)
End If

SetStatus "Combining documents...."

Set wApp = New Word.Application
Set wDoc = wApp.Documents.Add

'Add the first document
Set wRng = wDoc.Range(0, 0)
wRng.InsertFile FileName:=strDoc1

'Put a page break at the end of the first document
Set wRng = wDoc.Range(wDoc.Characters.Count, wDoc.Characters.Count)
wRng.InsertBreak wdPageBreak

'Add the second document
Set wRng = wDoc.Range(wDoc.Characters.Count, wDoc.Characters.Count)
wRng.InsertFile FileName:=strDoc2

'Save the new file
wDoc.SaveAs FileName:=strResult
wDoc.Close
Set wApp = Nothing

ClearStatus

UniteDocs = True

proc_exit:
Exit Function

proc_err:
Select Case ErrHand()
Case ErrAbort
    UniteDocs = False
    Resume proc_exit
Case ErrRetry
    Resume
Case ErrIgnore
    Resume Next
End Select

End Function

OK so the problem is the the first document gets added, fine and dandy, but the second document ends up inside a table in the first document, so my problem is I don't know how to tell word to properly get to the end of the first document, before appening a page break and then the second document

All help greatfully received.
ASKER CERTIFIED SOLUTION
jrsteele

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 14 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 14 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros