我有一个 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?
发布评论
评论(2)
(摘自: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 文件,则此行代码将失败:
但此代码始终有效:
这些是 Excel 2007 中的主要文件格式:
.xlsm)
宏,.xlsb)
(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:
But this code will always work:
These are the main file formats in Excel 2007:
.xlsm)
macro's, .xlsb)
如果您不知道,您可以检查宏中应用程序的版本,这样您就可以确定是否应将其保存为“xls”或“xlsm”
希望这会有所帮助。
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"
Hope this helps.