Mailmerge doc will not open from Access 2003

U

ucmlamb

I am using Access 2003 to open a Mail Merge document using the following
VB code. The document uses a text file as the datasource. This worked
in Access 2000, but the document will not open in Access 2003, word
opens and the status bar displays "docname.doc: 1,222 characters..."
then the document never opens. If I remove the "WordDoc.Close (False)"
line the document will display, but not with the updated merge data from
the datasource, instead it opens with the saved data from the "template"
or original merge document. As I said this code works perfectly in
earlier versions of Access.

Function MergeWord(strDocName As String)
Dim WordApp As Object ' running instance of word
Dim WordDoc As Object ' one instance of a word doc
Dim strActiveDoc As String ' doc name (no path)
Dim lngWordDest As Long ' const for dest, 0 = new
doc, 1 = printer

On Error GoTo CreateWordApp
Set WordApp = GetObject(, "Word.Application")
On Error Resume Next

Set WordDoc = WordApp.Documents.Open(strDocName)

strActiveDoc = WordApp.ActiveDocument.Name

With WordDoc.MailMerge
..Destination = 0 ' 0 = new doc
..MailAsAttachment = False
..MailAddressFieldName = ""
..MailSubject = ""
..SuppressBlankLines = True
With .DataSource
..FirstRecord = 1
..LastRecord = 1
End With
..Execute Pause:=True
End With
WordDoc.Close (False)

WordApp.Visible = True
WordApp.Windows(WordApp.Windows.Count).Activate

AppActivate "Microsoft Word"
WordApp.WindowState = 0 'wdWindowStateRestore

Set WordApp = Nothing
Set WordDoc = Nothing

DoEvents
Exit Function

CreateWordApp:
...
End Function


Any help would be appreciated.
Thanks
ucmlamb
 
P

Peter Jamieson

I can't be sure, but I suspect you need to follow the instructions in this
Knowledgebase article:

"Opening This Will Run the Following SQL Command" Message When You Open a
Word Document"

at

http://support.microsoft.com?kbid=825765

However, it the data source being generated by Access just before you open
the Word document, or is it already there? If it has just been generated,
you may be encountering another problem where Access (or perhaps a virus
checker) still has the file locked. It may be worth trying to experiment
with a data source that has not /just/ been created to see if it makes any
difference.

Peter Jamieson
 
U

ucmlamb

Thanks for your reply. I made one change that fixed this.
1. I called "WordApp.ActiveDocument.MailMerge.OpenDataSource" after
opening the document.

Here is the code sample:
...
Set WordDoc = WordApp.Documents.Open(strDocName)

strActiveDoc = WordApp.ActiveDocument.Name
strPathName = WordApp.ActiveDocument.Path
strTempName = strPathName + "\" +
GetFileName_NoExtension(strActiveDoc) + ".888"

On Error GoTo ERROR_HANDLER

'Open the text file data source and process the mail merge
WordApp.ActiveDocument.MailMerge.OpenDataSource Name:= _
strTempName _
, ConfirmConversions:=False, ReadOnly:=False,
LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="",
Revert:=False, _
Format:=0, Connection:="", SQLStatement:="", SQLStatement1 _
:=""
With WordApp.ActiveDocument.MailMerge
..Destination = 0 ' 0 = new doc
..MailAsAttachment = False
..MailAddressFieldName = ""
..MailSubject = ""
..SuppressBlankLines = True
With .DataSource
..FirstRecord = 1
..LastRecord = 1
End With
..Execute Pause:=True
End With

WordDoc.Close (False)
...

Fortunately this change seems to also work for previous versions of
Access as well.

Thanks
ucmlamb
 

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