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!

setting default current dir for file save 1

Status
Not open for further replies.

larryww

Programmer
Mar 6, 2002
193
US
I do some save-as macro code in Excel via ActiveWorkbook.SaveAs and wondered if I could specify a default destination location, rather than have to build a qualified path every time. I'm thinking that Application.DefaultFilePath is involved...?
 
Hey, larryww,

Excel has a couple of nifty methods to get open or save file names. Here's a snippet from Help for the GetSaveAsFilename Method...
Code:
GetSaveAsFilename Method
                

Displays the standard Save As dialog box and gets a file name from the user without actually saving any files.

Syntax

expression.GetSaveAsFilename(InitialFilename, FileFilter, FilterIndex, Title, ButtonText)

expression   Required. An expression that returns an Application object.

InitialFilename   Optional Variant. Specifies the suggested file name. If this argument is omitted, Microsoft Excel uses the active workbook's name.

FileFilter   Optional Variant. A string specifying file filtering criteria. 

This string consists of pairs of file filter strings followed by the MS-DOS wildcard file filter specification, with each part and each pair separated by commas. Each separate pair is listed in the Files of type drop-down list box. For example, the following string specifies two file filters, text and addin: "Text Files (*.txt), *.txt, Add-In Files (*.xla), *.xla".

To use multiple MS-DOS wildcard expressions for a single file filter type, separate the wildcard expressions with semicolons; for example, "Visual Basic Files (*.bas; *.txt),*.bas;*.txt".

If omitted, this argument defaults to "All Files (*.*),*.*".

FilterIndex   Optional Variant. Specifies the index number of the default file filtering criteria, from 1 to the number of filters specified in FileFilter. If this argument is omitted or greater than the number of filters present, the first file filter is used.

Title   Optional Variant. Specifies the title of the dialog box. If this argument is omitted, the default title is used.

ButtonText   Optional Variant. Macintosh only.

Remarks

This method returns the selected file name or the name entered by the user. The returned name may include a path specification. Returns False if the user cancels the dialog box.

This method may change the current drive or folder.
Hope this helps :) Skip,
metzgsk@voughtaircraft.com
 
Hi

This is what I did

Sub Save_Macro()

Dim myPath As String

myPath = "c:\Temp\"

ActiveWorkBook.SaveAs myPath & "myFileName.xls"

Application.Quit

End Sub

Hope this will helps.

Regards

LSTAN
 
LSTAN: thanks, though that's what I wanted to improve upon.

Skip, that is indeed nifty! Your best answer yet :)

One little thing:
Application.GetSaveAsFilename
alone is fantastic, but, imitating the help example,
Application.GetSaveAsFilename (Title := "Set destination dir")
and
Application.GetSaveAsFilename (,,,"Set destination dir")
fail to compile. Could I push my luck and ask for syntax help?! (anyone)
 
Actually, that was NOT an example. Here's an example from HELP...
Code:
GetSaveAsFilename Method Example

This example displays the Save As dialog box, with the file filter set to text files. If the user chooses a file name, the example displays that file name in a message box.

fileSaveName = Application.GetSaveAsFilename( _
    fileFilter:="Text Files (*.txt), *.txt")
If fileSaveName <> False Then
    MsgBox &quot;Save as &quot; & fileSaveName
End If
:) Skip,
metzgsk@voughtaircraft.com
 
Skip - I know, that's the example I imitated. Still, Application.GetSaveAsFilename compiles;
Application.GetSaveAsFilename (Title := &quot;Set destination dir&quot;)
bites.
 
Solved -
dim foo as variant
foo = Application.GetSaveAsFilename(, , , Title:=&quot;Set destination dir&quot;)

and discard (ignore) foo.

Again, nifty and effective. Muchos, muchos gracias, Skip.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top