MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

filedialog for saving text file

  Asked By: Clifton    Date: Mar 25    Category: MS Office    Views: 2280

I want to save text to a text file, and allow the user to select the
filename using a standard file dialog. I can do this using the
application.filedialog object, but the filter property is read-only,
so I end up with the full range of filename options that are normally
available to Excel. All I want is the *.txt option.

Is it possible to use the Commondialog control from VB? I don't have
VB installed on the system I am using for my VBA development, so I get
the license problem when I try to use the commondialog OCX by itself.



2 Answers Found

Answer #1    Answered By: Uma 1985     Answered On: Mar 25

Don't use FileDialog. Use GetSaveAsFilename to get the path & name for the new
file. You can set the filter  property on GetSaveAsFilename. Then use Save or
SaveAs or SaveAsCopy to actually write the data to disk. For example:

Sub ZZZZ()
Dim fName
fName = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt),
Loop Until fName <> False
ActiveWorkbook.SaveAs Filename:=fName, FileFormat:=xlTextMSDOS,
End Sub

Answer #2    Answered By: Angelina Gardner     Answered On: Mar 25

BTW, I found that I could make the FileDialog worked if I set
the .dialogtype to msoFileDialogFilePicker instead of
msoFileDialogFileSaveAs. I then have to change the .ButtonName
and .Title properies to "Save As".

Why having the dialogtype set to ..SaveAs makes the .filter property
read only is beyond me. I guess it makes sense to someone at MS.

Didn't find what you were looking for? Find more on filedialog for saving text file Or get search suggestion and latest updates.