Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mail Merge Document Separation 1

Status
Not open for further replies.

JayDM

IS-IT--Management
May 25, 2007
9
GB
Hi, im trying to separate mail merge documents into separate files so i can save each letter in a mail merge as a separate file.

Code:
Save each merged letter as a separate file
Normally, the mail merge result to a new document is one long file which can be edited and printed. Using the Master Document feature, it's possible to save each as a separate file. In the mail merge result document, each record's letter is created in its own section, so one can select each section, turn it into a sub-document, open the sub-document and save it as a separate file.

The only preparation you need to make in the main merge document is to select the first paragraph, go to Format/Paragraph and set the Outline Level to 1 (one). The Master Document feature needs this in order to create the sub-document.

The following sample VBA code can help automate this process.

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

that is what i've been given as a guide - yet it makes no real sense and although i've followed it as much as i can, it isnt giving me anything useful.

any help at all would be really appreciated as i need to be able to do this asap.

thanks

Jay
 



Hi,

On your MailMerge toolbar is the View Merged Data button that turns the display of the merge field DATA on and off.

Turn it on.

Turn on your macro recorder and record printing and advancing one record.

go to your VB Editor and wrap your recorded code in a simple loop for the record count.

Skip,

[glasses] [red][/red]
[tongue]
 
well i found one just after posting that seems to work

but it saves as in the format

page15ofmm2

mm2 being the original file name

what i really need - is to have the company name, and date in the file name -

eg

ABCCompany_25/5/07

i have the mail merge field for company name.

is there any possible way of doing this?

Code:
Sub EachPageDoc()

Dim aDoc As Document
Dim lngPageCount As Long, lngCurrentPage As Long
Dim var As Variant

Set aDoc = ActiveDocument
lngCurrentPage = 1
  ' use constant for number of pages
lngPageCount = aDoc.BuiltInDocumentProperties(14)

' turn off screen updating otherwise
' your brain will turn into mush
    Application.ScreenUpdating = False

'  go to top of doc
    Selection.HomeKey Unit:=wdStory
For var = 1 To lngPageCount

' make selection of page, and copy it
    aDoc.Bookmarks("\page").Select
    Selection.Copy

' make new document, paste, and save
    Application.Documents.Add
      Selection.PasteSpecial
        ActiveDocument.SaveAs FileName:=aDoc.Path & _
            Application.PathSeparator & "Page" & lngCurrentPage & "of" & aDoc.Name
        ActiveDocument.Close


' go to next page
    aDoc.Activate
    Selection.GoTo what:=wdGoToPage, Which:=wdGoToNext, Count:=1, Name:=""
    lngCurrentPage = lngCurrentPage + 1
Next

' turn screen update back on
    Application.ScreenUpdating = True

' release aDoc
    Set aDoc = Nothing

MsgBox "Done."
End Sub

this is the code i am NOW using, was mentioned on a previous post.
 




It's not that complex!
Code:
Sub PrintEachRecipient()
    Dim i As Integer
'
    For i = 1 To ActiveDocument.MailMerge.DataSource.RecordCount
        ActiveDocument.PrintOut
        ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
    Next
End Sub

Skip,

[glasses] [red][/red]
[tongue]
 
thanks - its been a long time since i looked at vb and by now i've forgotten how to link to mailmerge and everything.

would i just copy that code exactly? and that would work?
 




As long as your document is already a Mail Merge document, with a valid data source, just copy the code into a module and run.

Skip,

[glasses] [red][/red]
[tongue]
 
what i've done is moved the mailmerge so it shows as 67 pages long with each merge able to edit individually.

im sorry to be a pain, but, could you show me what the entire code should look like that i would paste into the vba.

from start to finish the whole code.

thanks
 




That's ALL the code. I actually modified it a little: here's the revised version, that sets it up for you...
Code:
Sub PrintEachRecipient()
    Dim i As Integer
'
    With ActiveDocument.MailMerge
        'SHOW ME THE DATA, rather than the merge codes
        .ViewMailMergeFieldCodes = False

        For i = 1 To .DataSource.RecordCount
            ActiveDocument.PrintOut
            .ActiveRecord = wdNextRecord
        Next
    End With
End Sub
You do NOT perform the MailMerge first!

This code performs the MERGE.

You just open the document that has the source data connection and the Merge Fields coded in the proper places in your document.

Skip,

[glasses] [red][/red]
[tongue]
 
i get a compile error

method or data member not found
 
also, doesnt your code tell me to print it out rather than save?

im basically completely new to vba as its been so long. the closest i've had is the one i mentioned on my second post - but that saves it as

pagexofmm2.doc

mm2 is the mailmerged filename

the company field in the Mail Merge is «Name»

i want it to save each document as basically the following

«Name»_DATE.doc

eg.

ABCCompany_25-5-07.doc

printing out isnt the problem its saving under different names that i want


thanks

Jay
 




did you check FAQ's???

How to MAIL MERGE and PRINT each Recipient Separately faq68-6655

How to MAIL MERGE and SAVE EACH DOCUMENT SEPARATELY faq68-5187

Skip,

[glasses] [red][/red]
[tongue]
 
i read it, but i still arent sure how to save exactly how i want.

here is the folder i want it to save into

S:\Finance\LettersToClients\

and it would be «Name» as the first part - that is the field for company name

and then i want the date the letter was saved as at the end.

 


S:\Finance\LettersToClients\
That's self explanatory in the code


and it would be «Name» as the first part - that is the field for company name

and then i want the date the letter was saved as at the end.

ActiveDocument.SaveAs x & .DataFields("Name").Value & Format(Date, "yyyymmdd") & ".doc"



Skip,

[glasses] [red][/red]
[tongue]
 
thats absolutely top class brilliant - thanks a load.

just what i needed

 
Hi skip,

I need that faq68-5187 to work but I get .Recordcount=-1 and wdNextRecord = -2

Tried with a query and the table itshelf for the DataSource.Connection with no luck

Could you think of something?

Using A2k3 with Word2003 on WinXP pro
 




Jerry,

Hmmmmmm?????

I have Windows XP Pro, Office 2003 Word

Just ran a test with no problems, using an Excel WB as database.

What kind of data source are you pointing to?

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top