Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Calling RFC (remote enabled function module) through excel VBA in sap

sanjana_lingras
Active Participant
0 Kudos

Hi Team,

I need to call Remote function module through excel for data validation on click of command button.

I am getting errors such as "user defined type not defined" for SAPFunctionsOCX.SAPFunctions.

Can anybody help me in this? Or is there any complete docuement for this procedure to call RFC through excel VBA?

Thanks,

Sanjana

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Please find sample VB script code and make changes as per needed.


Sub Button3_Click()

' Access Active X textbox value = ActiveSheet.OLEObjects("TextBox1").Object.Value

Dim Destination_System As Integer

Dim objBAPIControl As Object 'Function Control (Collective object)

Dim sapConnection As Object 'Connection object

Set objBAPIControl = CreateObject("SAP.Functions")

Set sapConnection = objBAPIControl.Connection

Dim valu As String

sapConnection.client = "client"

sapConnection.user = "username"

sapConnection.Language = "E"

sapConnection.hostname = "ip address of sap server"

sapConnection.Password = "password"

sapConnection.SystemNumber = "instance number"

sapConnection.System = "system ID"

If sapConnection.logon(1, True) <> True Then

MsgBox "No connection to R/3!"

Exit Sub 'End program

End If

Set objUserList = objBAPIControl.Add("BAPI_SALESORDER_GETLIST")

Worksheets(2).Select

Cells.Clear

Range("A1").Font.Italic = True

Range("A2:E2").Font.Bold = True

ActiveSheet.Cells(2, 1) = "SO Number"

ActiveSheet.Cells(2, 2) = "Item No"

ActiveSheet.Cells(2, 3) = "Material No"

ActiveSheet.Cells(2, 4) = "Text"

ActiveSheet.Cells(2, 5) = "Req Qty"

Worksheets(1).Select

valu = ActiveSheet.Cells(1, 2)

Worksheets(2).Select

valu = "0000" & valu

objUserList.exports("CUSTOMER_NUMBER") = valu

returnFunc = objUserList.Call

If returnFunc = True Then

Dim objTable As Object

Set objTable = objUserList.Tables("SALES_ORDERS")

For i = 1 To objTable.RowCount

ActiveSheet.Cells(2 + i, 1) = objTable.Cell(i, 1)

ActiveSheet.Cells(2 + i, 2) = objTable.Cell(i, 2)

ActiveSheet.Cells(2 + i, 3) = objTable.Cell(i, 3)

ActiveSheet.Cells(2 + i, 4) = objTable.Cell(i, 4)

ActiveSheet.Cells(2 + i, 5) = objTable.Cell(i, 7)

Next i

End If

End Sub

if you have installed SAP gui there there would no require other files such as

OCX.SAPFunctions etc..

Excel Ref Image.

-Avirat

5 REPLIES 5

Former Member
0 Kudos

This message was moderated.

0 Kudos

Hi All,

Issue is not resolved yet. Can anybody help or is there any complete sample VBA code with steps to connect to SAP through Excel ad call RFC.

Regards,

Sanjana

0 Kudos

This message was moderated.

0 Kudos

This message was moderated.

Former Member
0 Kudos

Hi,

Please find sample VB script code and make changes as per needed.


Sub Button3_Click()

' Access Active X textbox value = ActiveSheet.OLEObjects("TextBox1").Object.Value

Dim Destination_System As Integer

Dim objBAPIControl As Object 'Function Control (Collective object)

Dim sapConnection As Object 'Connection object

Set objBAPIControl = CreateObject("SAP.Functions")

Set sapConnection = objBAPIControl.Connection

Dim valu As String

sapConnection.client = "client"

sapConnection.user = "username"

sapConnection.Language = "E"

sapConnection.hostname = "ip address of sap server"

sapConnection.Password = "password"

sapConnection.SystemNumber = "instance number"

sapConnection.System = "system ID"

If sapConnection.logon(1, True) <> True Then

MsgBox "No connection to R/3!"

Exit Sub 'End program

End If

Set objUserList = objBAPIControl.Add("BAPI_SALESORDER_GETLIST")

Worksheets(2).Select

Cells.Clear

Range("A1").Font.Italic = True

Range("A2:E2").Font.Bold = True

ActiveSheet.Cells(2, 1) = "SO Number"

ActiveSheet.Cells(2, 2) = "Item No"

ActiveSheet.Cells(2, 3) = "Material No"

ActiveSheet.Cells(2, 4) = "Text"

ActiveSheet.Cells(2, 5) = "Req Qty"

Worksheets(1).Select

valu = ActiveSheet.Cells(1, 2)

Worksheets(2).Select

valu = "0000" & valu

objUserList.exports("CUSTOMER_NUMBER") = valu

returnFunc = objUserList.Call

If returnFunc = True Then

Dim objTable As Object

Set objTable = objUserList.Tables("SALES_ORDERS")

For i = 1 To objTable.RowCount

ActiveSheet.Cells(2 + i, 1) = objTable.Cell(i, 1)

ActiveSheet.Cells(2 + i, 2) = objTable.Cell(i, 2)

ActiveSheet.Cells(2 + i, 3) = objTable.Cell(i, 3)

ActiveSheet.Cells(2 + i, 4) = objTable.Cell(i, 4)

ActiveSheet.Cells(2 + i, 5) = objTable.Cell(i, 7)

Next i

End If

End Sub

if you have installed SAP gui there there would no require other files such as

OCX.SAPFunctions etc..

Excel Ref Image.

-Avirat