Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using GetSaveAsFileName in Excel 2007 VBA

Status
Not open for further replies.

phudgens

Technical User
Jul 8, 2004
117
0
0
US
I am using the GetSaveAsFileName method in Excel 2007 VBA to allow the user to navigate to and specify a folder & file that will be used later in the program for output of text data. Following is my code:

Code:
With Application
    FileName = .GetSaveAsFilename(Filter, FilterIndex, Title)
    ChDrive (Left(.DefaultFilePath, 1))
    ChDir (.DefaultFilePath)
End With

VBA invariably returns "Error 2015" as my FileName, which is dimmed as Variant. Filter and Title are dimmed as String, and FilterIndex as Integer. I need to allow the user to specify an existing file OR specify a brand new file for output. Are there specific items that need to be active in my Reference list? I see lots of stuff on the net about this issue, but no solutions. Thanks for any help.
 


hi,

The FIRST argument is the optional InitialFileName, which you have chosen to ignore. Therefore, you must explicitly define the arguments as follows...
Code:
With Application
    FileName = GetSaveAsFilename(FileFilter:=Filter, FilterIndex:=FilterIndex, Title:=Title)
'....
End with

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 



Why are you changing the Drive and Directory in your code? I cannot remember the last time I used these archaic MS DOS commands.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
I have them so that the dialog opens in the D drive, rather than some other obscure place, to make navigation easier for the user.

Is there a way to make the GetSaveAsFileName dialog prompt for overwrite if the user selects an existing file? As it is, VBA just overwrites the file without asking. Thanks.
 


Well the GetSaveAsFileName opens in the DefaultFilePath folder, which is what you are assigning the dos functions above.

???

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top