【问题标题】:xls to xlsx conversion using vba name statement corrupts the file使用 vba name 语句将 xls 转换为 xlsx 会损坏文件
【发布时间】:2013-03-25 20:50:32
【问题描述】:

我正在尝试使用以下代码将文件从 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

之后,当我尝试打开文件时(xyz.xlsx)出现以下错误-

excel cannot open the file xyz.xlsx because the file format or file extension is not valid

【问题讨论】:

  • 您能edit 发布更多代码吗?我没有看到任何实际重命名文件的内容。 (你知道只是重命名它不会将它从 .xls 转换为 .xlsx 文件,对吗?它仍然是 .xls 文件。)
  • 我已经发布了整个代码。谢谢!

标签: vba excel rename corrupt


【解决方案1】:

原因很简单。您使用的文件格式不正确。

您的.SaveAs 代码应如下所示

.SaveAs "\myserver\test\xyz.xlsx", FileFormat:=51

文件格式见下表

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)

会推荐这个link

【讨论】:

    猜你喜欢
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多