繁体   English   中英

如何制作给出错误 800A03EC“文件已移动或删除”的 VBS 文件并忽略该错误?

[英]How to make a VBS file giving Error 800A03EC “File was moved or deleted” and ignore the error?

我从另一个 Stack Overflow 页面复制了这个。 不记得在哪里,否则我会给予信任。 该脚本打开一个 XLS 文件并将其保存为 CSV 文件。 但是,有时用户会在服务运行之前移动或删除文件,以使用 XML 文件清理文件夹中的所有内容。 如果发生这种情况并且文件丢失,我会收到一个错误,导致我的小自动化任务崩溃。 如果.vbs只是忽略文件不存在的错误,我会喜欢它。 我没有弹出窗口或确认。 我希望它继续前进到下一个 CMD 行。

VBScript 文件:

if WScript.Arguments.Count < 2 Then
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.SaveAs WScript.Arguments.Item(1), 6
oBook.Close False
oExcel.Quit

这就是我从自动 CMD 运行它的方式:

XlsToCsv.vbs "location-inputfile.xls" "location-outputfile.csv"

我尝试了其他几个版本。

感谢大家的帮助。 想出了这个。 让我知道你们是否都看到任何事情完全搞砸了。

Function FileExists(FilePath)
  Set fso = CreateObject("Scripting.FileSystemObject")
  If fso.FileExists(FilePath) Then
    FileExists=CBool(1)
  Else
    FileExists=CBool(0)
  End If
End Function

If FileExists(Wscript.Arguments.Item(0)) Then
    Dim oExcel
    Set oExcel = CreateObject("Excel.Application")
    Dim oBook
    Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
    oBook.SaveAs WScript.Arguments.Item(1), 6
    oBook.Close False
    oExcel.Quit
Else
  WScript.quit
End If

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM