Excel SaveAs对话框

时间:2018-03-18 12:59:54

标签: excel vba excel-vba

我的代码有一个默认名称并为其添加时间戳。这很好用。现在我尝试创建一个弹出的另存为对话框,允许用户查看新文件名并编辑保存它的路径。它在下面第2部分给出了一个错误:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim sFileName As String
Dim sDateTime As String
Dim txtFileName As String

'0. Set new filename with timestamp.
With ThisWorkbook
    sDateTime = " (" & Format(Now, "yyyy-mm-dd hhmm") & ").xlsm"
    sFileName = "2018 Testy" & sDateTime

'1. Check of Save As was used.
If SaveAsUI = True Then
    Cancel = True

  '2. Call up your own dialog box.  Cancel out if user Cancels in the dialog box.
   txtFileName = Application.GetSaveAsFilename(sFileName, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm", , "Save As XLSM file")
    If txtFileName = "False" Then
        MsgBox "Action Cancelled", vbOKOnly
        Cancel = True
        Exit Sub
    End If

  '3. Save the file.
   Application.EnableEvents = False
    ThisWorkbook.SaveAs FileName:=txtFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    Application.EnableEvents = True

End If

End Sub

0 个答案:

没有答案