Macro to open a file

J

jackrobyn1

I have a simple macro in a workbook named "daily info" that activates when i
click a button, the macro opens a workbook called "Phonebook" both of these
workbooks are contained in the same folder called "my reports". What i need
is a macro that will look inside the folder called "my reports" even if i
move this folder to another place on my computer. In effect only looking
inside the immediate folder. I hope this makes sense.
Thanks for any help
 
S

Sheeloo

Use thisworkbook.Path to get the path of the workbook containing the macro
and use it to open the desired file.
 
J

jackrobyn1

As I recorded the macro with the macro recorder im not sure what to do with
the information you have given me, can you explain or give an example
Thanks
 
D

Dave Peterson

Are you looking in that folder for the file named PhoneBook.xls?

Dim PBWkbk as workbook
Dim PBName as string

PBName = "phonebook.xls"

set pbwkbk = nothing
on error resume next
set pbwkbk = workbooks.open(filename:=thisworkbook.path & "\" & pbname
on error goto 0

if pbwkbk is nothing then
msgbox pbname & " wasn't found in " & thisworkbook.path
else
msgbox "It opened!
end if
 
J

jackrobyn1

Great thanks

Dave Peterson said:
Are you looking in that folder for the file named PhoneBook.xls?

Dim PBWkbk as workbook
Dim PBName as string

PBName = "phonebook.xls"

set pbwkbk = nothing
on error resume next
set pbwkbk = workbooks.open(filename:=thisworkbook.path & "\" & pbname
on error goto 0

if pbwkbk is nothing then
msgbox pbname & " wasn't found in " & thisworkbook.path
else
msgbox "It opened!
end if
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top