Merge 2 documents into 1

J

Joe Torre

Hi all,

I have implemented a VBA to (With some help from you) a
few years ago to produce a word document using an exported
data file from another system.

I has worked very well over the last 3 years.

I do have a question/request on a further enhancement.

I have a master merge document that contains the embedded
merge fields and VBA script.

How would I go about modifying the VBA script in Document
1 to call ANOTHER document to merge the same information
used in Document 1 into Document 2 and have Document 2
placed at the end of Document 1.

Both documents have a different layout and both documents
have headers with embedded merge fields.

I hope that I have described this to you accurately and
that some one can help me.

Thanks,

Joe T.
 
R

Richard

Sounds like a pretty complicated project...
Maybe this will help.
Do a search on your computer for a help file called...
VBAWD10.CHM
This help file has helped me tremendously with
programming.
Its sorta like Cliff's Notes :)
Open the help file and do a search for Merge Method
When it lists the results... look for Merge Method in the
list.
Click it and check out what it has to say.

Hope this gets you pointed in the right direction.
 
A

Astrid

Hi Joe,
I have a master merge document that contains the embedded
merge fields and VBA script.
How would I go about modifying the VBA script in Document
1 to call ANOTHER document to merge the same information
used in Document 1 into Document 2 and have Document 2
placed at the end of Document 1.
Both documents have a different layout and both documents
have headers with embedded merge fields.

Assuming that both document use the same names for the merged fields and the second document is restored to a normal Word document (without an attached datasource), something like this:

--------------------------------------------------------------------
Sub CopyMergedData()
Dim oDoc1 As Document
Dim oDoc2 As Document
Dim oMergedDoc As Document
Dim oNewDoc As Document
Dim sDataSource As String
Dim sMaster2FileName
Dim oRange As Range
Dim sTempDir As String
Dim sFileName As String

'Filename for second document here
sMaster2FileName = "Path and FileName.doc"
'Get Tempdir
sTempDir = Environ("Temp")
If Right(sTempDir, 1) <> Application.PathSeparator Then
sTempDir = sTempDir & Application.PathSeparator
End If

Set oDoc1 = ActiveDocument
Set oRange = oDoc1.Content
oRange.Collapse direction:=wdCollapseEnd
oRange.InsertBreak Type:=wdSectionBreakContinuous
oRange.Collapse direction:=wdCollapseEnd

sDataSource = oDoc1.MailMerge.DataSource.Name
Set oDoc2 = Documents.Open(FileName:=sMaster2FileName)
With oDoc2
.MailMerge.OpenDataSource Name:=sDataSource
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute
End With

'Current document is merged result
Set oMergedDoc = ActiveDocument
'Save merged result to tempdir
sFileName = sTempDir & oMergedDoc.Name
oMergedDoc.SaveAs FileName:=sFileName
'Close merged result and second document
oMergedDoc.Close savechanges:=wdDoNotSaveChanges
oDoc2.Close savechanges:=wdDoNotSaveChanges

'Insert merged result at end of range
oRange.InsertFile FileName:=sFileName

Set oRange = Nothing
Set oMergedDoc = Nothing
Set oDoc2 = Nothing
Set oDoc1 = Nothing

End Sub
--------------------------------------------------------------------
Things can complicate if your section setup is very different for both documents. If you ran into trouble there, or this the above does not what you want, repost and we will try to help you out.
Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top