none
Word 2013 mail merge in macro not working if macro called via command line. RRS feed

  • Question

  • We are looking to upgrade to Word 2013 and in testing Word 2013 I found that the mail merge macros are no longer working when Word 2013 is opened and a macro is called from a program.  We also had the same issue with Word 2010 but were able to find a workaround.  The workaround for Word 2010 was to call a different macro with did some processing before calling the Mail Merge macro (the extra processing did not hurt the documents that did not need it).  This workaround no longer works with Word 2013.

    I can recreate the same issue as my program opening Word and calling a macro to do the mail merging by entering the command via a shortcut on the disktop.


    This is the Target for the shortcut:

    "C:\Program Files\Microsoft Office\Office15\WINWORD.EXE" /mMailMergeTest "C:\TEMP\master\PB21945.DOC"


    The template for the PB21945 document contains the following macro:

    Public Sub MailMergeTest()

        Dim fileDirDataDoc As String

        fileDirDataDoc = "C:\TEMP\MailMergeData.DOC"  ' was fileDirChartsDoc

        With ActiveDocument.MailMerge
           .OpenDataSource fileDirDataDoc
           .Destination = wdSendToNewDocument
           .Execute
        End With

    End Sub


    When I run the macro via the shortcut MailMerge does not happen when using Word 2010 or Word 2013.  If I open the document and trigger the macro within Word then the mail merge happens.  I know that the macro is called via the shortcut because in the real macro I do things after the MailMerge call like deleting the pre-merge document and setting the merged document and template to "Saved" and these things are happening.

    How do I get MailMerge in a macro to work when running the macro via the command line?



    • Edited by Ron Clutter Tuesday, November 5, 2013 12:38 AM Changed Comment from // to single quote.
    Monday, November 4, 2013 3:34 PM

Answers

  • Cindy, creating the AutoOpen macro will not work for me because we run four different macros based on the processing that we have to do before running the mail merge.

    This is the solution I came up with...

    'Add the following at the top of the module that contains the mail merge macro:
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    'Add the following to the mail merge macro before the ActiveDocument.MailMerge.Execute call:
    Dim cnt As Integer
    cnt = 10  'NOTE: should work with cnt = 1 but I made cnt bigger just in case a PC is slow in production
    Do While cnt > 0
      DoEvents
      Sleep (25)
      cnt = cnt - 1
    Loop

    'NOTE: I use signed Word Templates to bypass warning message about macros.  If you get the warning message because you are using an unsigned template then the mail merge will work because Word has time to open the document before running mail merge.

    'NOTE2: Added "ActiveDocument.ActiveWindow.View = wdPrintView" in a couple of places in my macros because of a Run-Time error '4506' caused by Word 2013 opening readonly documents in "reading" mode and the macros trying to change the document.

    • Marked as answer by Ron Clutter Thursday, November 7, 2013 8:12 PM
    Thursday, November 7, 2013 8:12 PM

