VBA转换为VBScript - 不工作,但没有错误(Converting VBA to VBSc

2019-09-27 10:56发布

我一直在下面的文章和VBA转换为VBScript的问题,但现在我卡住了。 下面的代码仍然工作在VBA(如果我删除的子过程调用),但它不会运行的脚本。

该代码打开到SQL Server的连接来检查表,看看是否进程已今天运行,并将结果加载到一个记录。 如果该字段设置为No ,然后它打开了一个Excel工作簿和运行一个宏。 它工作在VBA但是当我运行相同的代码作为脚本执行任何操作(没有任何错误)。

你能看到的问题是什么? 非常感谢。

NB。 有两行cmd.CommandText 。 注释掉线的设计总是返回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

Answer 1:

无法实例在VBScript与如外部对象的New ADODB.Connection因为有对外部库的引用。

你不能使用常量像adCmdText无论是。 他们将被视为未定义的空变量。

因为你让他们闭嘴你没有得到任何错误, On Error Resume Next 。 删除,你会得到你的错误。

确保所有外部对象实例化与做CreateObject喜欢自己正在做的Excel中,并与他们的文字值替换所有的外部常数。



文章来源: Converting VBA to VBScript - Not working but no error