简体   繁体   English

另存为启用宏的工作簿

[英]Save as Macro Enabled Workbook

I have a macro in a.xlsm file.我在 a.xlsm 文件中有一个宏。

Sub SaveReport()

Dim ReportName As String
    ReportName = Sheets("Report").Range("B2")

    Application.Dialogs(xlDialogSaveAs).Show ReportName

End Sub

How can I make it save as a Macro Enabled file format by default?默认情况下,如何将其保存为启用宏的文件格式?

Try this:尝试这个:

Option Explicit ' It is a good practice to use this to force the compiler to ask for
                ' Var declaration before use it

Sub SaveReport()
    Dim ReportName As String 'As I told you you need to declare every var before
    Dim T As Workbook: Set T = ThisWorkbook 'Store the Workbook inside the var
                                            'Notice you can use "Thisworkbook" as a Var

    ReportName = Sheets("Report").Range("B2")
    T.SaveAs Filename:=ReportName, FileFormat:=52 'use this statement to:
                                                  ' Save the workbook WITHOUT any dialog box
                                                  ' and define the format you want
    'Application.Dialogs(xlDialogSaveAs).Show ReportName 
    'This line is not necesary.
End Sub

Here some links to know how it works in detail:这里有一些链接可以详细了解它的工作原理:

Workbook.SaveAs 工作簿.另存为

XlFileFormat enumeration XlFileFormat 枚举

Some different versions of saving it as ".xlsm"将其另存为“.xlsm”的一些不同版本

Option Explicit

Sub SaveReport_1()

Dim ReportName As String
    ReportName = Sheets("Report").Range("B2")
    Application.DefaultSaveFormat = xlOpenXMLWorkbookMacroEnabled

    Application.Dialogs(xlDialogSaveAs).Show ReportName

End Sub



Sub SaveReport_2()

Dim ReportName As String
    ReportName = Sheets("Report").Range("B2")

    Application.Dialogs(xlDialogSaveAs).Show ReportName, 52
End Sub



Sub SaveReport_3()

Dim ReportName As String
    ReportName = Sheets("Report").Range("B2")

    ActiveWorkbook.SaveAs Application.GetSaveAsFilename(ReportName, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm", , "Save As xlsm file")
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM