Solved: Loop VBA for Mail Merge Document
Hello all,
I am writing a Macro in word (have done plenty in excel, not so much in MS Word). The Macro will take a mail merge document, open the first merge record as a new document, update all fields (to get the image to update) and then export as a PDF.
I have 2 updates that need to be made to this code in order for it to work perfectly.
1st I need to loop this process to move on to the next merge record in the main mail merge document, open as a new document and export as a PDF and so on until all Merge records have generated an individual PDF file.
2nd is I need the file name of the PDF to change based on a merge field (employee ID for example).
The end result would be multiple PDF documents saved to a folder with file names based on the employee's ID.
What I have so far is:
[VBA]Sub Macro1()
'
' Macro1 Macro
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
.LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
End With
.Execute Pause:=False
End With
ActiveWindow.ActivePane.VerticalPercentScrolled = 64
Selection.WholeStory
Selection.Fields.Update
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"Q:\GENERIC FILE PATH" & ".pdf", ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, FROM:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
ActiveWindow.Close
End Sub[/VBA]
Any help with the two items above would be GREATLY appreciated!!!
Thanks for taking the time to look
Code that worked on this!
Quote:
Originally Posted by
cfluegel
Hello all,
I am writing a Macro in word (have done plenty in excel, not so much in MS Word). The Macro will take a mail merge document, open the first merge record as a new document, update all fields (to get the image to update) and then export as a PDF.
I have 2 updates that need to be made to this code in order for it to work perfectly.
1st I need to loop this process to move on to the next merge record in the main mail merge document, open as a new document and export as a PDF and so on until all Merge records have generated an individual PDF file.
2nd is I need the file name of the PDF to change based on a merge field (employee ID for example).
The end result would be multiple PDF documents saved to a folder with file names based on the employee's ID.
What I have so far is:
[VBA]Sub Macro1()
'
' Macro1 Macro
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
.LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
End With
.Execute Pause:=False
End With
ActiveWindow.ActivePane.VerticalPercentScrolled = 64
Selection.WholeStory
Selection.Fields.Update
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"Q:\GENERIC FILE PATH" & ".pdf", ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, FROM:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
ActiveWindow.Close
End Sub[/VBA]
Any help with the two items above would be GREATLY appreciated!!!
Thanks for taking the time to look
==============================================
Hello Buddy, I am not sure if you have figured it out how to loop through the records in your code.
I have done example and it works best for me...i am not a hardcore coder, however I have tried my best... hope this helps!
In the below code, I have set "I" as user's input which is purely my project's requirement. once user enters the number, loop will start with that numbered line of record and generates new document until its done with generating documents.
Dim I As Integer 'Merge Record
Dim StartPoint As Integer 'Enter from which point you want to run certificate
Dim Num As Integer 'Number of Certificate Counts
Dim sText As String 'Blank text
Dim YesNo As String 'Yes No message box
Dim setPath As FileDialog 'SaveAs folder
Dim ActPath As String 'Set Path
'Warning Message before running following codes
YesNo = MsgBox("Are you sure you want to run thi Macro", vbYesNo + vbExclamation, "MailMerge Macro")
If YesNo = vbYes Then GoTo Line1 Else GoTo Line2
Line1:
'Take certificate counts from user
StartPoint = InputBox(Prompt:="Start Generating Certificate from: ", Title:="Start Certificate Number")
Num = InputBox(Prompt:="Generate Certificates till: ", Title:="End Certificate Number")
'Merge Individual Document in simultaneously
For I = StartPoint To Num
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = I
.LastRecord = I
End With
.Execute Pause:=False
End With