我覺得要搞懂sap不容易
程式方面除的要懂ABAP
要了解JAVA
有時候還要了解VBA
以下是我最近測到的一個作法
可以透過Excel的vba巨集語法讀取SAP資料及執行SAP程式
讀出sap資料後在結合Excel的相關圖表或計算式功能滿強大的
可以作到很多效果
Sub sap_conn()
Dim Functions As Object
Dim func As Object
Dim iTable As Object
'首先要先連線SAP打上用戶端語言帳號密碼跟連線IP
Set oFunction = CreateObject("SAP.LogonControl.1")
Set oConnection = oFunction.NewConnection
oConnection.client = "060" '設定用戶端
oConnection.Language = "zh" '設定語系
oConnection.user = "user" '設定帳號密碼
oConnection.Password = "pass"
oConnection.ApplicationServer = DEV '設定主機IP
oConnection.SystemNumber = "00"
result = oConnection.Logon(0, True)
Set ofun = CreateObject("SAP.FUNCTIONS")
Set ofun.Connection = oConnection
'看要讀取SAP中哪一個BAPI或自訂的FUNCTION,以下的例子是寫一個FUNCTION可以傳回
'某文件日期區間的所有訂單資料
Set func = ofun.Add("ZSDORDER01")
'==對該function下的傳遞參數
func.Exports("AUDATLOW") = "20091001"
func.Exports("AUDATHIGH") = "20091031"
If func.Call = True Then '==設定要傳回的ITAB
Set iTable = func.Tables("VBAK")
get_data iTable '==傳回得到的資料
MsgBox "Get Data OK!"
Else
MsgBox " Call Failed! error: " '+ func.Exception
End If
End Sub
Public Sub get_data(itabtable As Object)
Dim vField As Variant
Static j As Integer
For j = 1 To itabtable.RowCount
ThisWorkbook.Sheets(1).Cells(j, 1) = itabtable.Value(j, "VBELN") '讀取的欄位資料寫回活頁
ThisWorkbook.Sheets(1).Cells(j, 2) = itabtable.Value(j, "KUNNR")
Next
End Sub