How do I merge several documents in one package and retain setting

S

SlavetoWasabi

I'm trying to do a mail merge where I assemble a package from several
different documents, which can vary depending on the scenario, then perform a
mail merge. Each of these documents may have different margins, headers,
header margins, etc. I currently have a blank "main" document that I insert
each file into, then perform the mail merge. Each document, before being
included in the main document, has a "Section Break - Next Page" at the
bottom. However, the settings reflected in each individual document are not
being retained in the merged product. Is there a way to do what I'm trying
to do?
 
D

Doug Robbins - Word MVP

Are the settings of the individual documents retained in the mail merge main
document? How are you inserting the documents into the blank "main"
document - copy and paste or using an IncludeText field?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
S

SlavetoWasabi

Doug -

I'm using the InsertFile command to insert the documents:

WordApp.Selection.InsertFile FileName:=lstDocuments.List(n),
ConfirmConversions:=True

Settings that seem to revert back to the main "blank" document settings
include margins and the spacing between the header and footer and the edge of
the paper.

Thanks,
Chris
 
D

Doug Robbins - Word MVP

Best if you show us the complete code that you are using.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
S

SlavetoWasabi

Doug -

Here's a code snippet. Some of this isn't relevant to the issue, but you
should have what you need in here.

Thanks again for your help,
Chris
************* Start of code snippet *************

'Start an Instance of Word, or grab an already-existing one
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err Then
Err.Clear
Set WordApp = GetObject("", "Word.Application")
End If

'Supress merge warning message about data, otherwise document not opened as
merge mail document
WordApp.DisplayAlerts = wdAlertsAll

'Hide Word until merging is complete,
WordApp.Visible = False
WordApp.WindowState = wdWindowStateMinimize

'Open master (blank) document
WordApp.Documents.Open FileName:=MainDocument, ReadOnly:=True

'Determine the name used by Word for the Main Document
'so that it can be closed after the Merge is completed
MainDocument = StrRev(MainDocument)
n = InStr(MainDocument, "\")
MainDocument = StrRev(MainDocument)
If n > 0 Then
MainDocument = Right(MainDocument, n - 1)
End If

n = InStr(MainDocument, ".")
If n > 0 Then
MainDocument = Left(MainDocument, n - 1)
End If

'Insert documents to be included in package into master document
For n = 0 To lstDocuments.ListCount - 1
If lstDocuments.Selected(n) = True Then
WordApp.Selection.InsertFile FileName:=lstDocuments.List(n),
ConfirmConversions:=True
If Err Then
MsgBox "Unable to locate document: " & lstDocuments.List(n),
vbInformation
Err.Clear
End If
If n < lstDocuments.SelCount - 1 Then
'Now including section break at the end of each document per message board
post
'in attempt to retain individual document formatting
' WordApp.Selection.InsertBreak wdSectionBreakNextPage
End If
End If
Next

'Indicate that the merge is to a new document (rather than the printer)
WordApp.ActiveDocument.MailMerge.Destination = wdSendToNewDocument
If Err Then
MsgBox Err.Description
Err.Clear
Exit Sub
End If


'Perform the mail-merge
WordApp.ActiveDocument.MailMerge.Execute
If Err Then
MsgBox Err.Description
Err.Clear
Exit Sub
End If

'Close the master document
g.LocateWordWindow WordApp, MainDocument
WordApp.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges


'Set focus to the window containing the merged document
g.LocateWordWindow WordApp, "Form Letters"


'View
If chkView.Value = 0 Then
WordApp.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
Else
WordApp.Visible = True
WordApp.WindowState = wdWindowStateMaximize
End If


'Release the WordApp Object, and Exit
WordApp.Exit
Set WordApp = Nothing

******** End of Code Snippet **********
 
D

Doug Robbins - Word MVP

Hi Chris,

Do it this way:

Dim Target As Document, Source As Document
Dim trange As Range, srange As Range
'Start an Instance of Word, or grab an already-existing one
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err Then
Err.Clear
Set WordApp = GetObject("", "Word.Application")
End If

'Supress merge warning message about data, otherwise document not opened as
merge mail document
WordApp.DisplayAlerts = wdAlertsAll

'Hide Word until merging is complete,
WordApp.Visible = False
WordApp.WindowState = wdWindowStateMinimize

'Open master (blank) document
Set Target = WordApp.Documents.Open(FileName:=MainDocument,
ReadOnly:=True)

'Determine the name used by Word for the Main Document
'so that it can be closed after the Merge is completed
MainDocument = StrRev(MainDocument)
n = InStr(MainDocument, "\")
MainDocument = StrRev(MainDocument)
If n > 0 Then
MainDocument = Right(MainDocument, n - 1)
End If

n = InStr(MainDocument, ".")
If n > 0 Then
MainDocument = Left(MainDocument, n - 1)
End If

'Insert documents to be included in package into master document
With Target
For n = 0 To lstDocuments.ListCount - 1
If lstDocuments.Selected(n) = True Then
Set Source = Documents.Open(lstDocuments.List(n))
If Err Then
MsgBox "Unable to locate document: " & lstDocuments.List(n),
vbInformation
Err.Clear
End If
With Source
Set srange = .Range
srange.Collapse wdCollapseEnd
srange.InsertBreak wdSectionBreakNextPage
End With
Set trange = .Range
trange.Collapse wdCollapseEnd
trange.FormattedText = Source.Range.FormattedText
Source.Close wdDoNotSaveChanges
End If
Next
'Change the next page section break at the end of the last document
inserted to a continuous section break
Set trange = .Range
trange.Collapse wdCollapseEnd
trange.Sections(1).PageSetup.SectionStart = wdSectionContinuous
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
S

SlavetoWasabi

Doug -

That seems to have done the trick - thank you very much for your help!

Chris
 
S

SlavetoWasabi

Doug -

One follow-up question. I'm noticing that in some instances the font in the
original document is not retained when the text is moved to the target
document. It appears that I can address this by using the Paste Options
button and selecting Retain original formatting. Is there a way I can
access this functionality programatically?

Thanks,
Chris
 
D

Doug Robbins - Word MVP

Check that in the Modify Styles dialog that the Automatically Update Styles
box is not checked.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
S

SlavetoWasabi

Doug -

That didn't seem to help. I had to upgrade to Office 2007 anyway, so I went
ahead and did that today. Still having the same problems, so I've been
trying out the pasting manually. I set all the default paste options under
the Advanced button to "Keep Source Formatting". However, when I do "Paste
Special", then "Formatted Text", the source formatting is not maintained and
when I click on the Paste Options icon that displays after the paste, the
paste option is always set to "Use Destination Styles". When I change it to
"Keep Source Formatting", then everything shows up the way I want it. I get
the same behavior from my automation. Interestingly enough, when I'm testing
the copy/paste out manually in Word, if I select "Paste" instead of "Paste
Special", it seems to be pasting with the "Keep Source Formatting" option,
and the document looks fine. What do I need to change in my automation to
replicate this behavior?

Thanks,
Chris
 
D

Doug Robbins - Word MVP

Instead of the command

trange.FormattedText = Source.Range.FormattedText

try

Source.Range.Copy
trange.Paste

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
S

SlavetoWasabi

Doug -

That resulted in the same behavior, but this command seems to have worked:

trange.PasteAndFormat (wdFormatOriginalFormatting)

I turned on the macro recording, and that's the command that Word was using
when I clicked on "Paste".

Thanks again for all your help,
Chris
 

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