vba 打开excel 2007 excel文件并在VBA中另存为97-2003格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2142679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:14:36  来源:igfitidea点击:

Open excel 2007 excel files and save as 97-2003 formats in VBA

excelvbasave-as

提问by ABB

I have a weird situation where I have a set of excel files, all having the extension .xls., in a directory where I can open all of them just fine in Excel 2007. The odd thing is that I cannot open them in Excel 2003, on the same machine, without opening the file first in 2007 and going and saving the file as an "Excel 97-2003 Workbook". Before I save the file as an "Excel 97-2003 Workbook" from Excel 2007, when I open the excel files in 2003 I get the error that the file is not in a recognizable format.

我有一个奇怪的情况,我有一组 excel 文件,所有文件的扩展名都是 .xls。,在一个目录中,我可以在 Excel 2007 中打开所有这些文件。奇怪的是我无法在 Excel 2003 中打开它们, 在同一台机器上,没有在 2007 年首先打开文件并将文件保存为“Excel 97-2003 工作簿”。在我将文件从 Excel 2007 保存为“Excel 97-2003 工作簿”之前,当我在 2003 年打开 excel 文件时,我收到文件不是可识别格式的错误。

So my question is: if I already have the excel file opened in 2007 and I already have the file name of the open file stored in a variable, programatically how can I mimic the action of going up to the "office button" in the upper right and selecting, "save as" and then selecting "Excel 97-2003 Workbook"? I've tried something like the below but it does not save the file at all:

所以我的问题是:如果我已经在 2007 年打开了 excel 文件并且我已经将打开文件的文件名存储在一个变量中,那么我如何以编程方式模仿上升到“办公室按钮”的动作在上部右键并选择“另存为”,然后选择“Excel 97-2003 工作簿”?我试过类似下面的东西,但它根本不保存文件:

ActiveWorkbook.SaveAs TempFilePath & TempFileName & ".xls", FileFormat:=56

Thanks for any help or guidance!

感谢您的任何帮助或指导!

回答by burnside

It sounds like you should try the compatibility pack, but I can't see why your vba doesn't work. I tried the below and it worked perfectly:

听起来您应该尝试使用兼容包,但我不明白为什么您的 vba 不起作用。我尝试了以下方法,效果很好:

ThisWorkbook.SaveAs "C:\test" & ".xls", FileFormat:=56

回答by guitarthrower

This page: http://www.rondebruin.nl/saveas.htmhas been helpful for me. You have to declare a different file type if you are saving from 2007 to 2003 and earlier.

这个页面:http: //www.rondebruin.nl/saveas.htm 对我很有帮助。如果您要从 2007 年到 2003 年及更早的时间进行保存,则必须声明不同的文件类型。

回答by Nicolas C.

Here is a solution which is working for me. Basically, it opens again the same file and simulates a "Save As" from the Office button with the compatibiliy of Excel 97-2003. However, it does hide any alerts and specifies to Excel to NOT check the compatibility which is generating a popup window preventing you to work silentely in batch mode.

这是一个对我有用的解决方案。基本上,它再次打开同一个文件并模拟来自 Office 按钮的“另存为”与 Excel 97-2003 的兼容性。但是,它确实隐藏了任何警报并指定 Excel 不检查兼容性,这会生成一个弹出窗口,阻止您在批处理模式下静默工作。

'before that, my code was using Workbooks.Add method, saving and then closing.
'just add the following after your last operation, once your file is closed where
'of course "resultFile" is the workbook to be saved, and resultFileName the path

Application.DisplayAlerts = False
Set resultFile = Workbooks.Open(resultFileName)
resultFile.CheckCompatibility = False
resultFile.SaveAs Filename:=resultFileName, FileFormat:=xlExcel8
resultFile.Close
Application.DisplayAlerts = True

回答by user2481205

just closing the alerts also works. Application.DisplayAlerts = False Workbooks.Add.SaveAs name, FileFormat:=56 * do modifications to the file if necessary Activeworkbook.close SaveChanges:=true Application.DisplayAlerts = True

只是关闭警报也有效。Application.DisplayAlerts = False Workbooks.Add.SaveAs name, FileFormat:=56 * 必要时修改文件 Activeworkbook.close SaveChanges:=true Application.DisplayAlerts = True

回答by Juan Fernando

'this script is created by me. But usable to vbscript ( vbs ).
UTILIZAR EL ARCHIVO INICIAL.XLSX COMO COMODíN PARA CREAR UN ARCHIVO DATOS.XLS.

'Creado por Juan Fernando Arango Trujillo
Set app = CreateObject("Excel.Application")
app.Visible = False
app.DisplayAlerts = False
Set fso = CreateObject("Scripting.FileSystemObject")
Set wb = app.Workbooks.Open("C:\Users\Juan\Documents\Inicial.xlsx") 
wb.SaveAs "C:\Users\Juan\Documents\Datos.xls",  -4143
wb.Close True
app.Quit
Set app = Nothing
Set fso = Nothing

'FIN DEL PROCESO DE CREACIóN DE DATOS.XLS

'FIN DEL PROCESO DE CREACIón DE DATOS.XLS

回答by Jay

I'm not clear on whether you are just trying to convert all those files or if you are working on an application that will need to do that.

我不清楚您是只是想转换所有这些文件,还是正在开发需要这样做的应用程序。

If the former, see if this is helpful : http://www.microsoft.com/downloads/details.aspx?familyid=941b3470-3ae9-4aee-8f43-c6bb74cd1466&displaylang=en

如果是前者,看看这是否有帮助:http: //www.microsoft.com/downloads/details.aspx?familyid= 941b3470-3ae9-4aee-8f43-c6bb74cd1466&displaylang= en

It allows you to open Office 2007 in earlier versions of Office.

它允许您在早期版本的 Office 中打开 Office 2007。