Соединение с SAP из Excel

Сначала создайте в Экселе соответствующие кнопки, потом скопируйте на Код листа:
http://sap.ittoolbox.com/groups/technical-functional/sap-dev/sap-rfc-vba...

Dim LogonControl
Dim conn
Dim funcControl
Dim TableFactoryCtrl
Dim RFC_READ_TABLE
Dim eQUERY_TAB
Dim TOPTIONS
Dim TDATA
Dim TFIELDS

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Set LogonControl = CreateObject("SAP.LogonControl.1")
Set funcControl = CreateObject("SAP.Functions")
Set conn = LogonControl.NewConnection

conn.System = "10. HRD - ( ñèñòåìà ðàçðàáîòêè HR)" ' System ID istanza,di solito 0
conn.Client = "128" ' Client per logon to
conn.Language = "RU" ' Lingua logon
conn.User = "" ' User ID
conn.Password = "" ' Password
retcd = conn.Logon(0, False)
If retcd True Then
MsgBox " Cannot log on! "
MsgBox retcd
Stop
Else
MsgBox " Logon OK."
End If
funcControl.Connection = conn
End Sub

Private Sub Command2_Click()
conn.logoff
End Sub
Private Sub Command3_Click()
Const attachpath = "C:\Query.CSV"

Set objFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set filOutput = objFileSystemObject.CreateTextFile(attachpath, True)

Set RFC_READ_TABLE = funcControl.Add("RFC_READ_TABLE")
Set strExport1 = RFC_READ_TABLE.Exports("QUERY_TABLE")
Set strExport2 = RFC_READ_TABLE.Exports("DELIMITER")
Set tblOptions = RFC_READ_TABLE.Tables("OPTIONS")
Set tblData = RFC_READ_TABLE.Tables("DATA")
Set tblFields = RFC_READ_TABLE.Tables("FIELDS")

strExport1.Value = "MARC"
strExport2.Value = ","

' (EQ means =, LT means )
tblOptions.AppendRow
tblOptions(1, "TEXT") = "WERKS EQ '" & InputBox("DIV ") & "'"

tblFields.AppendRow
tblFields(1, "FIELDNAME") = "MATNR"

'tblFields.AppendRow
'tblFields(2, "FIELDNAME") = "WERKS"

'tblFields.AppendRow
'tblFields(3, "FIELDNAME") = "NAME2"

'tblFields.AppendRow
'tblFields(4, "FIELDNAME") = "NAME3"

'tblFields.AppendRow
'tblFields(5, "FIELDNAME") = "NAME4"

If RFC_READ_TABLE.Call = True Then
If tblData.RowCount > 0 Then
' INTESTAZIONI
filOutput.WriteLine "MAT,DIV"
For intRow = 1 To tblData.RowCount
filOutput.WriteLine tblData(intRow, "WA")
Next
MsgBox "COMPLETED SUCCESSFULLY"
Else
MsgBox "No records returned"
End If
Else
MsgBox "ERROR CALLING SAP REMOTE FUNCTION CALL"
End If
End Sub

Комментарии