All replies

  • Cross-posted at: http://www.techsupportforum.com/forums/f57/word-2013-mail-merge-in-macro-not-working-if-macro-called-via-command-line-742817.html
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    FWIW, there are some inconsistencies in the code you've posted - you declare and use 'Dim fileDirDataDoc As String', but you populate the undeclared 'fileDirChartsDoc = "C:\TEMP\MailMergeData.DOC"'. That alone would be enough to break the process.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Monday, November 4, 2013 8:59 PM
    Monday, November 4, 2013 8:53 PM
  • Hello,

    Did you try saving the document that contains the macro as "Word Macro-Enabled Document (*.docm)" and invoking from command line as "<path>\winword.exe /m MaiMergeTest <macro-file-path>\abc.docm ?

    Also, please check in the above listed code, fileDirDataDoc is not assigned but used with OpenDataSource.


    Thanks, Pradeep Kumar R

    Monday, November 4, 2013 10:32 PM
  • Sorry about the cross-posting.  My team lead had told me to post on more than one forum to see if anyone will help me.  This is the main post.

    Yes, it should have been fileDirDataDoc.  I was trying to remove my project's name "Charts" from my post and missed a spot. 

    Now that I have said I am sorry about the cross-posting will anyone help me? 

    I would be interested to hear is anyone else can do the mail merge macro from a command line using Word 2013 and have it work for them.  I am not sure that there not something in how Word 2013 is configured on my test computer that is causing the issue.

    Monday, November 4, 2013 10:41 PM
  • We have 500+ documents in the DOC format and did not want to change and test all of them.  The macro's are being called because the other code besides the Mail Merge functionality works.

    Yes, it should have been fileDirDataDoc.  I was trying to remove my project's name "Charts" from my post and missed a spot.  I fixed it in the post.

    Monday, November 4, 2013 10:48 PM
  • Hi Ron,

    I doubt anyone objects to cross-posting per se. All that is usually asked is that when doing so you provide links to the other posts in all of the forums concerned. That way, people don't waste time trying to reinvent the wheel and/or solve problems that have already been solved.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, November 4, 2013 11:48 PM
  • I tried my example on my home computer using Word 2010 and it mail merged at home.  I do not have Word 2013 at home so I cannot try it too.
    Tuesday, November 5, 2013 12:44 AM
  • I tried changing my document/template from DOC/DOT to DOCM/DOTM.  Still getting the same results.

    We have a default MS Office 2013 install except for:
    Microsoft Word 2013 > Word Options > Security > Trust Center > Trusted Locations > Allow Trusted Locations on the network = Enabled
    Microsoft Word 2013 > Privacy > Trust Center > Disable Op-in Wizard on first run = Enabled
    Microsoft Word 2013 > Privacy > Trust Center > Enable Customer Experience Improvement Program = Disabled
    Microsoft Word 2013 > Privacy > Trust Center > Automacticlly receive small updates to improve reliability = Disabled
    Microsoft Word 2013 > Privacy > Trust Center > Send Office Feedback = Disabled
    Microsoft Word 2013 > Privacy > Trust Center > Allow including screenshot with Office Feedback = Disabled
    Microsoft Word 2013 > Miscellaneous > Do not use hardware graphics acceleration = Enabled
    Microsoft Word 2013 > Miscellaneous > Show SkyDrive Sign In = Disabled
    Microsoft Word 2013 > First Fun > Disable First Run Movie = Enabled
    Microsoft Word 2013 > First Fun > Disable Office First Run on application boot = Enabled

    We added the following registry entry to remove the "Opening this document will run the following SQL command" message:
    HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Word\Options > SQLSecurityCheck REG_DWORD 0x00000000

    Tuesday, November 5, 2013 9:54 PM
  • Hi Ron

    Mmm. The first thing that occurs to me is that Word may not be in a "ready state" to execute a mail merge in 2013. More and more, Word is doing things asynchronously (doing more than one thing at a time).

    How about if you put a macro named AutoOpen in the file that calls MailMergeTest and remove the switch /mMailMergeTest from the command line. Does the mail merge execute?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, November 6, 2013 5:40 PM
    Moderator
  • Cindy, creating the AutoOpen macro will not work for me because we run four different macros based on the processing that we have to do before running the mail merge.

    This is the solution I came up with...

    'Add the following at the top of the module that contains the mail merge macro:
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    'Add the following to the mail merge macro before the ActiveDocument.MailMerge.Execute call:
    Dim cnt As Integer
    cnt = 10  'NOTE: should work with cnt = 1 but I made cnt bigger just in case a PC is slow in production
    Do While cnt > 0
      DoEvents
      Sleep (25)
      cnt = cnt - 1
    Loop

    'NOTE: I use signed Word Templates to bypass warning message about macros.  If you get the warning message because you are using an unsigned template then the mail merge will work because Word has time to open the document before running mail merge.

    'NOTE2: Added "ActiveDocument.ActiveWindow.View = wdPrintView" in a couple of places in my macros because of a Run-Time error '4506' caused by Word 2013 opening readonly documents in "reading" mode and the macros trying to change the document.

    • Marked as answer by Ron Clutter Thursday, November 7, 2013 8:12 PM
    Thursday, November 7, 2013 8:12 PM
  • Hi Ron

    Glad you found a solution, based on my thought. Thanks for sharing it here :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, November 8, 2013 7:34 PM
    Moderator
  • You might also be able to use something along the lines of:

    Public Sub MailMergeTest()
    Dim fileDirDataDoc As String
    fileDirDataDoc = "C:\TEMP\MailMergeData.DOC"
    With ActiveDocument.MailMerge
      .OpenDataSource fileDirDataDoc
      Do While .DataSource Is Nothing
      Loop
      .Destination = wdSendToNewDocument
      .Execute
    End With
    End Sub

    or:

    Public Sub MailMergeTest()
    Dim fileDirDataDoc As String
    fileDirDataDoc = "C:\TEMP\MailMergeData.DOC"
    With ActiveDocument.MailMerge
      .OpenDataSource fileDirDataDoc
      Do While .DataSource.RecordCount = 0
      Loop
      .Destination = wdSendToNewDocument
      .Execute
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, November 9, 2013 1:53 AM
  • We added the following registry entry to remove the "Opening this document will run the following SQL command" message:
    HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Word\Options > SQLSecurityCheck REG_DWORD 0x00000000

    This was the key for me.  All mailmege now opens with datasources already in the document. Many thanks

    Thursday, February 11, 2016 8:50 AM