Solved

launch MS mail merge wizard via code

Posted on 2000-03-28
7
432 Views
Last Modified: 2012-08-13
I have an interface that lets users build an SQL string (via text boxes, list boxes, etc.) that is used to launch forms and reports. I want to use this string to launch the MS mail merge wizard (Tools/OfficeLinks/Merge it with MS Word) and have it use my SQL string or a query def.

The command to run the wizard seems to be:

DoCmd.RunCommand (acCmdWordMailMerge)

Two problems:
1. I can't get it to work
2. It doesn't have a parameter to specify the SQL or table name.  
Any other ideas?
0
Comment
Question by:loe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 14

Expert Comment

by:mgrattan
ID: 2674815
First, make sure you add the wizard file to your References -- open a module and click Tools, References.  Select wzmain80 from the list (if it's not there, Browse for the wzmain80.mda file in the Office directory).

Once the reference is added you can use the following function:

Function RunMerge(sSQL as String)
  PM_Entry(sSQL)
End Function

0
 

Author Comment

by:loe
ID: 2681705
This looks like a good first step to a solution. I found this file:  Wzmain80.mde which obviously contains a bunch of wizard stuff, but did not find a file with the extension .mda.  Nevertheless, once I added the Wzmain80.mde file to my references I got the function PM_Entry() to work. However, it appears to need a query or table as an argument. Strings don't work.

A couple of questions:
1. Have you gotten it to work using an SQL string? Does the .mda file allow you to use a string? I suppose I could switch to using a querydef, but I'd rather use my code as-is if I can.
2. How did you discover this function? Is there a directory of libraries and their functions out there somewhere? I never would have known to look there if you hadn't suggested it.

Thanks. I look forward to your response.
0
 
LVL 14

Accepted Solution

by:
mgrattan earned 100 total points
ID: 2681731
Forgive me for not testing!  Looks like I need to take a little more time with my answers.  Anyway, a few points:

1.  It will not work with an SQL string as you have discovered.  However, there are ways around this (see routine below).
2.  It is an MDE file (it used to be an MDA file in 2.0 I think.)
3.  The Wizard functions are outlined in several references; I found this information in the Access 97 Developers Handbook by Litwin/Getz.

Sub RunMailMerge(sSQL As String)
Dim db As Database
Dim qdf As QueryDef

Set db = CurrentDb
Set qdf = db.CreateQueryDef("TempQuery", sSQL)

PM_Entry (qdf.Name)

For each qdf in db.querydefs
    if qdf.name = "TempQuery" then
    DoCmd.DeleteObject acQuery, "TempQuery"
    Exit For
Next qdf
End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:loe
ID: 2701584
Your FOR loop needed a little fine-tuning, but the concept was sound. Here's the final code I used:

Sub RunMailMerge(sSQL As String)
  Dim db As Database
  Dim qdf As QueryDef

  Set db = CurrentDb
  Set qdf = db.CreateQueryDef("TempQuery", sSQL)

  PM_Entry (qdf.Name)

  For Each qdf In db.QueryDefs
      If qdf.Name = "TempQuery" Then
          DoCmd.DeleteObject acQuery, "TempQuery"
      End If
  Next qdf

End Sub

Thanks for your help!
0
 
LVL 14

Expert Comment

by:mgrattan
ID: 2704225
loe,

The only thing you changed in my For loop was to remove the Exit For line.  Actually, it's better if you leave it in.  That way the routine will be exited as soon as the temp query is found and deleted and you won't be looping through the entire QueryDefs collection.
0
 

Author Comment

by:loe
ID: 2704476
I think this was what you had in mind:

For Each qdf In db.QueryDefs
    If qdf.Name = "TempQuery" Then
        DoCmd.DeleteObject acQuery, "TempQuery"
        Exit For
    End If
Next qdf

Your code was missing an End If statement, so it didn't compile when I tried to run it.

Good point about the Exit For statement. I was not familiar with it, so when I was troubleshooting the error I thought perhaps you had intended to put the End If statement there. I'll plug it back into my routine.

Thanks again for your help, my end-user is very happy with the new feature!
0
 
LVL 14

Expert Comment

by:mgrattan
ID: 2704547
Glad to hear everything's working.  Sorry about the missing End If.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question