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

问题描述

| 我有一个使用宏来保存文件的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用户的任何其他想法?     

解决方法

(摘自: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)     ,万一您不知道,您可以检查应用程序在宏中的版本,这样您可以确定应将其保存为\“ 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
希望这可以帮助。