다음을 통해 공유


Merge Word 2010 Documents by Using Access 2010 VB

A friend of me came to me with a problem -- he couldn't merge word docs. His old version of access could do it but the new version often got errors with it like :  The Remote Server Machine Does Not Exist Or Is Unavailable etc.. or others. Well in the end i concluded it was an Access bug, so i wrote something else.
I thought, well access inst made to merge word docs (and it has a slightly different VBA (that's why there is no macro recorder in Access 2010).

I created a document called merge.doc in which i created a macro that combines other word documents.
(notice i could place more macro's in it, for example to print the final documents, merge.doc is a place holder for macro's.
Also i advice to keep the merge.doc open until all other docs are joined and then close it.
Because that would speed up if you have to add a lot of documents
well here is the macro :

  Sub MergeDocs(file1 As String, file2 As  String, savename As String)


Dim rng As Range
Dim MainDoc As Document
Dim strFile As String


Set MainDoc = Documents.Add


Set 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 

Set rng = MainDoc.Range
rng.Collapse wdCollapseEnd 
rng.InsertFile file2 



MainDoc.SaveAs2 (savename)
MainDoc.Close


End Sub

**
**
> my friend wanted to add each doc on a new page; if you dont want that remove the red line above

Now from Access 2010 i made this macro or rather they call it module there
to add more documents repeat it but  start with final.doc and add more docs to it.

Sub  Creat_manual_Click()    ' its triggered by a button on a form...
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oRange As Word.Range
Set 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)
'

Set 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



Set oRange = Nothing
oDoc.Close
Set oDoc = Nothing
oWord.QuitSet oWord = Nothing
End sub

Notice I'm not an Access expert, please don't ask me questions about  that, my friend is good at it, and he is not at technet, i made this for him.

This code is a working concept, you should change it if  you want to print other docs, based on access table records. My sample assumes existing docs called test1.doc and test2.doc. The final doc will be created and overwritten if it already does exist.

If you like to use docx documents, it's not a problem for Word, as far as i know.

Have fun with it, if this helped you it would be nice to comment with a thanks

This Wiki article was created by Peter Boos; some think I'm an IT guy till you see my real art here www.peterboos.tk !  :)


See Also