|
下面是我写的SAP连接方法,但是只能连接一次sap查询一次,如果有多张表就需要连接多次,谁能帮我改一下?
运行下面红字会连接一次。
Function login()
'MsgBox "Execution Start"
Set R3 = CreateObject("SAP.Functions")
'connect to SAP
'call function
Set MyFunc = R3.Add("RFC_READ_TABLE")
End Function
Function conectSAPHeader()
Dim iData As Double
Dim nField As Integer
Dim nData As Double
Dim Result As Boolean
Dim vRow As Variant
ThisWorkbook.Worksheets(1).Cells.Clear
Set oParam1 = MyFunc.Exports("QUERY_TABLE")
Set oParam2 = MyFunc.Tables("FIELDS")
Set oParam3 = MyFunc.Tables("OPTIONS")
Set oParam4 = MyFunc.Exports("DELIMITER")
Set oParam5 = MyFunc.Tables("DATA")
Set oParam6 = MyFunc.Exports("FIELDS")
'Search Table
oParam1.Value = "LIKP" ' my table in SAP
'Search Fields
oParam2.Rows.Add
oParam2.Value(1, "FIELDNAME") = "VBELN" 'Delivery
oParam2.Rows.Add
oParam2.Value(2, "FIELDNAME") = "VSTEL" ' Shpt
oParam2.Rows.Add
oParam2.Value(3, "FIELDNAME") = "KUNNR" ' Ship-to
oParam2.Rows.Add
oParam2.Value(4, "FIELDNAME") = "CMWAE" ' Curr.
'add select condition
oParam3.Rows.Add
oParam3.Value(1, "TEXT") = "VBELN ='00" & ThisWorkbook.Worksheets(2).Cells(1, 1) & "'"
'seprate fields by comma
oParam4.Value = "^"
'call RFC
Result = MyFunc.Call
Set Data = MyFunc.Tables("DATA")
Set Fields = MyFunc.Tables("FIELDS")
nFields = Fields.RowCount
nData = Data.RowCount
For iField = 1 To nFields
'Cells(1, iField) = Fields(iField, "FIELDTEXT")
If iField = 1 Then ThisWorkbook.Worksheets(1).Cells(1, iField) = "Delivery"
If iField = 2 Then ThisWorkbook.Worksheets(1).Cells(1, iField) = "Ship From"
If iField = 3 Then ThisWorkbook.Worksheets(1).Cells(1, iField) = "Ship-to"
If iField = 4 Then ThisWorkbook.Worksheets(1).Cells(1, iField) = "Curr."
Next
For iData = 1 To nData
vRow = Split(Data(iData, 1), "^")
ThisWorkbook.Worksheets(1).Cells(iData + 1, 1) = vRow(0)
ThisWorkbook.Worksheets(1).Cells(iData + 1, 2) = vRow(1)
ThisWorkbook.Worksheets(1).Cells(iData + 1, 3) = vRow(2)
ThisWorkbook.Worksheets(1).Cells(iData + 1, 4) = vRow(3)
Next
End Function
|
|