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.
1) Create destination document
2) Select All, Copy and Paste from the first needed document into the destination
3) Do same with the second document
You're done. No need for FileSystemObject.
Cheers,
Andrew