我在Bartender(标签软件)模板上有一些VBScript,它访问Excel电子表格并将给定BOM代码的数据连接到报表上的字符串(所有数据都从同一行中提取)。通过与Bartender集成的应用程序修改电子表格或运行脚本会导致许多Excel实例打开,并且极大地降低了使用该模板的任何计算机的速度。
Public Function GetData()
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open _
("C:\Users\Public\Formulator\Labels\BOMSuffixes.xlsx")
intRow = 2
strNames = ""
genericDescription = ""
containerWeight = ""
containerUom = ""
Do Until objExcel.Cells(intRow,7).Value = ""
strNames = objExcel.Cells(intRow, 7).Value
If InStr(Field("BOM.BOMCode"),strNames) <> 0 Then
genericDescription = objExcel.Cells(intRow, 8).Value
containerWeight = objExcel.Cells(intRow, 9).Value
containerUom = objExcel.Cells(intRow, 10).Value
End If
intRow = intRow + 1
Loop
GetData = Field("BOM.BulkQuantity") + " " + Field("BOM.BulkUnits") +
" (" + CStr(Round(Field("BOM.BulkQuantity")*0.453592, 2) ) +
" Kg)" +" packed in a " + CStr(containerWeight) + " " +
containerUom + " (" + CStr(Round(containerWeight*0.453592, 2) ) +
" Kg)" + ", " + genericDescription
objExcel.Close
objExcel.Quit
objExcel.DisplayAlerts = False
End Function
答案 0 :(得分:0)
如果要避免反复创建和销毁Excel实例,可以让实例在脚本末尾运行,并(尝试)在开头重新连接到已经运行的实例。只有在失败时才创建新实例。
Public Function GetData()
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
If Err Then
If Err.Number = 429 Then
Set objExcel = CreateObject("Excel.Application")
Else
WScript.Echo Err.Number & ": " & Err.Description
WScript.Quit 1
End If
End If
On Error Goto 0
...
objExcel.Close
objExcel.Quit
objExcel.DisplayAlerts = False
Set objExcel = Nothing
End Function
但是请注意,使用这种方法,您需要某种清理工作来删除孤立的Excel实例。