ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 用VB读取SAP RFC的structure类型输出参数

用VB读取SAP RFC的structure类型输出参数

原创 Linux操作系统 作者:wangyuzhen 时间:2012-09-22 16:17:00 0 删除 编辑
近日有个ABAPer问我某个第三方系统没法传结构参数给SAP的RFC,是不是无解?这个问题其实在我心里也纠结了很久了,别说传给RFC,就算是从RFC返回的结构型参数都没法读取。
幸好有谷歌,把那些关键字排列组合的搜,多看看。终于看到一个有用的日志,居然还是ITPUB的空间里。万能的PUB啊!!!

尊重作者,原文地址:
使用VB通过RFC连接并处理SAP数据例子

这个例子里侧重于写入RFC的结构性参数,我疑问的读取还没有。自己依葫芦画瓢的在Excel的VBScript里弄了一把。
直接看调用RFC,定义参数及回传部分。其他,像连接系统啥的代码网上多的是。
================================================================
    ' call RFC function
    Dim poheader As Object
    Dim poitems As Object
    Dim poitemschedule As Object
    Dim ZData As Object
    Dim i, j As Integer
   
    ' 通过RFC接口远程运行SAP内部函数ZGET_ASSET_NUMBER
    Set theFunction = sapFunctionCtrl.Add("BAPI_PO_GETDETAIL") ' 赋要调用的SAP内建函数名
  
    theFunction.Exports("PURCHASEORDER") = Sheet1.Cells(1, 2) ' 测试用采购订单号,去sheet1的1行2列单元格数据
    'theFunction.exports("PURCHASEORDER") = "4800001870" ' 生产机备件采购订单号
   
    'theFunction.Exports("PO_REL_CODE") = "R3" ' 向函数入口赋值
    'theFunction.Exports("USE_EXCEPTIONS") = "X" ' 向函数入口赋值
    'theFunction.Exports("NO_COMMIT") = "X" ' 向函数入口赋值
   
    ' 向函数入口赋查询表名称
    Set poheader = theFunction.Imports.Item("PO_HEADER")
    Set poitems = theFunction.Tables.Item("PO_ITEMS")
    'Set ZData = TheFunction.Imports("IT_ZASSET_TAB")
   
    If (theFunction.Call = True) Then    ' 调用成功遍历显示所有信息条目
      'Sheet1.Cells(1, 1) = theFunction.IMports("REL_STATUS_NEW")
      Sheet1.Cells(2, 1) = poheader.Value("PO_NUMBER")
      Sheet1.Cells(2, 2) = poheader.Value("CO_CODE")
      Sheet1.Cells(2, 3) = poheader.Value("DOC_TYPE")
      Sheet1.Cells(2, 4) = poheader.Value("CREATED_ON")
      Sheet1.Cells(2, 5) = poheader.Value("VENDOR")
      For i = 1 To poitems.RowCount
        j = i + 3
        Sheet1.Cells(j, 1) = poitems(i, "PO_ITEM")
        Sheet1.Cells(j, 2) = poitems(i, "MATERIAL")
        Sheet1.Cells(j, 3) = poitems(i, "SHORT_TEXT")
        Sheet1.Cells(j, 4) = poitems(i, "PLANT")
        Sheet1.Cells(j, 5) = poitems(i, "QUANTITY")
      Next i
   
      'Set ZData = theFunction.Tables("PO_ITEM_TEXTS")
      'For i = 1 To ZData.RowCount
      '
      '  j = i + 1
      '  Sheet1.Cells(j, 1) = ZData(i, "PO_NUMBER")
      '  Sheet1.Cells(j, 2) = ZData(i, "PO_ITEM")
      '  Sheet1.Cells(j, 3) = ZData(i, "TEXT_ID")
      '  Sheet1.Cells(j, 4) = ZData(i, "TEXT_FORM")
      '  Sheet1.Cells(j, 5) = ZData(i, "TEXT_LINE")
      'Next i
    Else
        MsgBox "No date OR Error in R/3 " + theFunction.Exception
    End If


================================================================

上面代码中,ZData是取返回表参数的另外一种写法,以前用过也是可以的。

下面是原帖中写入RFC结构型参数的代码,我抄过来一下,还未验证
=============================================================
Option Explicit
Dim functionCtrl As Object
Dim sapConnection As Object
Dim theFunc As Object
Dim PoNumber

Private Sub Command1_Click()

Set functionCtrl = CreateObject("SAP.Functions")
Set sapConnection = functionCtrl.Connection
sapConnection.Client = "500"
sapConnection.user = "USERNAME"
sapConnection.password = "PASSWORD"
sapConnection.Language = "EN"

If sapConnection.logon(0, False) True Then
MsgBox "No connection to R/3 System"
Exit Sub 'End program
End If
Set theFunc = functionCtrl.Add("BAPI_PO_CREATE")


Dim poheader As Object
Dim poitems As Object
Dim poitemschedule As Object
Dim retMess As Object
Dim returnFunc As Boolean
Dim startzeil As Integer
Dim endcol As Integer
Dim the_name As String

Set poheader = theFunc.exports.Item("PO_HEADER")
Set poitems = theFunc.tables.Item("PO_ITEMS")
Set poitemschedule = theFunc.tables.Item("PO_ITEM_SCHEDULES")

poheader.Value("VENDOR") = Text1.Text

poheader.Value("PURCH_ORG") = Text2.Text
poheader.Value("PUR_GROUP") = Text3.Text
poheader.Value("DOC_TYPE") = Text4.Text

poitems.Rows.Add
poitems.Value(1, "PUR_MAT") = Text5.Text
poitems.Value(1, "PLANT") = Text6.Text
poitems.Value(1, "NET_PRICE") = Text7.Text


poitemschedule.Rows.Add
poitemschedule.Value(1, "DELIV_DATE") = Text8.Text
poitemschedule.Value(1, "QUANTITY") = Text9.Text

returnFunc = theFunc.call
PoNumber = theFunc.imports("PURCHASEORDER")
Set retMess = theFunc.tables.Item("RETURN")
If retMess Is Nothing Then

MsgBox retMess.Value(1, "MESSAGE")
Else
MsgBox "Purchase Order No : " & PoNumber & "Created"
End If

End Sub


=============================================================

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/1605/viewspace-744813/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-12-11

  • 博文量
    64
  • 访问量
    944265