SAP RFC without explicit logon

bconforto

New Member
Joined
Nov 17, 2016
Messages
30
Hello,
I am running a macro that creates documents in SAP, so the user is already logged on in the system.

I want to call a RFC. I have this code that is already working, but my question is, as the user is already logged on, do I always need to make this logon again?
Is it possible to take credentials from active SAP Session or similar?
If not, each time my script saves a document and RFC is called, another loggon will happen.

Code:
Public Sub RFC_Read_Table()Dim Functions As Object
Dim RfcCallTransaction As Object
Dim Messages As Object
Dim BdcTable As Object
Dim tblOptions
Dim tblData
Dim tblFields
Dim strExport1
Dim strExport2
Dim filOutput
Dim intRow As Integer
Dim conn As Object
Dim sap As Object
 

Set Functions = CreateObject("SAP.Functions")
Functions.Connection.System = "RR1"
Functions.Connection.client = "010"
Functions.Connection.user = "confob1"
Functions.Connection.Password = "xxx"
Functions.Connection.Language = "EN"

If Functions.Connection.logon(0, True) <> True Then
Exit Sub
End If

Set RfcCallTransaction = Functions.Add("RFC_READ_TABLE")
Set strExport1 = RfcCallTransaction.exports("QUERY_TABLE")
Set strExport2 = RfcCallTransaction.exports("DELIMITER")
Set tblOptions = RfcCallTransaction.Tables("OPTIONS") '
Set tblData = RfcCallTransaction.Tables("DATA") '
Set tblFields = RfcCallTransaction.Tables("FIELDS") '
 
strExport1.Value = "VBFA"
strExport2.Value = ";"
tblOptions.AppendRow
tblOptions(1, "TEXT") = "VBELV EQ '0050015909' and VBTYP_N EQ 'I' "
tblOptions.AppendRow
 
tblFields.AppendRow
tblFields(1, "FIELDNAME") = "VBELN"

If RfcCallTransaction.Call = True Then
If tblData.RowCount > 0 Then
For intRow = 1 To tblData.RowCount
OutputTxt1 = tblData(intRow, "WA")
Next

Else
MsgBox "No records returned"
End If
Else
MsgBox "Error"
End If
Set filOutput = Nothing
Functions.Connection.Logoff
End Sub

Regards,
Brian
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi,

My SAP knowledge is getting a bit hazy now so others may well have better answers.

However, the reason you need to logon each time is because you logoff at the end. If you don't do that then you will remain logged on. Perhaps a UserForm or MsgBox to ask if you want to go round the loop again might be added to pause the processing between runs?

One thing to bear in mind is, if you could re-use a connection, what would happen if the user is logged in to two different SAP sessions, one on the DEV box and one on the PROD box, say? Which connection would you expect it to make?

Also, you are quite lucky to have RFC_READ_TABLE available because it is a way to circumvent the usual user profiles set up to restrict access to various parts of the system.

It would be safer to use an appropriate BAPI because that will ensure that the authorization profiles are complied with.

Regards,
 
Upvote 0
Thanks RickXL for your reply.

This script is designed for end users that only have access to Production.
When it starts, it takes active user session to process document creation.

Code:
Sub Main()If Not IsObject(SAP_Application) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set SAP_Application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = SAP_Application.Children(0)
End If
If Not IsObject(session) Then
   Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject session, "on"
   WScript.ConnectObject SAP_Application, "on"
End If
Set xclsht = ActiveWorkbook.ActiveSheet
session.findById("wnd[0]").maximize

....

I mean, Is it neccesary to do the loggon just before running RFC? I tried commenting those lines of code but then it is not working.

Regards,
Brian
 
Upvote 0
Unfortunately, (maybe that should be fortunately!) I no longer have access to an SAP system. So I can't try anything out.

However, there are ways in VBA to locate applications that are already running. For instance, this will link to a current Outlook session:
Code:
Dim olApp As Outlook.Application

On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
On Error Goto 0 

If olApp Is Nothing Then
    Set olApp = CreateObject("Outlook.Application")
End If

If olApp Is Nothing Then
    Err.Raise 999, , "Failed to get Outlook reference"
End If
However, with SAP you always set up the Application, Connection and Session Objects so it may be more difficult than that.

If the code is for end-users then, if I were your Security Administrator, I would not allow you to distribute it. In fact, I would have prevented access to the whole Function Group (SDTX, if memory serves). The issue is that someone who is computer literate could modify the code to dump everyone's Personnel Numbers and Salaries just by making a few small changes - or any other sensitive information e.g. Finance.

Regards,
<code>

</code>
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,692
Members
449,117
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top