VBA调用SAP RFC出错

疯狂的国度~zZ 2019-06-12 07:14:14
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#!
...全文
103 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

2,462

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