【问题标题】:Convert Excel file to CSV - cannot supress Excel prompt window将 Excel 文件转换为 CSV - 无法抑制 Excel 提示窗口
【发布时间】:2017-02-02 21:16:54
【问题描述】:

我有一个 Gulp 任务监视 Excel 文件并运行 VBScript:

Gulp 代码:

.pipe(shell([
    'XlsToCsv.vbs test test.xlsx test.csv'
]))

VBScript 代码:

If WScript.Arguments.Count < 3 Then
    WScript.Echo "Please specify the sheet, the source, the destination files. Usage: ExcelToCsv <sheetName> <xls/xlsx source file> <csv destination file>"
    Wscript.Quit
End If

csv_format = 6

Set objFSO = CreateObject("Scripting.FileSystemObject")

src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(1))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(2))

Dim oExcel
Set oExcel = CreateObject("Excel.Application")

Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)

oBook.Sheets(WScript.Arguments.Item(0)).Select
oBook.SaveAs dest_file, csv_format

oBook.Close False
oExcel.Quit

它工作正常,除了当我以 CSV 格式重新保存文件时提示跳转。 比如“同名的文件已经存在。要保存吗?”

我无法在保存之前删除之前的文件,因为 Gulp 观察程序会触发代码执行。 oBook.Close SaveChanges = TrueoExcel.DisplayAlerts = False 不起作用。

我需要在每次保存时静默运行此脚本。

我应该在脚本中添加什么命令?

附:我不介意使用任何其他脚本,除了 PowerShell - Set-ExecutionPolicy remotesigned 导致错误

Set-ExecutionPolicy : 拒绝访问注册表项 HKEY_LOCAL_MACHINE...\Microsoft.PowerShell'

【问题讨论】:

  • Re: PowerShell - 您需要在提升的提示符下运行 Set-ExecutionPolicy

标签: excel powershell csv vbscript gulp


【解决方案1】:

在保存之前删除现有文件

If objFSO.FileExists(dest_file) Then objFSO.DeleteFile dest_file
oBook.SaveAs dest_file, csv_format

或设置DisplayAlerts = False 以禁止提示(这也强制替换现有文件):

oExcel.DisplayAlerts = False
oBook.SaveAs dest_file, csv_format

【讨论】:

  • 1.我无法删除该文件,因为 Gulp 观察程序触发了不必要的代码执行 2. 我在错误的位置添加了 DisplayAlerts = False。谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-03-19
  • 1970-01-01
  • 2021-07-21
  • 2013-05-19
  • 2018-01-17
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
相关资源
最近更新 更多