Using Excel VBA to open a Word document and enable Word macros

Strider71

New Member
Joined
Jan 27, 2012
Messages
7
I have an Excel 2007 macro that opens up a Word document and runs a Word macro that does a mail merge. It works great in Excel 2007, but I just switched to Excel 2010 (and Word 2010 of course). The Excel code still opens the Word file, but the macros are disabled when it opens Word so it doesn't run the mail merge macro.
Even if I enable Word macros in the Trust Center Settings (I know that's not recommended) it still doesn't allow me to run the Word macro.
If I open the Word file on my own and enable the macro, it works just fine.

The Excel code to Open Word and run the macro is:

Code:
Sub Run_Mail_Merge()
Dim wordApp As Object
    Set wordApp = CreateObject("Word.Application")
    wordApp.Documents.Open Filename:="D:\Form Letter.docm"
    wordApp.Visible = True
    wordApp.Run "!newmacros.MailMergeMacro"
End Sub


My Word code (which won't run if the Excel macro opens the file), is:


Code:
Sub MailMergeMacro()
If ActiveDocument.MailMerge.State = wdMainAndDataSource Then
    ActiveDocument.MailMerge.Execute
End If
ActiveDocument.SaveAs FileName:="Completed Letter.doc", 
   FileFormat:=wdFormatDocument, LockComments:=False, Password:="",AddToRecentFiles:= True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
        False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False
End Sub

Any insight would be greatly appreciated!
Thanks.
-Strider71
 
My code should open the merge mail document, and then ask whether you want to open the source data. If you answer Yes, it will load the source data, and you'll be able to preview the results. If you answer No, the source data isn't loaded. Isn't this what happens when you run my code?
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
No sorry, it does exactly what my code does, no preview
No doubt both codes pick up the record/s for mail merge (display the correct count with prev/next record buttons) but wont allow preview! Only possible after .destination event Only!
 
Upvote 0
It appears when mergefile is executed via VBA, there is no preview as it expects a fully automated merge through to print or new document...
Unless ofcourse you are able to use prev/next record in word mergemail file on opening via vba excel??
 
Upvote 0
tomcatonnet99: When you execute a mailmerge via VBA, you don't get any of the normal UI access/prompts or preview. If you need that functionality, you need to roll your own. Providing your own SQL-type prompt is quite easy; previews are something else. One way of generating previews (of sorts) would be to send the merge for each record to a separate document (not difficult) and let the user see those before deciding what to do with them (e.g. save/print, add to an aggregation document, discard). Previewing all records, though, would be a PITA in some cases, in which case, I'd be questioning the reason for wanting previews.
 
Upvote 0
I've tested my macro in Excel 2010 and 2013. In each version, it behaves as expected. It opens the merge document, and asks whether to open the data source. If Yes, it opens the data source and the preview results are available. You must have changed my code somehow. After taking another look at your recent code, it looks like you may have replaced...

Code:
SQLStatement:="SELECT * FROM `Sheet1$`"

with

Code:
SQLStatement:="SELECT * FROM `Sheet1$` WHERE `Printed`='N'"
 
Upvote 0
Got the previous/next preview working

ActiveDocument.ActiveWindow.View.ShowFieldCodes = False

Set myMerge = ActiveDocument.MailMerge

If myMerge.State = wdMainAndSourceAndHeader Or _

myMerge.State = wdMainAndDataSource Then

myMerge.ViewMailMergeFieldCodes = False

End If
 
Upvote 0
Now with one mergefield (alphanumeric) I need to insert double space, any idea why it results in error bookmark not found! On mail merge
If I insert double spaces it wont update at times
 
Upvote 0

Forum statistics

Threads
1,215,148
Messages
6,123,301
Members
449,095
Latest member
Chestertim

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top