J
Jack Sons
Doug,
I have problems with the code below. Suzanne Barnhill thouht you are the
author. I formulated my problem below the code (it is my original mail to
Suzanne, already with an attachment). You see that is is absolutely
necessary to send you a sample of the mailmerged document that causes
execution of the code to derail. If you want to help me, please allow me to
mail my document directly to you, as I can't post in the NG with an
attachment.
Jack Sons
The Netherlands
----------------------------------------------------------------------------
------------------------
Sub SaveRecsAsFiles()
' Convert all sections to Subdocs
AllSectionsToSubDoc ActiveDocument
'Save each Subdoc as a separate file
SaveAllSubDocs ActiveDocument
End Sub
Sub AllSectionsToSubDoc(ByRef doc As Word.Document)
Dim secCounter As Long
Dim NrSecs As Long
NrSecs = doc.Sections.Count
'Start from the end because creating
'Subdocs inserts additional sections
For secCounter = NrSecs - 1 To 1 Step -1
doc.Subdocuments.AddFromRange _
doc.Sections(secCounter).Range
Next secCounter
End Sub
Sub SaveAllSubDocs(ByRef doc As Word.Document)
Dim subdoc As Word.Subdocument
Dim newdoc As Word.Document
Dim docCounter As Long
docCounter = 1
'Must be in MasterView to work with
'Subdocs as separate files
doc.ActiveWindow.View = wdMasterView
For Each subdoc In doc.Subdocuments
Set newdoc = subdoc.Open
'Remove NextPage section breaks
'originating from mailmerge
RemoveAllSectionBreaks newdoc
With newdoc
.SaveAs FileName:="MergeResult" & CStr(docCounter)
.Close
End With
docCounter = docCounter + 1
Next subdoc
End Sub
Sub RemoveAllSectionBreaks(doc As Word.Document)
With doc.Range.Find
.ClearFormatting
.Text = "^b"
With .Replacement
.ClearFormatting
.Text = ""
End With
.Execute Replace:=wdReplaceAll
End With
End Sub
----------------------------------------------------------------------------
------------------------------------------------------------------
A year or two ago I got code for splitting a mail mergerd document into its
separate parts and saving these parts.
I hope I made clear what I mean. In other words: after mailmerging a new
document exists that comprises a number of documents that are identical
except for the merged data in it, for instance the name and address of the
adressees.
The code below will not work to split my standard mail merged
correspondence of which I would like to put a small example below the code,
but it is to complex, some items disappear. Therefore I have to put it in an
attachment. Have no fear opening it, all my incoming and outgoing stuff is
screened by my provider and also by my own Norton Anti Virus.
I often produce mail merged letters consisting of 20 to 50 separate
letters. I always have to extract the individual sub letters in a tedious
way, by selecting one and deleting all that is above it and all that is
below it, then saving the remaining separate letter, then calling and
opening the complete mail merged document again, selecting the next sub
document, deleting all that is above it etc. 50 times! It is awfully time
consuming.
You would be an enormous help if you could give me the code that does the
trick.
For your information, the first subdocument should be saved as "Ermakova 1
SHS.doc". It is clear where the Ermakova and the SHS come from, the 1 is the
number between the --, so it is the one of -1- Ermakova is the last word of
the first line (with text) after Aan: (Aan: could or could not be
underlined).
If automatically saving with a name in the way I indicated is to complex,
saving via a prompt (kind of box in which to fill in the complete name, so
in this case Ermakova 1 SHS.doc) that wil be fine for me, perhaps that is a
safer solution than automatically naming what is to be saved, although it
would be nice if both options where available to me so I can choose which
one to use for a particular mail merged document (if all adresses are simple
and straight forward I would do it the automatical way, otherwise via the
prompt box).
Well again, my life will be a lot easier if you could get this to work. A
thousand thanks in advance. Please don't mind my somewhat peculiar English.
I have problems with the code below. Suzanne Barnhill thouht you are the
author. I formulated my problem below the code (it is my original mail to
Suzanne, already with an attachment). You see that is is absolutely
necessary to send you a sample of the mailmerged document that causes
execution of the code to derail. If you want to help me, please allow me to
mail my document directly to you, as I can't post in the NG with an
attachment.
Jack Sons
The Netherlands
----------------------------------------------------------------------------
------------------------
Sub SaveRecsAsFiles()
' Convert all sections to Subdocs
AllSectionsToSubDoc ActiveDocument
'Save each Subdoc as a separate file
SaveAllSubDocs ActiveDocument
End Sub
Sub AllSectionsToSubDoc(ByRef doc As Word.Document)
Dim secCounter As Long
Dim NrSecs As Long
NrSecs = doc.Sections.Count
'Start from the end because creating
'Subdocs inserts additional sections
For secCounter = NrSecs - 1 To 1 Step -1
doc.Subdocuments.AddFromRange _
doc.Sections(secCounter).Range
Next secCounter
End Sub
Sub SaveAllSubDocs(ByRef doc As Word.Document)
Dim subdoc As Word.Subdocument
Dim newdoc As Word.Document
Dim docCounter As Long
docCounter = 1
'Must be in MasterView to work with
'Subdocs as separate files
doc.ActiveWindow.View = wdMasterView
For Each subdoc In doc.Subdocuments
Set newdoc = subdoc.Open
'Remove NextPage section breaks
'originating from mailmerge
RemoveAllSectionBreaks newdoc
With newdoc
.SaveAs FileName:="MergeResult" & CStr(docCounter)
.Close
End With
docCounter = docCounter + 1
Next subdoc
End Sub
Sub RemoveAllSectionBreaks(doc As Word.Document)
With doc.Range.Find
.ClearFormatting
.Text = "^b"
With .Replacement
.ClearFormatting
.Text = ""
End With
.Execute Replace:=wdReplaceAll
End With
End Sub
----------------------------------------------------------------------------
------------------------------------------------------------------
A year or two ago I got code for splitting a mail mergerd document into its
separate parts and saving these parts.
I hope I made clear what I mean. In other words: after mailmerging a new
document exists that comprises a number of documents that are identical
except for the merged data in it, for instance the name and address of the
adressees.
The code below will not work to split my standard mail merged
correspondence of which I would like to put a small example below the code,
but it is to complex, some items disappear. Therefore I have to put it in an
attachment. Have no fear opening it, all my incoming and outgoing stuff is
screened by my provider and also by my own Norton Anti Virus.
I often produce mail merged letters consisting of 20 to 50 separate
letters. I always have to extract the individual sub letters in a tedious
way, by selecting one and deleting all that is above it and all that is
below it, then saving the remaining separate letter, then calling and
opening the complete mail merged document again, selecting the next sub
document, deleting all that is above it etc. 50 times! It is awfully time
consuming.
You would be an enormous help if you could give me the code that does the
trick.
For your information, the first subdocument should be saved as "Ermakova 1
SHS.doc". It is clear where the Ermakova and the SHS come from, the 1 is the
number between the --, so it is the one of -1- Ermakova is the last word of
the first line (with text) after Aan: (Aan: could or could not be
underlined).
If automatically saving with a name in the way I indicated is to complex,
saving via a prompt (kind of box in which to fill in the complete name, so
in this case Ermakova 1 SHS.doc) that wil be fine for me, perhaps that is a
safer solution than automatically naming what is to be saved, although it
would be nice if both options where available to me so I can choose which
one to use for a particular mail merged document (if all adresses are simple
and straight forward I would do it the automatical way, otherwise via the
prompt box).
Well again, my life will be a lot easier if you could get this to work. A
thousand thanks in advance. Please don't mind my somewhat peculiar English.