VBA调用SAP RFC出错

Sub sapMARC()
Dim iData As Integer
Dim nField As Integer
Dim nData As Integer
Dim Result As Boolean
Dim vRow As Variant
MsgBox “Program Start!”
Set R3 = CreateObject(“SAP.Functions”)
R3.Connection.System = “KP1”
R3.Connection.ApplicationServer = SapApplicationServer
R3.Connection.Client = SapCient
R3.Connection.SystemNumber = SapSystemNumer
R3.Connection.User = sapuser
R3.Connection.Password = sappass
R3.Connection.Language = SapLanguage

If R3.Connection.logon(0, False) <> True Then
Exit Sub
End If
‘-------------------------------------------------------
With ThisWorkbook.Sheets(“Program”)
Lrow = .Range(“a65536”).End(xlUp).Row
brr = Application.Transpose(.Range(“A7:A” & .Range(“A65536”).End(xlUp).Row))
If IsArray(brr) Then
For s = 1 To UBound(brr)
brr(s) = "’" & brr(s) & “’”
Next s
optin = Join(brr, “,”)
Else
optin = “’” & brr & “’”
End If
.Cells(11, 10) = optin
End With
'---------------------------------------------------------
Set RFC = R3.Add(“RFC_READ_TABLE”)
'Print
Set it_fields = RFC.Tables(“FIELDS”)
Set it_options = RFC.Tables(“OPTIONS”)
Set it_data = RFC.Tables(“DATA”)

RFC.exports(“QUERY_TABLE”).Value = “MARC” ’ my table in SAP

With ThisWorkbook.Sheets(“Parts info”)
it_fields.Rows.Add
it_fields.Value(1, “FIELDNAME”) = “MATNR”
it_fields.Rows.Add
it_fields.Value(2, “FIELDNAME”) = “WERKS”
it_fields.Rows.Add
it_fields.Value(3, “FIELDNAME”) = “MMSTA”
it_fields.Rows.Add
it_fields.Value(4, “FIELDNAME”) = “EKGRP”
it_fields.Rows.Add
it_fields.Value(5, “FIELDNAME”) = “DISMM”
it_fields.Rows.Add
it_fields.Value(6, “FIELDNAME”) = “DISPO”
it_fields.Rows.Add
it_fields.Value(7, “FIELDNAME”) = “PLIFZ”
it_fields.Rows.Add
it_fields.Value(8, “FIELDNAME”) = “DISLS”
it_fields.Rows.Add
it_fields.Value(9, “FIELDNAME”) = “BESKZ”
it_fields.Rows.Add
it_fields.Value(10, “FIELDNAME”) = “SOBSL”
it_fields.Rows.Add
it_fields.Value(11, “FIELDNAME”) = “MINBE”
it_fields.Rows.Add
it_fields.Value(12, “FIELDNAME”) = “BSTMI”
it_fields.Rows.Add
it_fields.Value(13, “FIELDNAME”) = “BSTRF”
it_fields.Rows.Add
it_fields.Value(14, “FIELDNAME”) = “MTVFP”
it_fields.Rows.Add
it_fields.Value(15, “FIELDNAME”) = “STRGR”
'it_fields.Rows.Add
'it_fields.Value(3, “FIELDNAME”) = “MAKTX”
Set it_options = RFC.Tables(“OPTIONS”)
it_options.Rows.Add
it_options.Value(1, “TEXT”) = “MATNR in (” & optin & “)”
it_options.Rows.Add
it_options.Value(2, “TEXT”) = “AND WERKS =‘8701’”
RFC.exports(“DELIMITER”).Value = “,”
Result = RFC.Call
.UsedRange.Clear
nFields = it_fields.RowCount
nData = it_data.RowCount
For iField = 1 To nFields
.Cells(1, iField) = it_fields.Rows(iField).Value(“FIELDTEXT”)
Next
.Columns(“a:a”).NumberFormatLocal = “@”
For iData = 1 To nData
For j = 1 To 15
n = j - 1
vRow = Split(it_data(iData, 1), “,”)
.Cells(iData + 1, j) = vRow(n)
Next
Next
.Activate
End With
R3.Connection.LOGOFF
MsgBox “Program End!”
End Sub

一部分代码摘自 小懒_lan 博主的代码,我把MATNR(物料)设置为多颗料【it_options.Value(1, “TEXT”) = “MATNR in (” & optin & “)”】,现在遇到的问题是一旦料号超过4个,result就返回False,有木有博友知道问题的原因,帮忙看下怎么修改0.0#!
[1]: http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference
[2]: https://mermaidjs.github.io/
[3]: https://mermaidjs.github.io/
[4]: http://adrai.github.io/flowchart.js/

你可能感兴趣的:(SAP)