Sub
MergeDocs(file1
As
String
, file2
, savename
)
Dim
rng
Range
MainDoc
Document
strFile
Set
MainDoc = Documents.Add
rng = MainDoc.Range
rng.Collapse wdCollapseEnd
Selection.InsertNewPag '
REMOVE THIS LINE IF YOU DONT EACH ADDED DOC TO START AT A NEW PAGE
rng.InsertFile file1
rng.InsertFile file2
MainDoc.SaveAs2 (savename)
MainDoc.Close
End
Sub Creat_manual_Click() ' its triggered by a button on a form... Dim
Creat_manual_Click()
' its triggered by a button on a form...
oWord
Word.Application
oDoc
Word.Document
oRange
Word.Range
oWord = CreateObject(
"Word.Application"
oWord.Visible =
False
'hide word ' >> notice if you get into in usage errors >>> 'through taskmanager kill all winword processes (normally that shouldnt be needed) '
oDoc = oWord.Documents.Open(
"F:\merge.doc"
) 'we open it to use its macro.
'If you would like to do some looping (while wend / for next..etc) 'To add multiple docs, then build it from here build the loop '(for each ... / loop while ....) 'this is the magic to do merge of documents we are using the word macro here .
oWord.Application.Run
"mergeDocs"
,
"F:\1.doc"
"F:\2.doc"
"F:\final.doc"
'here should be the end of your loop (...wend /...next) 'BTW for loops to keep adding to the document use it like 'oWord.Application.Run "mergeDocs", "F:\final.doc", "F:\3.doc", "F:\final.doc" 'it would add doc3 to the previous final.doc and then save it as a new final doc 'the word macro saves between every doc you add. its a safety thing
"F:\3.doc"
oRange =
Nothing
oDoc.Close
oDoc =
oWord.Quit
oWord =
End sub
PGT2008 edited Revision 4. Comment: 30-10 updated with code look of the macro's
Ed Price - MSFT edited Original. Comment: Great article. Added tags and updated the title to title casing.