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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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