filedialog in conjunction with printing to a text file

  • Thread starter Thread starter Geoff C
  • Start date Start date
G

Geoff C

Hi I'm trying to adapt a macro that "saves" a spreadsheet as pipe-delimited
text. The macro I've found on the web does what it says on the tin, but has a
hard-coded file name,

Open "C:data.txt" For Output As #1

and at a certain point in the macro, it use the line

Print #1, CurrTextStr

to save the data. I was hoping to find a way to get the user to detemine the
output path/filename rather than having it hard-coded, but I just don't
understand the filedialog syntax well enough to make it work. Can anyone
help with this?

Many thanks,
Geoff.
 
Dim myFileName As Variant

myFileName = Application.GetSaveAsFilename _
(InitialFileName:="C:\myfile.txt", _
filefilter:="Text Files, *.txt")

if myfilename = false then
exit sub 'user hit cancel
end if

....

open myfilename for output as #1

...

print #1, currtextstr


======
You don't need the initialfilename if you don't want it.
 
Many thanks, that was perfect.
Geoff.

Dave Peterson said:
Dim myFileName As Variant

myFileName = Application.GetSaveAsFilename _
(InitialFileName:="C:\myfile.txt", _
filefilter:="Text Files, *.txt")

if myfilename = false then
exit sub 'user hit cancel
end if

....

open myfilename for output as #1

...

print #1, currtextstr


======
You don't need the initialfilename if you don't want it.
 
Back
Top