0

我在通过 SQL Server 代理和 Windows 调度程序执行涉及 VB 脚本的 shell 命令时遇到问题(我试图让这些选项中的至少一个工作)。

当从 Windows 命令提示符手动执行时,该命令可以正常运行并完成。下面是实际的命令(它由一个带有两个参数的 VB 脚本组成):

XlsxToCsv.vbs DataDictionaries.xlsx DataDictionaries.csv

XlsxToCsv.vbs 是一个 VB 脚本,用于编辑 xlsx 文件并将其转换为 csv 格式。

当我安排上面的命令通过 SQL Server 作业代理运行时,它开始运行 VB 脚本,完成它的第一步,但然后有点永远不会结束或不会继续。它应该创建的文件没有被创建。因此,作业步骤一直在运行,永无止境。

以下是 VB 脚本中的内容:

'1. Delete the CSV file.
dFile = WScript.Arguments.Item(1)
SET oFso = CREATEOBJECT("Scripting.FileSystemObject")
oFso.DeleteFile dFile

'2. Read the input Excel file, replace , with c1o2m3m4a5 and save the file as csv, using the second parameter as the csv file name.
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
Dim oSheet
Set oSheet = oBook.Worksheets(1)
Dim oRange
Set oRange = oSheet.UsedRange
oRange.Replace ",", "c1o2m3m4a5"
oBook.SaveAs WScript.Arguments.Item(1), 6
oBook.Close False
oExcel.Quit

似乎当由 SQL Server 代理执行时,上面的 VB 脚本在某处“暂停”并等待某事。但是我很困惑,因为它从 MS-DOS 提示符(或命令提示符,虽然没有通过 power-shell 尝试过)手动运行得很好。

任何人都可以发现 VB 脚本的问题,或者通过 SQL Server 代理正确执行上述命令可能需要额外的东西?

请帮忙 :)

4

1 回答 1

0
  1. 由于在执行期间显示警报,脚本没有继续。以下命令关闭警报: https ://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-displayalerts-property-excel
oExcel.DisplayAlerts = False
  1. 我还需要确保 SQL Server 代理用户对该链接中讨论的特殊目录具有完全权限。 http://justgeeks.blogspot.com/2012/10/troubleshooting-microsoft-excel-cannot.html

最终脚本看起来像这样并且可以毫无问题地执行:

'Define an object to reference MS Excel application.
Dim oExcel
Set oExcel = CreateObject("Excel.Application")

'Turn-off alerts and messages for the MS Excel application object.
oExcel.DisplayAlerts = False

'Define an object to open and hold an Excel file/workbook.
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))

'Define an object to open and hold an Excel worksheet.
Dim oSheet
Set oSheet = oBook.Worksheets(1)

'Define an object and read and store the data from an Excel worksheet.
Dim oRange
Set oRange = oSheet.UsedRange

'Replace all the commas in the Excel worksheet object with    pattern "c1o2m3m4a5"
oRange.Replace ",", "c1o2m3m4a5"

'Save the edited Excel worksheet object into the CSV file 
'(CSV file name must be passed as the 2nd parameter when running this     script).
oBook.SaveAs WScript.Arguments.Item(1), 6

'Close the Excel file/workbook.
oBook.Close False

'Quit the MS Excel. End of the script.
oExcel.Quit
于 2018-04-23T20:09:37.183 回答