Results 1 to 3 of 3
  1. #1
    Ganymede is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Dec 2011
    Posts
    64

    VBA Code for Mail Merge via Dynamic Data Exchange

    I'm trying to write a code that opens a word document template (from my database) and enters criteria from a query (within my database) into the template. Here's my current code:


    Code:
       Dim objWord As Word.Document
       Set objWord = GetObject("S:\(My Name)\Affidavit.doc", "Word.Document")
       ' Make Word visible.
       objWord.Application.Visible = True
       ' Set the mail merge data source as the Northwind database.
       objWord.MailMerge.OpenDataSource _
          Name:="S:\(My Name)\Database19.accdb", _
          LinkToSource:=True, _
          Connection:="QUERY ClaimantList", _
          SQLStatement:="SELECT * FROM [ClaimantList]"
       ' Execute the mail merge.
       objWord.MailMerge.Execute
    I cannot get this code to properly mail-merge my claimant list. Every time I run the macro, it opens word and prompts me with this:


    I tested the code with other inquiries and tables and it worked perfectly. However, I think I have discovered why.

    1.) The queries that worked with this code did not have anything in the "criteria field". The query that I'd like to use does have a criteria field. It is triggered by the value of a certain text box within a form.

    2.) I believe that having criteria in a query means it cannot be read as an OLE (Object linking and embedding) data source.

    When I attempt to manually mail merge from the word document and I select OLE Database files (I have confirm data source on), I get a list of every table AND every inquiry that does not contain anything in the criteria field.


    However, when I select "Show All" at the "Confirm Data Source" prompt and select "MS Access Databases via DEE (*.mdb; *.mde), I get a list that includes all queries, including the one that contains criteria.



    This leads me to believe that I may be able to write a working code to mail merge the contents of my query IF I can find a way to express it as a DEE source rather than a OLE source. I'm looking for how I might change the code for this effect.

  2. #2
    SteveF is offline Generally AccessAble
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2010
    Location
    Fourth Corner
    Posts
    123
    This is going back years for me, but as I recall I had my best success using the following formula:

    Export the results of your query to an external data file, e.g. CSV.
    Then use your Access code to point the Word doc at the external file for its merge data and execute the merge.

    These steps can all be done in code, of course.

  3. #3
    Ganymede is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Dec 2011
    Posts
    64
    Quote Originally Posted by SteveF View Post
    This is going back years for me, but as I recall I had my best success using the following formula:

    Export the results of your query to an external data file, e.g. CSV.
    Then use your Access code to point the Word doc at the external file for its merge data and execute the merge.

    These steps can all be done in code, of course.
    Thank you. I'll have to try that out.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Mail Merge
    By sakhtar in forum Access
    Replies: 8
    Last Post: 09-20-2020, 09:10 AM
  2. Mail Merge
    By DCV0204 in forum Forms
    Replies: 6
    Last Post: 12-13-2011, 09:32 AM
  3. Mail Merge data source queries missing
    By UTS in forum Queries
    Replies: 6
    Last Post: 09-21-2011, 01:48 AM
  4. Mail Merge w/ data from 2 tables
    By sedain121 in forum Import/Export Data
    Replies: 3
    Last Post: 06-30-2010, 09:43 AM
  5. Mail merge
    By grgerhard in forum Forms
    Replies: 0
    Last Post: 04-25-2006, 05:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums