Forum Discussion
Using Mail Merge with MACROS
Hello,
I am currently trying to use Mail Merge from EXCEL with a WORD document to be emailed in OUTLOOK
The emailed document takes the form of a .pdf attachment
Our designer has created a Customer Order Update document that requires a WORD MACRO to realise his formatting once the Merge has taken place.
I have created a MACRO in the WORD document that is able to perform the correct formatting on the WORD document after the merge with EXCEL spreasheet data has taken place.
However, with a multiple page document I find that the MACRO is only applied to the 1st page of the document
Subsequent pages of the generated .pdf do NOT have the MACRO applied
( I have various detail lines that are hash preceded capital letters that trigger the MACRO to perform a subtitle substitution )
As the MACRO goes through 25 lines of detail (below the fixed document header) in a Loop to perform substitutions I wondered if expanding the Loop to 50 lines would allow the second page of the document to also receive the MACRO processing. This did NOT happen
Please advise how I can get the MACRO to apply to every page of the mail merged document
I do not believe that the MACRO can only be applied once to the 1st page of the mail mereged document only
regards
4 Replies
Can you respond with the code that you have in the macro so that we can make some appropriate suggestions.
- Charles_KenyonBronze Contributor
Sticking my two bits in here...
Try using Range rather than selection and cycle through Word sections.
A mail merge will create a new section for each record. (If the primary mail merge document has multiple sections, you will need to account for that as well.
Hi! A Word macro is not applied separately to each merged page—the merged result is one document containing sections. Run the formatting macro after MailMerge.Execute, loop through the resulting document's sections or paragraphs rather than a fixed number of lines, and export the completed result to PDF only after that loop finishes.
- ChrisBlanchardCopper Contributor
Hello Jamony
I do not know how to address the next section to apply the MACRO. Although the resulting pdf occupied two pages addressing the second page using (an absolute setting for test purposes):
Selection.GoTo wdGoToPage, wdGoToAbsolute, 2
merely resulted in the MACRO applied to the 1st page again, which is what you suggested could happen
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord - this worked interactively but would not work with the actual merge to create the document, as I presume merge would no longer be active then, and even then some changes from the previus page were carried over
I had two tables on a single page so I wondered if the merge would generate extra tables on the next page for the same document
Thus, I used:
ActiveDocument.Tables(1).Cell(index, 1).Select
To select the 1st cell in the table on the 1st page
I wondered if new Tables were created for subsequent pages in the same active document
when I ran
ActiveDocument.Tables(3).Cell(index, 1).Select
the table was not found
I tried to see if the SECTIONS property of ACTIVEDOCUMENT could be used
ActiveDocument.Sections.Count = 2
An attempt at using this to navigate resulted in an error applying an assignment to a read only property
I still seem to be trapped applieing my macro to the 1st page of the document
regards