Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1386
  • Last Modified:

Launching a publisher document from access using mail merge and VBA

I am trying to do  a publisher mail merge from VBA code in the access db holding the merge info. I have done this successfully into Word, but cannot get publisher to work. I am receiving a 432 error (file or class not found). Here is the code:
Dim appPub As Publisher.Application
    Set appPub = New Publisher.Application
    Dim objPub As Publisher.Document
   
    Set objPub = GetObject(CurrentProject.Path & "\secondcontact.pub", "publisher.document")
    objPub.MailMerge.OpenDataSource _
        bstrDataSource:=CurrentDb.Name, _
        bstrTable:="tempSecondContact", _
        fNeverprompt:=True, fopenexclusive:=True
       
    objPub.MailMerge.Execute True
0
dfisher6
Asked:
dfisher6
  • 3
  • 3
1 Solution
 
pteranodon72Commented:
your line:
Set objPub = GetObject(CurrentProject.Path & "\secondcontact.pub", "publisher.document")

looks really suspicious.  You only need GetObject if you want to avoid having a reference to Publisher and use late binding.

Since you create a new instance of Publisher, use the Open method of the object to set the document.

Set objPub = appPub.Open(CurrentProject.Path & "\secondcontact.pub")

The rest should follow OK.
---
For getObject to work, the document would have to already be open.  You could use CreateOjbect, but you've already got the app, use its methods instead.

HTH,

pT
0
 
dfisher6Author Commented:
Thank you! That worked - it merged directly to a printer. Can it merge to print preview (as I ususally do in word)?
0
 
pteranodon72Commented:
It's not really quite a Print Preview but why not replace your .Execute line with

Dim docResult as Publisher.Document
Set docResult =  objPub.MailMerge.Execute(True, pbMergeToNewPublication)

This executes the merge into a new document, which you can look at and decide to save or not.

If you go into the Access module code, press F2 to see the object browser. Choose the publisher library and then you'll be able to see all of the methods of the Application, Document, and MailMerge objects to pick up their parameter possibilities.

HTH,

pt
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
dfisher6Author Commented:
Thank you again - one more question? It merges each record into a postcard twice. I haven't found any parameter with number of copies. Any ideas on that?
0
 
pteranodon72Commented:
The only thing I can think of is that some postcard templates are two pages, so the merged result should be 2 * #records pages.  Or, if you're using pbMergeToExistingPublication, you'll append new  pages every time each time you execute.  Last resort -- check you number of records in your datasource!

HTH,

pT
0
 
dfisher6Author Commented:
Thanks for the help - none of the suggestions was the case for double pages, but as long as they are able to open in preview mode, they can just print the ones needed.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now