views:

114

answers:

2

Hi, I'm not sure why my code is not working.. I have code in vba in Access 03 which opens a word document which runs a mail merge process. When the code runs, it asks me what the Header Delimiters are. I specified those in the export file as a comma for a field delimiter and as the record delimiter. After I confirm this in word, I get a a run time error.

The error I'm getting is: Run-time error '5922': Word was unable to open the data source.

strExportFullyQualifiedName is a text file which is exported with the field names listed in there which is being used as a data source file.

I checked the links and verify, everything exists. The connection is not being made. strExportFullyQualifiedName is the location of the txt file.

 With objWordDoc
        .MailMerge.OpenDataSource _
          Name:=strExportFullyQualifiedName, Format:=wdOpenFormatAuto, _
          ConfirmConversions:=False, ReadOnly:=False, SubType:=wdMergeSubTypeAccess, AddToRecentFiles:=False
        .MailMerge.Destination = wdSendToNewDocument
        .MailMerge.MainDocumentType = wdDirectory
        .MailMerge.SuppressBlankLines = True
    End With

Thanks !

+1  A: 

Do you have a reference to the Word Library (Code Window->Tools->References)? If not, you will need to use the values for built-in constants such as wdOpenFormatAuto

EDIT re Comment

As you have a text file, I do not believe that your types are correct. Try something like:

''To create output, if required
''DoCmd.TransferText acExportDelim, , "qryMailMerge", strExportFullyQualifiedName, True

 With objWordDoc
     .MailMerge.OpenDataSource _
      Name:=strExportFullyQualifiedName, Format:=wdOpenFormatText
     .MailMerge.Destination = wdSendToNewDocument
     .MailMerge.MainDocumentType = wdDirectory
     .MailMerge.SuppressBlankLines = True
 End With
Remou
Yes I do. It's: Microsoft Word 11.0 Object Library
JK0124
+1  A: 
  1. You said twice that strExportFullyQualifiedName is the location of a text file. I've never automated this process before, but I thought you could only export from a Word file.

  2. Does strExportFullyQualifiedName contain both the filepath & filename? e.g. "C:\Test.Doc"?

PowerUser
@PowerUser: the context in which the code is running is Access, not Word, no?
David-W-Fenton
Correct me if I'm wrong, but I don't think it matters here what the code is running in. Mail Merge is a function in MS Word, which as far as I know, can only work with Word files. So, his code should reference a Word file as his source.
PowerUser
Word mail merge can use any number of data sources, text files, Excel files, Access MDBs, etc. It doesn't need a Word file as the data *source*.
David-W-Fenton
I was able to get this to work, by recording the connection macro from Word, and editing some stuff out and putting it back in Access, and it worked!
JK0124
Problem solved then!
PowerUser