Word 2003 (was 2000) Mailmerge Failure in Access 2003 VBA

M

ML

The following code has worked perfectly (it was derived from macros) until
Word was upgraded to 2003.

Set oApp = CreateObject("Word.Application")

' With oApp
' .Visible = True
' .Activate
' .WindowState = wdWindowStateMaximize
' End With

oApp.ChangeFileOpenDirectory SourceDocsDirectory
oApp.Documents.Open Filename:="A_Word_Merge_Doc.doc",
ConfirmConversions:=False, ReadOnly:= _
True, AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:= _
"", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto


With oApp.Documents(1).MailMerge
.Destination = wdSendToNewDocument
.Execute
.MainDocumentType = wdNotAMergeDocument
End With

The doc, called A_Word_Merge_Doc.doc here, was set up using Word 2000 mail
merger to query a table in an Access mdb.
With Word 2003, it now appears to lose track that it is a "merge" doc, and
fails with a 5852 Requested Object is not available"
at the (mailmerge) ,destination = wdSendTonewDocument statement.

I have tried recreating the "merge" doc, both as a Jet and as an ODBC
connected doc.

I still can't get a Word 2003 mailmerge doc, to open in and Access VBA set
of statements.

Can anyone help

TIA
Michael
 
G

Geoff

Michael,

I don't know if this will answer your problem, but it might give you a lead.

I think that, from Office 2002 onwards, you must call the OpenDataSource
method of the MailMerge object. (You didn't have to do that before. You
could rely on the main document pointing to the data source.) You should
also check the State property of the Mailmerge object before executing the
merge (see example below).

I usually follow the advice in the "Access Cookbook" by Getz, Litwin and
Baron and first export the data to be merged from the Access database to an
RTF file using:

DoCmd.OutputTo acOutputQuery, MyQueryName, _
acFormatRTF, strDataPathName

(And, before I export the data, I normally open a recordset on the query
and check that it actually returns records. If the query doesn't return
records, then there's no point in proceeding with the merge.)

So...with strDataPathName pointing to the RTF file that you know contains
records, you could write:

With oApp.Documents(1).MailMerge
.OpenDataSource strDataPathName
' Check State before setting any more properties:
If .State <> wdMainAndDataSource Then GoTo Err_CantProceed
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
.MainDocumentType = wdFormLetters
' For good measure, check state again before executing merge:
If .State = wdMainAndDataSource Then
.Execute
Else
GoTo Err_Houston_We_Have_A_Problem
End if
'.MainDocumentType = wdNotAMergeDocument
End With

One last tip, I'd leave your code in to make Word visible before executing
the merge, even if you don't activate Word. If you make a mistake with your
main document (eg include an invalid field), Word may display a dialog so
you can correct the problem. If you can't see the dialog because Word is
invisible, you'll think Access/Word has frozen.

If that doesn't do it, then sorry, I don't know.
Good luck!
Geoff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top