G
Graham Mayor
My web site hosts an add-in created by fellow MVP Doug Robbins to split a
merge into individual files. This worked fine, but occasionally there was an
issue when the original document template on which the merge was based was
not available and the resulting merge, then based on the normal template,
produced some odd formatting.
To overcome that issue I added the following piece of code which saves the
document with a new name (so as to leave the original merge document
intact). The copy document is then stripped of content and saved as a
template and closed, The copied merge document is then reopened and the
template so created attached ready to merge..
Sub CreateMergeTemplate()
Dim sTempPath As String
Dim sQuery As String
Dim sRestore As String
Dim sATemp As String
Dim oSection As Section
Dim oStory As Range
Dim oMergeDoc As Document
If Documents.Count = 0 Then
MsgBox "No document present!" & vbCr & _
"Open the merge document and run this macro again", _
vbCritical, "Merge Template Creator"
Exit Sub
End If
Set oMergeDoc = ActiveDocument
If InStr(1, oMergeDoc, ".dot") Then
MsgBox "Active document is a template!" & vbCr & _
"Open the merge document and run this macro again", _
vbCritical, "Merge Template Creator"
Exit Sub
End If
If ActiveDocument.MailMerge.MainDocumentType <> wdFormLetters Then
MsgBox "Active document is not a letter merge document!" & vbCr & _
"Open the merge document and run this macro again", _
vbCritical, "Merge Template Creator"
Exit Sub
End If
sTempPath = Options.DefaultFilePath(Path:=wdUserTemplatesPath) & Chr(92)
With oMergeDoc
If Application.Version = 12 Then
.SaveAs FileName:="SplitMerge.docx", _
FileFormat:=wdFormatDocument
Else
.SaveAs FileName:="SplitMerge.doc", _
FileFormat:=wdFormatDocument
End If
sRestore = .FullName
For Each oSection In .Sections
oSection.Range.Delete
Next oSection
For Each oStory In .StoryRanges
oStory.Delete
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Delete
Wend
End If
Next oStory
Set oStory = Nothing
With .PageSetup
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
End With
If Application.Version = 12 Then
.SaveAs FileName:=sTempPath & "SplitMerge.dotx", _
FileFormat:=wdFormatTemplate
Else
.SaveAs FileName:=sTempPath & "SplitMerge.dot", _
FileFormat:=wdFormatTemplate
End If
sATemp = .FullName
.Close SaveChanges:=wdDoNotSaveChanges
End With
Documents.Open sRestore
With ActiveDocument
.AttachedTemplate = sATemp
.Save
End With
End Sub
This produces an error message when run on Word 2007 docx format merge
document from the SaveAs function and is unable to attach the template, but
it works fine on Word 2003
As a temporary workaround I have changed the sections below to work in
compatibility mode thus:
If Application.Version = 12 Then
.SaveAs FileName:="SplitMerge.doc", _
FileFormat:=wdFormatDocument97
and
If Application.Version = 12 Then
.SaveAs FileName:=sTempPath & "SplitMerge.dot", _
FileFormat:=wdFormatTemplate97
which does work without error, but I would prefer to work with docx and dotx
formats in case the merge document contains features introduced by Word
2007. Does anyone have any insights into what this needs to force it to work
in Word 2007's native formats?
Thanks
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
merge into individual files. This worked fine, but occasionally there was an
issue when the original document template on which the merge was based was
not available and the resulting merge, then based on the normal template,
produced some odd formatting.
To overcome that issue I added the following piece of code which saves the
document with a new name (so as to leave the original merge document
intact). The copy document is then stripped of content and saved as a
template and closed, The copied merge document is then reopened and the
template so created attached ready to merge..
Sub CreateMergeTemplate()
Dim sTempPath As String
Dim sQuery As String
Dim sRestore As String
Dim sATemp As String
Dim oSection As Section
Dim oStory As Range
Dim oMergeDoc As Document
If Documents.Count = 0 Then
MsgBox "No document present!" & vbCr & _
"Open the merge document and run this macro again", _
vbCritical, "Merge Template Creator"
Exit Sub
End If
Set oMergeDoc = ActiveDocument
If InStr(1, oMergeDoc, ".dot") Then
MsgBox "Active document is a template!" & vbCr & _
"Open the merge document and run this macro again", _
vbCritical, "Merge Template Creator"
Exit Sub
End If
If ActiveDocument.MailMerge.MainDocumentType <> wdFormLetters Then
MsgBox "Active document is not a letter merge document!" & vbCr & _
"Open the merge document and run this macro again", _
vbCritical, "Merge Template Creator"
Exit Sub
End If
sTempPath = Options.DefaultFilePath(Path:=wdUserTemplatesPath) & Chr(92)
With oMergeDoc
If Application.Version = 12 Then
.SaveAs FileName:="SplitMerge.docx", _
FileFormat:=wdFormatDocument
Else
.SaveAs FileName:="SplitMerge.doc", _
FileFormat:=wdFormatDocument
End If
sRestore = .FullName
For Each oSection In .Sections
oSection.Range.Delete
Next oSection
For Each oStory In .StoryRanges
oStory.Delete
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Delete
Wend
End If
Next oStory
Set oStory = Nothing
With .PageSetup
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
End With
If Application.Version = 12 Then
.SaveAs FileName:=sTempPath & "SplitMerge.dotx", _
FileFormat:=wdFormatTemplate
Else
.SaveAs FileName:=sTempPath & "SplitMerge.dot", _
FileFormat:=wdFormatTemplate
End If
sATemp = .FullName
.Close SaveChanges:=wdDoNotSaveChanges
End With
Documents.Open sRestore
With ActiveDocument
.AttachedTemplate = sATemp
.Save
End With
End Sub
This produces an error message when run on Word 2007 docx format merge
document from the SaveAs function and is unable to attach the template, but
it works fine on Word 2003
As a temporary workaround I have changed the sections below to work in
compatibility mode thus:
If Application.Version = 12 Then
.SaveAs FileName:="SplitMerge.doc", _
FileFormat:=wdFormatDocument97
and
If Application.Version = 12 Then
.SaveAs FileName:=sTempPath & "SplitMerge.dot", _
FileFormat:=wdFormatTemplate97
which does work without error, but I would prefer to work with docx and dotx
formats in case the merge document contains features introduced by Word
2007. Does anyone have any insights into what this needs to force it to work
in Word 2007's native formats?
Thanks
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>