locked
Word Mail Merge Macro RRS feed

  • Question

  • Greetings,

    I have generated the below macro and running it. It's basically a mail merge macro which reads from a fixed file position and emails the document upon prompting the user. It works perfectly except for one small, critical issue. If one of the recipients is deselected, I get a run time error 5853 Invalid parameter and the below line is highlighted.

    ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord

    The full code block can be found below. Any help is appreciated alot.

    Sub MailMergeMacro()
    
    Dim Choice As Integer
    
        Response = MsgBox(prompt:="Select 'Yes' or 'No'.", Buttons:=vbYesNo)
        
            If Response = vbYes Then
            
            Application.Dialogs(wdDialogMailMergeRecipients).Show
    
                    ActiveDocument.Fields.Add Range:=Selection.Range, Type:= _
                        wdFieldAddressBlock, Text:= _
                        "\f ""<<_TITLE0_ >><<_FIRST0_>><< _LAST0_>><< _SUFFIX0_>>" & Chr(13) & "<<_COMPANY_" & Chr(13) & ">><<_STREET1_" & Chr(13) & ">><<_STREET2_" & Chr(13) & ">><<_CITY_" & Chr(13) & ">><<_STATE_" & Chr(13) & ">><<_POSTAL_>><<" & Chr(13) & "_COUNTRY_>>"" \l 2057 \c 2 \e ""Un"
                    Selection.TypeParagraph
                    Selection.TypeParagraph
                    ActiveDocument.Fields.Add Range:=Selection.Range, Type:= _
                        wdFieldGreetingLine, Text:= _
                        "\f ""<<_BEFORE_ Dear >><<_TITLE0_>><< _LAST0_>>" & Chr(10) & "<<_AFTER_ ,>>"" \l 2057 \e ""Dear Sir or Madam,"""
                    Selection.TypeParagraph
                    ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
                    With ActiveDocument.MailMerge
                        .Destination = wdSendToEmail
                        .SuppressBlankLines = True
                        With .DataSource
                            .FirstRecord = wdDefaultFirstRecord
                            .LastRecord = wdDefaultLastRecord
                        End With
                        .Execute Pause:=False
                    End With
                
                
                
                MsgBox "The document has been mailed"
                
            Else
            
                MsgBox "The document has not been mailed"
                
            End If
            
            
    End Sub
    

    Thanks again, Regards

    Tuesday, October 21, 2014 2:12 PM

Answers

  • Just a comment since I will not be able to follow up on this for a while, but checking/unchecking individual records could certainly cause problems with wdNextRecord, particularly if the first record or last record to be merged has been excluded by the user or programmatically. I do not know if this was ever fixed.

    A long time ago I found out that displaying the MailMerge recipients dialog seemed to work around at least one of the problems in this area - you may be able to do that via

    Dialogs(wdDialogMailMergeRecipients).Display TimeOut:=1



    Peter Jamieson

    Wednesday, October 22, 2014 8:46 AM

All replies

  • Hi,

    Since you need help with Macro, I suggest you post the question in Word for Developers forum to get further assistance:

    https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=worddev

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Regards,

    Melon Chen
    TechNet Community Support


    It's recommended to download and install Configuration Analyzer Tool (OffCAT), which is developed by Microsoft Support teams. Once the tool is installed, you can run it at any time to scan for hundreds of known issues in Office programs.

    Wednesday, October 22, 2014 1:47 AM
  • Just a comment since I will not be able to follow up on this for a while, but checking/unchecking individual records could certainly cause problems with wdNextRecord, particularly if the first record or last record to be merged has been excluded by the user or programmatically. I do not know if this was ever fixed.

    A long time ago I found out that displaying the MailMerge recipients dialog seemed to work around at least one of the problems in this area - you may be able to do that via

    Dialogs(wdDialogMailMergeRecipients).Display TimeOut:=1



    Peter Jamieson

    Wednesday, October 22, 2014 8:46 AM