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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SaveAs code doesn't work

Status
Not open for further replies.

wmbb

Technical User
Jul 17, 2005
320
NL
I want to open the saveas dialog to save a workbook using VBA.
I've created the code below but it doesn't work.
"methode 'execute' of object 'filedialog' failed"
Has anyone an idee what goes wrong ?

Code:
Sub SaveWorkbook()
Set fd = Application.FileDialog(msoFileDialogSaveAs)
With fd
  .FilterIndex = 2
  .AllowMultiSelect = False
  .InitialView = msoFileDialogViewDetails
  .Title = "Choose file location..."
  .InitialFileName = "save_test" & ".xlsm"
  If .Show = -1 Then .Execute
End With
Set fd = Nothing
End Sub
 
Why not use the Application.GetSaveAsFilename method?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Because I didn't know about this method !
I'm no expert in programming and I try to learn VBA by this forum and [Try and Error]
I'll try this methode and see if I can use this.

Thanks for your suggestion.
 
I changed the code to...

Code:
fileSaveName = Application.GetSaveAsFilename( _
InitialFileName:="save_test.xlsm" _
FileFilter:="Test Workbook , *.xlsm", _
Title:="Choose file location...")
If fileSaveName <> False Then ActiveWorkbook.SaveAs FileFormat:=fileSaveName

But this code also gives the same error:
"Methode 'SaveAs' of object '_Workbook' failed"

 
This error often occurs becasue the selected file format for the save is wrong. This is surely the case for your GetSaveAsFilename code

ActiveWorkbook.SaveAs FileFormat:=fileSaveName

should probably be

ActiveWorkbook.SaveAs Filename:=fileSaveName

This does not explain why you get the problem with your original code, unless there is additional code that you didn't show us.
 
Thank you for your response.
You were right, a little type error.
Changed ActiveWorkbook.SaveAs FileFormat:=fileSaveName to ActiveWorkbook.SaveAs fileSaveName and it works like it should.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top