使用 vba 名称语句将 xls 转换为 xlsx 会损坏文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15800044/
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
xls to xlsx conversion using vba name statement corrupts the file
提问by user2242660
I am trying to rename a file from abcd.xls
to xyz.xlsx
using the following code-
我试图从重命名文件abcd.xls
来xyz.xlsx
使用以下代码-
NumOfAttachments = Range("RecCount").Value
DestinationFolderPath = Range("destinationfolder").Value
NewShtName = "Sheet1"
If NumOfAttachments <> 0 Then
For X = 0 To NumOfAttachments - 1
OrigName = Range("Startcell").Offset(X, 1).Value
NewName = Range("startcell").Offset(X, 2).Value
SourceFolderPath = Range("startcell").Offset(X, 3).Value
NewFile = DestinationFolderPath & NewName
If Dir(DestinationFolderPath & OrigName) <> "" Then Kill DestinationFolderPath & OrigName
FileCopy SourceFolderPath & OrigName, DestinationFolderPath & OrigName
If Dir(NewFile) <> "" Then Kill NewFile
Name DestinationFolderPath & OrigName As NewFile
After this when I try to open the file(xyz.xlsx
) I get the following error-
在此之后,当我尝试打开文件(xyz.xlsx
)时,出现以下错误-
excel cannot open the file xyz.xlsx because the file format or file extension is not valid
excel cannot open the file xyz.xlsx because the file format or file extension is not valid
回答by Siddharth Rout
The reason is very simple. You are not using the correct file format.
原因很简单。您没有使用正确的文件格式。
Your .SaveAs
code should look like this
你的.SaveAs
代码应该是这样的
.SaveAs "\myserver\test\xyz.xlsx", FileFormat:=51
See the below table for File Formats
文件格式见下表
50 = xlExcel12 (Excel Binary Workbook in 2007-2010 with or without macro's, xlsb)
51 = xlOpenXMLWorkbook (without macro's in 2007-2010, xlsx)
52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2010, xlsm)
56 = xlExcel8 (97-2003 format in Excel 2007-2010, xls)
Would recommend this link
会推荐这个链接