Mail Merge Woes RRS feed

  • Question

  • I have a Word VSTO UC that passes data to mail merge. Something along the lines of:

    string location = "C:\Temp\Resources\MailMerge\test.odc"
    Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;
    object sqlquery = "SELECT, c.address FROM VwMailMerge c " + "WHERE X=Y"
    object omissing = System.Type.Missing;
    object dsrevert = false;

    document.MailMerge.OpenDataSource(location, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref dsrevert, ref omissing, ref omissing, ref omissing, ref sqlquery, ref omissing, ref omissing, ref omissing);

    This works fine. The problem I have is that - if the user edits the recipient list in Word 2010 and sorts the list or applies a further filter; my original query filters are removed. In the example above if you choose to sort the recipient list it shows all the results from

    SELECT, c.address FROM VwMailMerge c

    and removes my "WHERE X=Y".

    Is there a way to instruct Word to retain the original source or is there a better way of formulating the connection so as to prevent this from happening?
    • Edited by maphisto Wednesday, November 30, 2011 10:18 AM formatting error
    Wednesday, November 30, 2011 10:17 AM


  • Hi maphisto

    The sort answer is, NO.

    The longer answer is, if you want to allow the user such capability you'd have to either

    1. Create an interface for this (and disable the built-in one) so that you keep control; or

    2. Disable the built-in command the executes the merge, replace it with your own, and in that analyze the current query string, integrate that into your query, then set the complete query string before executing.

    Cindy Meister, VSTO/Word MVP
    Wednesday, November 30, 2011 2:49 PM