如何修复vbscript中的CopyFromRecordset错误

时间:2019-03-25 12:05:16

标签: sql-server excel vba vbscript

从VBScript执行宏会导致错误

  

对象“范围”的方法CopyFromRecordset失败

但是,当我从Excel工作簿中执行相同操作时,效果很好。

我尝试从Excel工作簿执行,但效果很好。

VBA代码: 设置conn = New ADODB.Connection “ conn.connectionString =” PROVIDER = SQLOLEDB;数据源= localhost \ SQLEXPRESS; INITIAL CATALOG = DEVDB; INTEGRATED SECURITY = sspi;“     Set rs = New ADODB.Recordset

str = "exec SP_Getcustomers @RequestID=" & requestID
rs.Open str, conn, adOpenStatic, adLockReadOnly

If Not IsEmptyRecordset(rs) Then
    rs.MoveFirst

    'Populate the first row of the sheet with recordset’s field names
    i = 0
    For Each fld In rs.Fields
        ActiveWorkbook.Worksheets("Customer Table").Cells(1, i + 1).Value = rs.Fields.Item(i).Name
        i = i + 1
    Next fld
    'Populate the sheet with the data from the recordset
    ActiveWorkbook.Worksheets("Customer Table").Range("A2").CopyFromRecordset rs  
End If

VB脚本代码:

Dim path, macroname
path = ""
path = Wscript.Arguments.Item(2) 
macroname = ""
macroname = Wscript.Arguments.Item(3)



Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'" & path & "'!" & macroname,     WScript.Arguments.Item(0)
objExcel.Visible = False
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing

0 个答案:

没有答案