VBA Code Query - fixed path to relative path

creativespaace

New Member
Joined
Oct 25, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello All,

If someone could help please as I am looking to change fixed path to relative path in following code

Below Macro code is in word document and this is linked to excel file to get data in the same folder. I usually copy entire folder to new location and looking for a code where it will pick excel file from same folder (08.Documents) where word and excel files are.

Name of all files folders are kept same and only thing changes is folder name as shown below in red.

Appreciate if someone could guide with some magic. Thank You

Current code for reference.

Sub test2()
'
' test2 Macro
'
'
ActiveDocument.MailMerge.OpenDataSource Name:= _
"C:\Users\04a.PWLive\000.Blank Project - Copy\08.Documents\PWMailMerge.xlsx" _
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:= _
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=C:\Users\04a.PWLive\000.Blank Project - Copy\08.Documents\PWMailMerge.xlsx;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System data" _
, SQLStatement:="SELECT * FROM `PWData$`", SQLStatement1:="", SubType:= _
wdMergeSubTypeAccess
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Untested:
VBA Code:
ActiveDocument.MailMerge.OpenDataSource Name:= _
ActiveDocument.Path & "\PWMailMerge.xlsx" _
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:= _
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & ActiveDocument.Path & "\PWMailMerge.xlsx;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System data" _
, SQLStatement:="SELECT * FROM `PWData$`", SQLStatement1:="", SubType:= _
wdMergeSubTypeAccess
 
Upvote 0
Solution

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top