open text function in VB macro after double click

  • Thread starter Thread starter Kengco
  • Start date Start date
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?
 
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....
 
Back
Top