Ralative path for data source

S

SHKDXB

hi anybody can help me to make the path of the data source as relative?. when
trnasporting the files to other machines, i have problem of missisng data
soure - due to absoulte path in the main mail merge documnet. is there a way
to change this data source to relative path??
 
P

Peter Jamieson

is there a way
to change this data source to relative path??

Not easily - you have to use code, e.g. Word VBA.

First, before you distribute the solution, ensure that the Mail Merge Main
Document has been disconnected from its data source. You have to do that
because nothing you do in VBA can change the data source path /before/ Word
tries to open it.

You can disconnect the data source while the mail merge main document is
open by using

ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument

in the Word VBA Editor Immediate WIndow.

Then, in Word VBA, create an ordinary module in your Document and put a
routine such as the following in it - this assumes that you know the name of
the .xls and that it will be in the same folder as the .doc

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

' set this to be the file name of your data source
strDataSource = "myexcelfile.xls"

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

' set this to be the query for your data source
' if you need to sort aor filter, you need to add
' the appropriate ORDER BY and WHERE clauses
' strQuery = "SELECT * FROM [myrangename]"

With ActiveDocument
strDataSource = .Path & "\" & strDataSource
With .MailMerge
.OpenDataSource _
Name:=strDataSource, _
SQLStatement:=str
' 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

Unfortunately, you, or your user, will probably also have to take account of
the following artiicle:

http://support.microsoft.com/kb/825765/en-us
 
S

SHKDXB

Thanks for the info
couldn't understsnad the line

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

tried but getting error as "compile error argument not optional" str
highlighted in statemnet SQLStatement:=Str
any help?


Peter Jamieson said:
is there a way
to change this data source to relative path??

Not easily - you have to use code, e.g. Word VBA.

First, before you distribute the solution, ensure that the Mail Merge Main
Document has been disconnected from its data source. You have to do that
because nothing you do in VBA can change the data source path /before/ Word
tries to open it.

You can disconnect the data source while the mail merge main document is
open by using

ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument

in the Word VBA Editor Immediate WIndow.

Then, in Word VBA, create an ordinary module in your Document and put a
routine such as the following in it - this assumes that you know the name of
the .xls and that it will be in the same folder as the .doc

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

' set this to be the file name of your data source
strDataSource = "myexcelfile.xls"

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

' set this to be the query for your data source
' if you need to sort aor filter, you need to add
' the appropriate ORDER BY and WHERE clauses
' strQuery = "SELECT * FROM [myrangename]"

With ActiveDocument
strDataSource = .Path & "\" & strDataSource
With .MailMerge
.OpenDataSource _
Name:=strDataSource, _
SQLStatement:=str
' 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

Unfortunately, you, or your user, will probably also have to take account of
the following artiicle:

http://support.microsoft.com/kb/825765/en-us

--
Peter Jamieson
http://tips.pjmsn.me.uk

SHKDXB said:
hi anybody can help me to make the path of the data source as relative?.
when
trnasporting the files to other machines, i have problem of missisng data
soure - due to absoulte path in the main mail merge documnet. is there a
way
to change this data source to relative path??
 
P

Peter Jamieson

Yes, there are some errors in the code. What you need depends partly on the
data source, but...

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

' set this to be the query for your data source
' if you need to sort or filter, you need to add
' the appropriate ORDER BY and WHERE clauses
strQuery = "SELECT * FROM [myrangename]"

With ActiveDocument
strDataSource = .Path & "\" & strDataSource
With .MailMerge
.OpenDataSource _
Name:=strDataSource, _
Connection:=strConnection, _
SQLStatement:=strQuery

and so on...


--
Peter Jamieson
http://tips.pjmsn.me.uk

SHKDXB said:
Thanks for the info
couldn't understsnad the line

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

tried but getting error as "compile error argument not optional" str
highlighted in statemnet SQLStatement:=Str
any help?


Peter Jamieson said:
is there a way
to change this data source to relative path??

Not easily - you have to use code, e.g. Word VBA.

First, before you distribute the solution, ensure that the Mail Merge
Main
Document has been disconnected from its data source. You have to do that
because nothing you do in VBA can change the data source path /before/
Word
tries to open it.

You can disconnect the data source while the mail merge main document is
open by using

ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument

in the Word VBA Editor Immediate WIndow.

Then, in Word VBA, create an ordinary module in your Document and put a
routine such as the following in it - this assumes that you know the name
of
the .xls and that it will be in the same folder as the .doc

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

' set this to be the file name of your data source
strDataSource = "myexcelfile.xls"

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

' set this to be the query for your data source
' if you need to sort aor filter, you need to add
' the appropriate ORDER BY and WHERE clauses
' strQuery = "SELECT * FROM [myrangename]"

With ActiveDocument
strDataSource = .Path & "\" & strDataSource
With .MailMerge
.OpenDataSource _
Name:=strDataSource, _
SQLStatement:=str
' 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

Unfortunately, you, or your user, will probably also have to take account
of
the following artiicle:

http://support.microsoft.com/kb/825765/en-us

--
Peter Jamieson
http://tips.pjmsn.me.uk

SHKDXB said:
hi anybody can help me to make the path of the data source as
relative?.
when
trnasporting the files to other machines, i have problem of missisng
data
soure - due to absoulte path in the main mail merge documnet. is there
a
way
to change this data source to relative path??
 

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