+ Reply to Thread
Results 1 to 4 of 4

Mail merge - edit recipients list using macro

  1. #1
    Registered User
    Join Date
    03-19-2013
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10

    Mail merge - edit recipients list using macro

    I have a mailmerge document and have tried to record a macro doing the following in word 2007

    1. Select Mailings tab
    2. Click Edit Recipitent list
    3. Click filter
    4. Change a value on one of my existing field filters which is from email to print.

    Unfortunately when I use the record macro and do these steps nothing is being recorded?

    Be grateful for some advice or how I can add this into my VBA code
    Last edited by PAUL41EXCEL; 08-09-2015 at 06:23 AM.

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Mail merge - edit recipients list using macro

    There's a lot you can do in Office via dialogues for which there's no VBA equivalent or, even if there is, there's no VBA recording mechanism. In this case, there's no VBA equivalent.

    What you're trying to do is not to edit the data source, but to apply a filter. That's ordinarily done via defining an appropriate query string, which you can do via the:
    • MailMerge.OpenDataSource Method; or the
    • MailMergeDataSource.QueryString Property
    Using either of these entails writing your own SQL statement. An alternative is to use the:
    • MailMergeDataSource.Included Property
    which you can use inside an If test, for example, so that you don't need to understand SQL. Yet another alternative is to use a SKIPIF field in the mailmerge main document itself. For example, a SKIPIF field coded as per the following text representation prompts for the record range to process, based on a simple record count. With this coding, the user will be prompted to supply the first and last record #s to merge. Any records outside this range will be excluded.
    {SKIPIF{=({MERGEREC}< {FILLIN "First Record #" \d 1 \o})+({MERGEREC}> {FILLIN "Last Record #" \d 999 \o})}= 1}

    Similarly, a SKIPIF field coded as per the following prompts for the record range to process, based on dates in a ‘DbDate’ field in the source data. With this coding, the user will be prompted to supply the first and last dates to merge. Any records outside this date range will be excluded.
    {SKIPIF{={IF{MERGEFIELD DbDate \@ YYYYMMDD}<{FILLIN "First Date to include." \@ YYYYMMDD \o} 1 0}*{IF{MERGEFIELD DbDate \@ YYYYMMDD}>{FILLIN "Last Date to include." \@ YYYYMMDD \o} 1 0}}= 1}

    A SKIPIF field coded as per the following first prompts for the item to include/exclude, then solicits input for the inclusion/exlusion rule.
    {SKIPIF{IF{FILLIN "Contract to include/exclude." \o }={MERGEFIELD CONTRACT} 0 1}= {FILLIN "1: include. 0: exclude." \o}}

    In a generalised sense, the SKIPIF field can be used to apply AND and OR filters for multiple conditions, using coding like:
    (AND) - {SKIPIF{={IF{MERGEFIELD Field1}= Y 1 0}*{IF{MERGEFIELD Field2}= "" 1 0}}= 1}
    (OR) - {SKIPIF{={IF{MERGEFIELD Field1}= Y 1 0}+{IF{MERGEFIELD Field2}= "" 1 0}}> 0}
    (AND) - {SKIPIF{={IF{MERGEFIELD Field1}= Y 1 0}*{IF{MERGEFIELD Field2}= "" 1 0}*{IF{MERGEFIELD Field3}= "" 1 0}}= 1}
    (OR) - {SKIPIF{={IF{MERGEFIELD Field1}= Y 1 0}+{IF{MERGEFIELD Field2}= "" 1 0}+{IF{MERGEFIELD Field3}= "" 1 0}}> 0}

    Note: The field brace pairs (i.e. '{ }') for the above examples are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac); you can't simply type them or copy & paste them from this message. Nor is it practicable to add them via any of the standard Word dialogues.
    Last edited by macropod; 08-09-2015 at 06:39 AM.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    03-19-2013
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    10
    Thanks for the reply. Does this mean I cannot have this as part of my macro.

    My end goal is to have one button which will print all documents and second button that will email documents.

    I do know SQL so would be happy approaching this route, the original data source is in Excel format though and not an oracle or SQL database. Would the SQL to filter records be called from my macro? Ideally I would not want a prompt appearing but for the user to click a different button based on the filter option of print or email.
    Have you got any idea were is should start using SQL method.

    Thanks again for your help and reply

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Mail merge - edit recipients list using macro

    No, it just means you have to take a different approach. You could, for example, use an InputBox to provide the SQL statement (good luck typing that in correctly every time you run the macro) or you could programmatically add the necessary SKIPIF field code to the mailmerge main document, so the user gets prompted for the appropriate filter criteria when the merge is executed.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Mail Merge - Merge fields lister macro
    By adil.master in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-26-2015, 10:33 AM
  2. Replies: 0
    Last Post: 01-05-2015, 07:35 AM
  3. Loop to find E-mail recipients from list with duplicates
    By d_max_c in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-17-2014, 09:44 AM
  4. Macro to email HTML formatted mail merge from list in excel
    By jamesshakedown in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-19-2013, 10:14 AM
  5. Word 2007 Mail Merge allows to send one message to multi recipients
    By wowow in forum Word Programming / VBA / Macros
    Replies: 3
    Last Post: 12-22-2011, 08:22 AM
  6. Replies: 1
    Last Post: 01-28-2009, 10:25 AM
  7. [SOLVED] Can't edit or Refresh under mail merge recipients
    By Jennifer Mcdermeit in forum Excel General
    Replies: 1
    Last Post: 07-25-2006, 09:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1