将 VBA 转换为 VBScript - 不工作但没有错误
Converting VBA to VBScript - Not working but no error
我一直在关注有关将 VBA 转换为 VBScript 的文章和问题,但我现在被困住了。以下代码在 VBA 中仍然有效(如果我删除 Sub 例程调用)但它不会 运行 作为脚本。
代码打开到 SQL 服务器的连接以检查 table 以查看该进程今天是否已经 运行 并将结果加载到 Recordset 中。如果该字段设置为 No
,则它会打开一个 Excel 工作簿和 运行 一个宏。它在 VBA 中工作,但是当我 运行 与脚本相同的代码时,什么也没有发生(也没有错误)。
你能看出是什么问题吗?非常感谢。
注意。 cmd.CommandText
有两行。注释掉的行设计为始终 return No
仅用于测试目的。
' Author Steve Wolstencroft
' Inititates the Automated Excel Refresh Procedure
Option Explicit
Pivot_Refresh
Public Function ConnectToSQLDwarfP()
On Error Resume Next
ConnectToSQLDwarfP = "Driver={SQL Server Native Client 10.0};Server=DwarfP;Database=DwarfPortable;Trusted_Connection=yes;"
End Function
Public Sub Pivot_Refresh()
On Error Resume Next
Dim cnx
Dim Rst
Set cnx = New ADODB.Connection
cnx.ConnectionString = ConnectToSQLDwarfP
cnx.Open
Dim cmd
Set cmd = New ADODB.Command
cmd.ActiveConnection = cnx
cmd.CommandType = adCmdText
cmd.CommandText = "Select Case When max(DwarfPortable.dbo.fn_GetJustDate(pl.StartDateTime)) = DwarfPortable.dbo.fn_GetJustDate(getDate()) Then 'Y' Else 'N' End as RunToday From ProcessControl.dbo.ProcessLog pl Where pl.ProcessName = 'Excel_Auto_Refresh'"
'cmd.CommandText = "Select Case When max(pl.StartDateTime) = DwarfPortable.dbo.fn_GetJustDate(getDate()) Then 'Y' Else 'N' End as RunToday From ProcessControl.dbo.ProcessLog pl Where pl.ProcessName = 'Excel_Auto_Refresh'"
Set Rst = cmd.Execute
Dim objXL, objBook
Set objXL = CreateObject("Excel.Application")
If Rst.Fields("RunToday") = "N" Then
Set objBook = objXL.Workbooks.Open("\nch\dfs\SharedArea\HI\Clinical-Informatics\InfoRequestOutputs\Regular-Jobs\Pivot-Refresh\Pivot-Refresh-Control.xls", 0, True)
objXL.Application.Visible = True
objXL.Application.Run "'Pivot-Refresh-Control.xls'!Auto_Refresh"
objXL.ActiveWindow.Close
objXL.Quit
Set objBook = Nothing
Set objXL = Nothing
End If
End Sub
您不能在 VBScript 中实例化外部对象,例如New ADODB.Connection
因为没有对外部库的引用。
你 can't use constants 也喜欢 adCmdText
。它们将被视为未定义的空变量。
你不会收到任何错误,因为你用 On Error Resume Next
关闭了它们。删除它,你会得到你的错误。
确保使用 CreateObject
完成所有外部对象实例化,就像您对 Excel 所做的那样,并将所有外部常量替换为它们的字面值。
我一直在关注有关将 VBA 转换为 VBScript 的文章和问题,但我现在被困住了。以下代码在 VBA 中仍然有效(如果我删除 Sub 例程调用)但它不会 运行 作为脚本。
代码打开到 SQL 服务器的连接以检查 table 以查看该进程今天是否已经 运行 并将结果加载到 Recordset 中。如果该字段设置为 No
,则它会打开一个 Excel 工作簿和 运行 一个宏。它在 VBA 中工作,但是当我 运行 与脚本相同的代码时,什么也没有发生(也没有错误)。
你能看出是什么问题吗?非常感谢。
注意。 cmd.CommandText
有两行。注释掉的行设计为始终 return No
仅用于测试目的。
' Author Steve Wolstencroft
' Inititates the Automated Excel Refresh Procedure
Option Explicit
Pivot_Refresh
Public Function ConnectToSQLDwarfP()
On Error Resume Next
ConnectToSQLDwarfP = "Driver={SQL Server Native Client 10.0};Server=DwarfP;Database=DwarfPortable;Trusted_Connection=yes;"
End Function
Public Sub Pivot_Refresh()
On Error Resume Next
Dim cnx
Dim Rst
Set cnx = New ADODB.Connection
cnx.ConnectionString = ConnectToSQLDwarfP
cnx.Open
Dim cmd
Set cmd = New ADODB.Command
cmd.ActiveConnection = cnx
cmd.CommandType = adCmdText
cmd.CommandText = "Select Case When max(DwarfPortable.dbo.fn_GetJustDate(pl.StartDateTime)) = DwarfPortable.dbo.fn_GetJustDate(getDate()) Then 'Y' Else 'N' End as RunToday From ProcessControl.dbo.ProcessLog pl Where pl.ProcessName = 'Excel_Auto_Refresh'"
'cmd.CommandText = "Select Case When max(pl.StartDateTime) = DwarfPortable.dbo.fn_GetJustDate(getDate()) Then 'Y' Else 'N' End as RunToday From ProcessControl.dbo.ProcessLog pl Where pl.ProcessName = 'Excel_Auto_Refresh'"
Set Rst = cmd.Execute
Dim objXL, objBook
Set objXL = CreateObject("Excel.Application")
If Rst.Fields("RunToday") = "N" Then
Set objBook = objXL.Workbooks.Open("\nch\dfs\SharedArea\HI\Clinical-Informatics\InfoRequestOutputs\Regular-Jobs\Pivot-Refresh\Pivot-Refresh-Control.xls", 0, True)
objXL.Application.Visible = True
objXL.Application.Run "'Pivot-Refresh-Control.xls'!Auto_Refresh"
objXL.ActiveWindow.Close
objXL.Quit
Set objBook = Nothing
Set objXL = Nothing
End If
End Sub
您不能在 VBScript 中实例化外部对象,例如New ADODB.Connection
因为没有对外部库的引用。
你 can't use constants 也喜欢 adCmdText
。它们将被视为未定义的空变量。
你不会收到任何错误,因为你用 On Error Resume Next
关闭了它们。删除它,你会得到你的错误。
确保使用 CreateObject
完成所有外部对象实例化,就像您对 Excel 所做的那样,并将所有外部常量替换为它们的字面值。