How do I connect to a mail merge data base using a relative path?

P

Peter Jamieson

As far as I know you cannot do this without code, e.g. VBA code, because
a. Word always stores the pathnames of data sources as absolute path names
b. in any case, Word treats relative path names as relative to the /active/
folder, which is not necessarily the folder containing the mail merge main
document.

To use VBA to fix the path, you really have to do at least two things:
c. make sure that the Mail Merge Main Document is never saved with any data
source information - otherwise, when you re-open it, the first thing that
Word does is to try to locate the existing data source. You can't write a
macro that intercepts Word before this point and you can't really guarantee
to avoid the dialog boxes that will appear if the data source is not found.
d. use an AutoOpen macro to connect to the new data source

To disconnect a datasource from its datasource before saving it, you can use

ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument

You will lose the source name, connection information and any sort/filter
selections, but not any of the fields in the document.

Here's a sample AutoOpen for you:

Sub AutoOpen()
Dim strDataSource As String
Dim strConnection As String
Dim strQuery As String

' this is a simple example for a data source which is a Word document
' set this to be the file name of your data source
strDataSource = "kt.doc"

' set this to be the connection string for your data source
'strConnection = ""

' set this to be the query for your data source
' strQuery = ""

With ActiveDocument
strDataSource = .Path & "\" & strDataSource
' for a Word document as data source, we just need the path name
With .MailMerge
.OpenDataSource _
Name:=strDataSource
' use the type you need
.MainDocumentType = wdFormLetters
' use the destination you need
.Destination = wdSendToNewDocument

' NB the above code does not execute the merge.
End With
End With
End Sub

When connecting programmatically, you may also have to implement the
registry change described in

http://support.microsoft.com/kb/825765

Peter Jamieson
 

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