【问题标题】:Open an Excel file and save as .XLS打开 Excel 文件并另存为 .XLS
【发布时间】:2013-07-04 07:50:56
【问题描述】:

我有以下代码,我希望它打开保存为 .xlsx 的文件,然后使用相同的文件名再次保存它们,但这次保存为 .xls 文件,以便它们与 Excel 2003 兼容

Set app = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder("Y:\Billing_Common\autoemail").Files
  If LCase(fso.GetExtensionName(f)) = "xlsx" Then
    Set wb = app.Workbooks.Open(f.Path)

app.DisplayAlerts = False

wb.SaveAs "*.xls*"
wb.Close SaveChanges=True
app.Close
app.Quit

  End if

Set f = Nothing
Set fso = Nothing
Next

【问题讨论】:

  • 很高兴看到一些代码,但您能向我们描述一下目前解决方案的问题吗?

标签: excel vbscript ms-office


【解决方案1】:

正如Bathsheba 已经指出的那样,Set fso = Nothingapp.Quit 属于脚本末尾(循环外)。不过还有一些错误。

  • wb.SaveAs "*.xls*"

    您无法将工作簿保存为通配符名称。如果您想以当前名称保存工作簿,只需使用wb.Save。否则你将不得不使用一个明确的名字(你还应该设置文件类型):

    wb.SaveAs "new.xlsx", 51
    

    wb.SaveAs "C:\path\to\new.xls", -4143
    
  • wb.Close SaveChanges=True

    VBScript 不支持命名参数(请参阅here)。如果你想调用 Close 方法并将 SaveChanges 参数设置为 True 你必须这样做:

    wb.Close True
    
  • app.Close

    应用程序对象没有Close 方法。

不是错误,而是值得改进的地方:

  • app.DisplayAlerts = False 应该在循环开始之前运行,除非您也在循环内重新启用它。

  • 我建议在创建应用程序对象后添加一行app.Visible = False。当您必须调试脚本时,您只需将该值更改为 True 即可在桌面上显示应用程序。这对发现错误很有帮助。

修正脚本:

Set app = CreateObject("Excel.Application")
app.Visible = False
app.DisplayAlerts = False

Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder("Y:\Billing_Common\autoemail").Files
  If LCase(fso.GetExtensionName(f)) = "xlsx" Then
    Set wb = app.Workbooks.Open(f.Path)

    wb.Save
    wb.Close True
  End if
Next

app.Quit
Set app = Nothing
Set fso = Nothing

【讨论】:

  • 是否可以将其保存为新的文件类型,但保留原来的名称?我正在使用您提供的脚本。顺便感谢一百万!
  • 这是可能的,但这样做可能会遇到问题(例如,您可以将 OpenXML 工作簿保存为旧工作簿格式,但尝试在 Excel 中打开该文件会导致错误)。
  • 稍作改动以使此脚本成为从xlsxbxlsxm 的转换器:pastebin.com/JJbM9qRC
  • @AnsgarWiechers, save as 行中的数字是什么意思?
  • @EsterF。它们是文件格式参数的数值。检查documentation
【解决方案2】:

两个严重的错误:

  • Set fso = Nothing 不应该在你的循环中:你需要fso 在项目期间。

  • 另外,从循环中删除app.Quit;保持 Excel 打开直到非常
    结束。

Set f = Nothing 是不必要的(尽管是良性的);让循环为您选择值。

【讨论】:

    【解决方案3】:
    Dim app, fso, file, fName, wb, dir 
    
    dir = "d:\path\"
    
    Set app = CreateObject("Excel.Application")
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    For Each file In fso.GetFolder(dir).Files
        If LCase(fso.GetExtensionName(file)) = "xlsx" Then  
        fName = fso.GetBaseName(file)
    
        Set wb = app.Workbooks.Open(file) 
        app.Application.Visible = False
        app.Application.DisplayAlerts = False
        app.ActiveWorkbook.SaveAs dir & fName & ".xls", 43
        app.ActiveWorkbook.Close 
        app.Application.DisplayAlerts = True 
        app.Application.Quit 
    
        End if
    Next
    
    Set fso = Nothing
    Set wb = Nothing    
    Set app = Nothing
    
    wScript.Quit
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多