打开Excel文件并另存为.XLS

打开Excel文件并另存为.XLS,excel,vbscript,ms-office,Excel,Vbscript,Ms Office,我有以下代码,我想让它打开我的文件,这些文件被保存为.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 L

我有以下代码,我想让它打开我的文件,这些文件被保存为.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
两个严重的错误:

  • Set fso=Nothing
    不应该在循环中:您需要
    fso
    在课程期间

  • 此外,从循环中删除
    app.Quit
    ;保持Excel处于打开状态,直到 结束

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

如前所述,
Set fso=Nothing
app.Quit
属于脚本末尾(循环之外)。不过,还有一些bug

  • wb.SaveAs“*.xls*”

    无法将工作簿保存为通配符名称。如果要以当前名称保存工作簿,只需使用
    wb.save
    。否则,您必须使用显式名称(然后还应设置文件类型):

  • wb.Close SaveChanges=True

    VBScript不支持命名参数(请参见)。如果要在
    SaveChanges
    参数设置为
    True
    的情况下调用
    Close
    方法,则必须这样做:

    wb.Close True
    
  • 应用程序关闭

    应用程序对象没有
    Close
    方法

不是bug,而是值得改进的东西:

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

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

固定脚本:

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中打开该文件会导致错误)。稍微更改此脚本,使其成为从
xlsxb
xlsxm
的转换器:@AnsgarWiechers,“另存为”行中的数字是什么意思?@EsterF。它们是文件格式参数的数值。检查一下。
wb.Close 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
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