ITPub博客

首页 > 数据库 > 数据库开发技术 > 用vba连接sap

用vba连接sap

原创 数据库开发技术 作者:yangjulei 时间:2014-05-12 10:10:17 0 删除 编辑
sample code:
  Dim iData  As Integer
  Dim nField As Integer
  Dim nData  As Integer
  Dim Result As Boolean
  Dim vRow   As Variant
  
  MsgBox "Execution Start"
  
    Set R3 = CreateObject("SAP.Functions")
    R3.Connection.System = "XXX" '此处为系统标示
    R3.Connection.SystemNumber = "XX" '系统编号
    R3.Connection.client = "500"
    R3.Connection.User = "ID"
    R3.Connection.Password = "PWD"
    R3.Connection.Language = "EN"
    R3.Connection.ApplicationServer = "hostname"
    
    If R3.Connection.logon(0, True) = True Then
       MsgBox "Connection Succefully"
    Else
      MsgBox "Connection Failed"
    End If
    
    'connect to SAP
    'call function
    Set MyFunc = R3.Add("RFC_READ_TABLE")


    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 = "T001W" ' my table in SAP
    'Search Fields
    oParam2.Rows.Add
    oParam2.Value(1, "FIELDNAME") = "WERKS"  'plant
    oParam2.Rows.Add
    oParam2.Value(2, "FIELDNAME") = "NAME1" ' name
    'add select condition
    oParam3.Rows.Add
    oParam3.Value(1, "TEXT") = "WERKS = '2000' OR WERKS = '1000'"
    '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")
    Next
    
    For iData = 1 To nData
        vRow = Split(Data(iData, 1), ",")
        Cells(iData + 1, 1) = vRow(0)
        Cells(iData + 1, 2) = vRow(1)
    Next

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

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

注册时间:2010-03-08

  • 博文量
    72
  • 访问量
    229973