vba Excel 中的 SaveAs FileFormat 参数与版本无关

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4210895/
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-11 12:19:01  来源:igfitidea点击:

SaveAs FileFormat argument in Excel isn't version-dependent

vbaexcel-vbaexcel-2007file-formatexcel

提问by NeedHelp

Starting in Excel 2007 it is recommended that you provide a FileFormatargument to the Workbook.SaveAscommand because even if you specified a .xls filename but without a FileFormatargument, it will make the file corrupt and unable to be read in Excel 2003 since the newly saved file will take the format of the ActiveWorkbook (which would be 2007).

从 Excel 2007 开始,建议您为命令提供一个FileFormat参数,Workbook.SaveAs因为即使您指定了 .xls 文件名但没有FileFormat参数,它也会使文件损坏并且无法在 Excel 2003 中读取,因为新保存的文件需要ActiveWorkbook 的格式(应该是 2007)。

In order to save in Excel 2003 compatible format, it is suggested to use these following FileFormatvalues: -4143if in Excel 2003 and 56in Excel 2007 as so:

为了以 Excel 2003 兼容格式保存,建议使用以下FileFormat值: -如果在 Excel 2003 中为4143, 在 Excel 2007 中为56,如下所示:

If Val(Application.Version) < 12 Then
  ' You use Excel 97-2003
  FileExtStr = ".xls": FileFormatNum = -4143
Else
  ' you use excel 2007 or later
  FileExtStr = ".xls": FileFormatNum = 56
End If

However i tried using -4143regardless if the executing client was Excel 2003 or 2007 and it worked fine. Can anyone confirm if this is the case?

但是,无论执行的客户端是 Excel 2003 还是 2007,我都尝试使用-4143并且它运行良好。任何人都可以确认是否是这种情况?

Source: Use VBA SaveAs in Excel 2007-2010

来源:在 Excel 2007-2010 中使用 VBA 另存为

回答by baldmosher

If you're using Excel 2003 to save a 2003 format file, then there's no need to specify the file format at all, since the default would be to save 2003 format anyway. The Application.Version code in the question works fine -- but no need to specify the FileFormat.

如果您使用 Excel 2003 来保存 2003 格式的文件,则根本不需要指定文件格式,因为默认情况下无论如何都会保存 2003 格式。问题中的 Application.Version 代码工作正常——但无需指定 FileFormat。

Unless you're using the 2007 document converters in 2003, in which case I don't know how 2003 would play that card, but I expect it'll choose the default (56) unless you specify otherwise.

除非您在 2003 年使用 2007 文档转换器,在这种情况下我不知道 2003 会如何播放该卡,但我希望它会选择默认值 (56),除非您另有说明。

NB: I certainly can't specify xlFileFormat.xlExcel8 in 2003, as suggested above, so that's not much help.

注意:我当然不能在 2003 年指定 xlFileFormat.xlExcel8,正如上面所建议的,所以这没有多大帮助。

回答by Dirk Vollmar

I'd suggest explicitly using xlFileFormat.xlExcel8which equals 56 if you want to get a binary .xls document.

xlFileFormat.xlExcel8如果您想获得二进制 .xls 文档,我建议明确使用which equals 56。