Find full path address for workbook

JoeS01

Well-known Member
Joined
Jun 25, 2005
Messages
832
Hello, I am using VBA in Excel 2010, and have this workbook and an Access database mdb file in the one folder called workbooks.

To extract data from the database to the workbook, I need to know the full address of the folder ie like C:\Usder\ My Documents\workbooks....... The locatioon of the workbooks folder can change when I am using different computers, so need to do it automatically.

Can anyone please advise how to get the current path of the workbook's folder as a string variable?
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I have tried the following code, but the error meassge said : Invalid outside procedure. Can anyone please suggest the right was to get the address of a workbook folder and use it as a public string variable?

Public Const strDBFileName = "Tx_Master_Database.mdb"
Public strMasterDBPath As String
strMasterDBPath = ActiveWorkbook.Path <<<<< Error
 
Upvote 0
Tried another approach , still without success - another Compiler error. HELP!!

Public Const strDBFileName = "Tx_Master_Database.mdb"
Public strMasterDBPath As String
strMasterDBPath = FolderPath() <<< Invalid outside procedure

********************************************************
Public Function FolderPath() As String
' Returns the path of the current workbook
FolderPath = ThisWorkbook.Path
End Function
 
Last edited:
Upvote 0
I had a similar problem
Why don't u just make sure workbooks are in the dame folder and just put:
ThisWorkBook.Path

I wanted to know name of the destination to extract data from a closed workbook.
Code:
Workbook.Open("C:\users\Alan\documents\workfiles\example.")
Workbook.Open(ThisworkBook.Path & "\example.xls")

This works for me. Without using a function. :)
 
Upvote 0
I have tried the following code, but the error meassge said : Invalid outside procedure. Can anyone please suggest the right was to get the address of a workbook folder and use it as a public string variable?

Well the error you are getting is clearly telling what you are doing wrong. You are trying to execute the code line outside a procedure .

try somethig along these lines :

Code:
Public Const strDBFileName = "Tx_Master_Database.mdb"
Public strMasterDBPath As String                                   

Sub Test
    strMasterDBPath = ThisWorkbook.Path 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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