PDA

View Full Version : [SOLVED:] Open a Workbook with path



Cinema
12-06-2015, 08:16 AM
Hello,


I want to open a workbook with path which is stated in the Range B1 of the Active Workbook. My code gives me the Runtime error 1004 and says that the Workbook cannot be found although the name is correct.

Here is my code:

Sub OpenReport()
Dim path As String
path = ActiveWorkbook.Worksheets("Sheet1").Range("B1").Value
Dim name As String
name = ActiveWorkbook.Worksheets("Sheet1").Range("B2").Value
Workbooks.Open Filename := path & name
End Sub


in B1 there is the path : C:\Users\BenutzerXY\Desktop\
in B2: Map1.xls

jolivanes
12-06-2015, 10:20 AM
Does it work if you change B1 to "C:\Documents and Settings\Owner\Desktop\"?
The "BenutzerXY" is the German person that owns the machine I assume?


This should work also. You don't have to put the path to the desktop in. This code will do that.
It only works of course if the file is on the desktop.



Sub OpenReport()
Dim path As String
Dim name As String
Application.ScreenUpdating = False
Range("B1").Value = CreateObject("WScript.Shell").specialfolders("Desktop") & "\"
path = ActiveWorkbook.Worksheets("Sheet1").Range("B1").Value
name = ActiveWorkbook.Worksheets("Sheet1").Range("B2").Value
Workbooks.Open Filename:=path & name
Application.ScreenUpdating = True
End Sub

Cinema
12-06-2015, 11:11 AM
Hi jolivanes,

thank you for your help. What can I do if my file is not on the desktop?
"The "BenutzerXY" is the German person that owns the machine I assume"?" --> yes

jolivanes
12-06-2015, 11:29 AM
Re: "What can I do if my file is not on the desktop?"
All you can do there is making sure that the path is right and that the backslash is added. Copy it from the Explorer address bar maybe.
Or search for the file with

Application.GetOpenFilename()
You also have to leave the following line out

Range("B1").Value = CreateObject("WScript.Shell").specialfolders("Desktop") & "\"

Cinema
12-06-2015, 11:57 AM
Thank you :D