问题
When a user opens up this workbook, I want to force them to save the file as a new file immediately. The dialog box opens up but it will only let you save it as "All Files".
Dim Workbook_Orig As Variant
Workbook_Orig = Application.GetSaveAsFilename
If Workbook_Orig <> False Then
ActiveWorkbook.SaveAs _
Filename:="File Name", _
FileFormat:=52
End If
In place of the "52" I've tried "xlOpenXMLWorkbookMacroEnabled" but that made no difference.
Can you not SaveAs immediately? Do you have to make a change to the file or something?
Any help is greatly appreciated.
回答1:
Try specifying a file filter:
Workbook_Orig = Application.GetSaveAsFilename( _
fileFilter:="XLSM Files (*.xlsm), *.xlsm")
回答2:
Changing the FileFormat
only matters if they don't select anything in the dialogue box (since that is the only time workbook_orig = False)
Try specifying the file filter parameter
Workbook_Orig = Application.GetSaveAsFilename(fileFilter:="Excel Macro-Enabled Workbook (*.xlsm),*.xlsm")
In your GetSaveAsFileName
instruction.
回答3:
Why not present the user with an InputBox? Have them enter the new file name and have the macro perform the save as.
Something like:
Sub saveAs()
userAnswer = InputBox("Please enter filename")
ActiveWorkbook.saveAs Filename:= _
"C:\" & userAnswer & ".xls", FileFormat:=xlExcel8, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
End Sub
EDIT
Sub saveAs()
userAnswer = InputBox("Please enter filename")
userDirectory = InputBox("Please enter directory to save file")
ActiveWorkbook.saveAs Filename:= _
userDirectory & "\" & userAnswer & ".xls", FileFormat:=xlExcel8, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
End Sub
来源:https://stackoverflow.com/questions/37418708/vba-saveas-saving-as-file-type-instead-of-xlsm