open text function in VB macro after double click

K

Kengco

I have a macro that opens a text file using open.text
function.
The text file is in the same directory.
The macro will only find the text file if the main excel
file is originally opened from excel. If the file is
opened by double clicking the main filename then the macro
cannot find the file.
If I give the macro a discrete location for the file
specifying the drive and subdirectories then it can find
it but I want the macro to work in different locations.
Any help?
 
D

Dave Peterson

Maybe you can use:

dim myFilename as variant
myfilename = application.getopenfilename("Text files, *.txt")
if myfilename = false then
exit sub 'user hit cancel
end if

Workbooks.OpenText Filename:=myfilename, ....rest of your code....
 

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