PDA

View Full Version : Please help - mail merge saving each page as separate file with mergefield as filenam



jbndylan
08-17-2015, 10:16 AM
Hi all,

May you point me in the right directions . . . .

I am trying to write a vba to run a mail merge and save each page (letter) with a mail merge field as part of the file name
Basically mergefield+dt.docx.
I'm using Word 10.

Here goes . . .

I am currently getting a "compile error method or data member not found" on the fname.end = filename.end -1 statement.

Sub PVOHLetterSave11()

Dim fname As MailMergeDataField
Dim StrName As String


' Used to set criteria for moving through the document by section.
Application.Browser.target = wdBrowseSection

With ActiveDocument
StrFolder = .path & Application.PathSeparator

'A mailmerge document ends with a section break next page.
For i = 1 To .MailMerge.DataSource.RecordCount

Set fname = ActiveDocument.MailMerge(i).DataFields("MASTER_VENDOR_MNEMONIC").Range
fname.End = fname.End - 1
dt = Format(CStr(Now), "mmddyyy")
If Trim(.DataFields("MASTER_VENDOR_MNEMONIC")) = "" Then Exit For
StrName = .DataFields("MASTER_VENDOR_MNEMONIC") & "_" & dt


' Removes the break that is copied at the end of the section, if any.
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1


ChangeFileOpenDirectory "\\vcuhshmo\groups\Claims Administration\CCU\PVOH\PVOH Mail Merge Letters"
docnum = docnum + 1
ActiveDocument.SaveAs fileName:=StrName.Text & ".docx"

ActiveDocument.ExportAsFixedFormat OutputFileName:= _
ActiveDocument.path & "\" & ActiveDocument.Name & ".pdf", ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False


' ActiveDocument.Close
' Move the selection to the next section in the document
Application.Browser.Next

Next i

' ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

gmayor
08-18-2015, 01:55 AM
You will save yourself a lot of grief if you use http://www.gmayor.com/individual_merge_letters.htm or http://www.gmayor.com/MergeAndSplit.htm to merge the document and create the separate documents/pdfs on the fly.

The problem with splitting after the merge is that the fields don't exist after the merge, they are converted to text, so you are going to have to work with ranges to identify the field name value. Then there is the issue of duplicate filenames to contend with, and of course problems of formatting. The first issue can be accommodated in the merge document and there is code in the first of those links that explains how, but frankly it is easier to use the tools provided on those pages.

jbndylan
08-18-2015, 05:21 AM
Hi Graham, Thank you for the input, but my company does not allow adding non-standard add ins. Are there any other suggestions?

gmayor
08-18-2015, 10:54 PM
If you cannot get your company IT department to evaluate the add-in (which incidentally is used by thousands of individuals, businesses and government departments around the world) then you are going to have to re-invent the wheel. There are code examples in the first of the linked pages.

Frankly a company that doesn't allow proven add-ins, but will allow users to tinker with VBA, is its own worst enemy.

jbndylan
08-19-2015, 06:33 AM
OK, I appreciate your feelings but do you have any other constructive ideas?

gmayor
08-19-2015, 09:02 PM
I have already said that the merge fields are converted to text in the process, so you cannot access them from the document, and I have shown you where to see sample code.