Open file without calling Filepath

I am using excel VBA. I want to press a button that opens another file directly without the effect of "selecting a file window".

This is the current code:

Sub loadFile_click() Workbooks.Open("C:\Users\GIL\Desktop\ObsReportExcelWorkbook.xlsx") End Sub 

In this case, the file is in the same folder as the main file.

Is there any way to do this without entering a file path?

+6
source share
5 answers

If the file name is corrected, you can use the ActiveWorkbook.Path function to return the path to the current workbook:

Dim FileName as string FileName = ActiveWorkbook.Path & "\myfile.xlsx

Check if you need an extra slash character.

+20
source

If the file is in the same folder as the document containing your VBA macro, use

 ThisWorkbook.Path 

eg:

 Workbooks.Open(ThisWorkbook.Path & "\ObsReportExcelWorkbook.xlsx") 

(this works even if the document is not active, or you have changed the current directory).

+9
source

If it is in the same folder, then

 Workbooks.Open("ObsReportExcelWorkbook.xlsx") 

or

 Workbooks.Open(".\ObsReportExcelWorkbook.xlsx") 

must work. I just tried both in Excel VBA with success.

+2
source

Workbooks.Open ("D: \ data \ Mohit Singh \ msk \ data \ book1.xlsx")

0
source

Open ActiveWorkBook folder

 Sub OpenFolderofProject() Dim strMsg As String, strTitle As String Dim sFolder As String sFolder = ThisWorkbook.Path strMsg = "Open WorkBook Folder Location? " strTitle = "Folder Location:" & sFolder If MsgBox(strMsg, vbQuestion + vbYesNo, strTitle) = vbNo Then Else Call Shell("explorer.exe " & sFolder, vbNormalFocus) End If End Sub 

Context: Usually, your changes in your project are constantly changing or you have an automatically created workbook with a dynamic name. No matter what happens. This will know the location of your path to the book and ask if you want to open this particular folder.

This was very useful for me when I dynamically saved a bunch of Excels programmatically in the same folder of a workbook. Because instead of closing / minimizing the workbook, go to Explorer. I could focus on the project and not lose my mind.

-1
source

All Articles