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
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