PDA

View Full Version : Making Macros Available in a merged document?



FhM
10-13-2009, 04:58 AM
Me again :) another day another problem. I have a different problem today in that I want to run some macros on a mail merge output. I want to do this automatically in some cases and in others I would like to make a button to do it. I want this merge document to be portable so I don't want to save the macros in the global template.

I have my code all written and I tried placing it in several places (This document, a module etc) but when you run the merge document you cannot seem to call the macro from the normal menu and and buttons you make disappear. I suppose what I want is this original merge document to act like a template. Is this possible at all?

macropod
10-14-2009, 03:15 PM
Hi FhM,

I assume the menu & buttons you refer to are in your mailmerge main document.

If you put both your existing code, plus the following code in your mailmerge main document and execute your mailmerge from the 'RunMerge' macro, you'll have the option whether to run your extra code against the mailmerge output. This should work OK even if the subs you're running against the mailmerge output are coded for the active document.
Sub RunMerge()
Application.ScreenUpdating = False
Dim MainDoc As Document, Resp As Variant
Set MainDoc = ActiveDocument
With MainDoc.MailMerge
If .State = wdMainAndDataSource Then
.Destination = wdSendToNewDocument
.Execute
End If
End With
Resp = MsgBox("Run Child Process?", _
vbYesNo, "Child Process Options")
If Resp = vbYes Then
Call ChildProcess
MainDoc.Activate
End If
Application.ScreenUpdating = True
End SubNaturally, replace the dialogue text with whatever is appropriate to your situation and 'ChildProcess' with whatever your existing main sub is called.

As coded, answering 'Yes' to the prompt and executing the 'ChildProcess' macro leaves the mailmerge main document active, whereas not doing so leaves the mailmerge output document active.

FhM
10-15-2009, 12:11 AM
Thank you I will have a look at this later. Much appreciated.