如何修改 Excel 2007 的代码以使用 SaveAs

发布于 2024-11-09 13:51:01 字数 523 浏览 0 评论 0 原文

我有一个 Excel 模板,它使用宏来保存文件,以便用户保持标准化的文件名格式。我使用 Excel 2003 创建了代码。代码如下:

Sub SaveBook()
   Dim sFile As String
   sFile = "ConsolidatedDemand" & "_" & Format(Now(), "yyyy.mm.dd") & ".xls"
   ActiveWorkbook.SaveAs Filename:= "\\file location\" & sFile
End Sub

我有一位使用 Excel 2007 的用户。当他们尝试运行宏时,他们收到错误: “以下功能无法保存在无宏工作簿中:VB 项目。 要保存具有这些功能的文件,请单击“否”,然后在文件类型列表中选择启用宏的文件类型。要继续另存为无宏工作簿,请单击“是””

我尝试在第二行代码中将文件扩展名更改为“.xlsm”,但这产生了相同的错误消息。有关如何修改此代码的任何其他想法,以便它适用于 Excel 2007 用户吗?

I have an excel template that uses a macro to save the file, so that the users maintain standardized file name formats. I created the code using Excel 2003. the code is as follows:

Sub SaveBook()
   Dim sFile As String
   sFile = "ConsolidatedDemand" & "_" & Format(Now(), "yyyy.mm.dd") & ".xls"
   ActiveWorkbook.SaveAs Filename:= "\\file location\" & sFile
End Sub

I have one user who uses Excel 2007. When they try to run the macro, they recieve an error:
"The following features can not be saved in macro-free workbooks: VB project.
To save a file with these features, click No, and then choose macro-enabled file type in the file type list. To continue saving as a macro-free workbook, click yes"

I tried chaing the file extension to ".xlsm" in the second line of code, but that yielded the same error message. Any other ideas of how I can modify this code so that it will work for Excel 2007 users?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此岸叶落 2024-11-16 13:51:01

(摘自:http://blogs.office.com/b/microsoft-excel/archive/2009/07/07/use-the-vba-saveas-method-in-excel-2007.aspx)

在 Excel 2007 中,SaveAs 方法要求您提供 FileFormat 参数和正确的文件扩展名。

例如,在 Excel 2007 中,如果 ActiveWorkbook 不是 .xlsm 文件,则此行代码将失败:

ActiveWorkbook.SaveAs "C:\ron.xlsm"

但此代码始终有效:

ActiveWorkbook.SaveAs "C:\ron.xlsm", fileformat:=52 

这些是 Excel 2007 中的主要文件格式:

  • 51 = xlOpenXMLWorkbook(2007 年没有宏,.xlsx )
  • 52 = xlOpenXMLWorkbookMacroEnabled(2007 年有或没有宏,
    .xlsm)
  • 50 = xlExcel12(2007 年的 Excel 二进制工作簿,有或没有
    宏,.xlsb)
  • 56 = xlExcel8(Excel 2007 中的 97-2003 格式,.xls)

(taken from: http://blogs.office.com/b/microsoft-excel/archive/2009/07/07/use-the-vba-saveas-method-in-excel-2007.aspx)

In Excel 2007, the SaveAs method requires you to provide both the FileFormat parameter and the correct file extension.

For example, in Excel 2007 this line of code will fail if the ActiveWorkbook is not an .xlsm file:

ActiveWorkbook.SaveAs "C:\ron.xlsm"

But this code will always work:

ActiveWorkbook.SaveAs "C:\ron.xlsm", fileformat:=52 

These are the main file formats in Excel 2007:

  • 51 = xlOpenXMLWorkbook (without macro's in 2007, .xlsx)
  • 52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007,
    .xlsm)
  • 50 = xlExcel12 (Excel Binary Workbook in 2007 with or without
    macro's, .xlsb)
  • 56 = xlExcel8 (97-2003 format in Excel 2007, .xls)
会发光的星星闪亮亮i 2024-11-16 13:51:01

如果您不知道,您可以检查宏中应用程序的版本,这样您就可以确定是否应将其保存为“xls”或“xlsm”

If Application.Version = "13.0" Then
    MsgBox "You are using Excel 2010."
ElseIf Application.Version = "12.0" Then
    MsgBox "You are using Excel 2007."
ElseIf Application.Version = "11.0" Then
    MsgBox "You are using Excel 2003."
ElseIf Application.Version = "10.0" Then
    MsgBox "You are using Excel 2002."
ElseIf Application.Version = "9.0" Then
    MsgBox "You are using Excel 2000."
ElseIf Application.Version = "8.0" Then
    MsgBox "You are using Excel 97."
ElseIf Application.Version = "7.0" Then
    MsgBox "You are using Excel 95."
Else
    MsgBox "You are using an unknown version of Excel: " & Application.Version
End If

希望这会有所帮助。

Just incase you don't know, you can check which version the application is in the macro, that way you can determine if it should be saved as "xls" or "xlsm"

If Application.Version = "13.0" Then
    MsgBox "You are using Excel 2010."
ElseIf Application.Version = "12.0" Then
    MsgBox "You are using Excel 2007."
ElseIf Application.Version = "11.0" Then
    MsgBox "You are using Excel 2003."
ElseIf Application.Version = "10.0" Then
    MsgBox "You are using Excel 2002."
ElseIf Application.Version = "9.0" Then
    MsgBox "You are using Excel 2000."
ElseIf Application.Version = "8.0" Then
    MsgBox "You are using Excel 97."
ElseIf Application.Version = "7.0" Then
    MsgBox "You are using Excel 95."
Else
    MsgBox "You are using an unknown version of Excel: " & Application.Version
End If

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文