简体   繁体   English

从 VBA 登录 SAP 期间没有功能模块 RFC PING 的 RFC 授权

[英]No RFC authorization for function module RFC PING during SAP logon from VBA

Good morning, everybody!大家早上好!

I've been looking for the solution in the last days but I really have not managed to succeed: I am trying to make a VBA code to:最近几天我一直在寻找解决方案,但我真的没有成功:我正在尝试制作一个 VBA 代码:

  1. log into SAP,登录 SAP,
  2. run some transactions,运行一些事务,
  3. export to excel.导出到excel。

But even the "log into SAP" part is not OK!但即使是“登录 SAP”部分也不行!

I tried several codes, the one below OPENS the SAP logon screen, but does not fill in any fields.我尝试了几个代码,下面的一个打开 SAP 登录屏幕,但没有填写任何字段。

In the first attempt, I Used CreateObject("Sapgui.ScriptingCtrl.1") :在第一次尝试中,我使用了CreateObject("Sapgui.ScriptingCtrl.1")

Sub Entrar_SAP()

If Not IsObject(SAPguiApp) Then
    Set SAPguiApp = CreateObject("Sapgui.ScriptingCtrl.1")
End If
If Not IsObject(Connection) Then
    Set Connection = SAPguiApp.OpenConnection("xxxxxxx)", True)
End If
If Not IsObject(session) Then
    Set session = Connection.Children(0)
End If
session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "100"     
session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "user"     
session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "pass" 
session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "PT" 
session.findById("wnd[0]/usr/txtRSYST-LANGU").SetFocus     
session.findById("wnd[0]/usr/txtRSYST-LANGU").caretPosition = 2 
session.findById("wnd[0]").sendVKey 0

In the second attempt, I tried CreateObject("SAP.Functions") , it showed:在第二次尝试中,我尝试了CreateObject("SAP.Functions") ,它显示:

"RFC error received. No RFC authorization for function module RFC PING" “收到 RFC 错误。功能模块 RFC PING 没有 RFC 授权”

The code is:代码是:

'Declaration
Dim objBAPIControl As Object 'Function Control (Collective object)
Dim sapConnection As Object 'Connection object
Set objBAPIControl = CreateObject("SAP.Functions")
Set sapConnection = objBAPIControl.Connection
sapConnection.Client = "xxxxx" 
sapConnection.User = "xxxxxx"
sapConnection.Language = "PT" 
sapConnection.hostname = "xxxxx"
sapConnection.Password = "xxxxxxxx" 'Fake password         
sapConnection.SystemNumber = "4"
sapConnection.System = "xxxxxx)"
sapConnection.Logon 
If sapConnection.Logon(1, True) <> True Then
    MsgBox "No connection to R/3!"
Exit Sub 'End program 
End If

Can someone please help me?有人可以帮帮我吗? Thanks!!谢谢!!

First of all, RFC is a perfectly fine method for interacting with SAP.首先,RFC 是一种非常好的与 SAP 交互的方法。 It's not out of support.这不是不支持。

Second, you don't have enough authorization so your code will not work even if you get the syntax right.其次,您没有足够的授权,因此即使您的语法正确,您的代码也无法运行。 "RFC error received. No RFC authorization for function module RFC PING". “收到 RFC 错误。功能模块 RFC PING 没有 RFC 授权”。 Ask your SAP team to give you access to execute RFCs remotely.请您的 SAP 团队授予您远程执行 RFC 的权限。 Ask for SAP_S_RFCACL .请求SAP_S_RFCACL

On a side note, your main object of running some transactions and exporting to Excel is quite easy to do in SAP.附带说明一下,运行一些事务并导出到 Excel 的主要对象在 SAP 中很容易完成。 Maybe you should just ask your SAP team to do it for you instead of developing it in VBA?也许你应该让你的 SAP 团队为你做这件事,而不是在 VBA 中开发它?

I assume your pulling via an RFC read Table.我假设您通过 RFC 读取表进行拉取。 This Connection will work fine for those.这个连接对那些人来说很好用。

Dim LogonControl As Object
Dim conn As Object
Dim retcd As Boolean

Set LogonControl = CreateObject("SAP.LogonControl.1")
   Set conn = LogonControl.NewConnection
conn.System = "strSystemName"
conn.Client = "100"
conn.Language = "EN"
conn.User = "sUserName"
conn.Password = "strPassword"
retcd = conn.Logon(0, True) 'True = No logon GUI 'False = logon GUI

If retcd <> True Then
    MsgBox "Login failed for- " & strSystemName & " -UserName or Password are incorrect, check them and run try again ."
    Exit Sub
End If
 Set funcControl = CreateObject("SAP.Functions")
 funcControl.Connection = conn

From this Point on you can make your RFC call without any issues.从现在开始,您可以毫无问题地进行 RFC 调用。

But to be truthful though, Above is almost exactly what you have as your second example.但说实话,上面几乎就是你作为第二个例子所拥有的。 your RFC Error your getting seems like you don't have security settings for SAP to make RFC calls to whatever table your pulling from and not a problem with your login code.您收到的 RFC 错误似乎没有 SAP 的安全设置,无法对您从中提取的任何表进行 RFC 调用,而您的登录代码没有问题。

Disclaimer : RFC_READ_TABLE is NOT supported by SAP and is more of a backdoor then a day to day method for pulling data.免责声明:RFC_READ_TABLE不是由SAP支持,更多的是一种后门程序,然后进行日常方法提取数据的。

Edit1: To Cover the Comments and not turn this into a discussion I will try and Summarize them here.编辑 1:为了覆盖评论而不是将其变成讨论,我将尝试在这里总结它们。

Firstly首先

the pop-up: If you want the pop-up for the log in then you need to change this line of code弹窗:如果你想要登录的弹窗,那么你需要改变这行代码

retcd = conn.Logon(0, True) 

to

retcd = conn.Logon(0, False) 'This one DISPLAYS the pop-up

Secondly其次

The Permissions: RFC_Read_Table uses Very Different Security Settings then a SAP t-Code uses, The technical Difference is difficult to explain but for a rule of thumb, If you cant access the SAP Table (t-Code SE16) you most likely not be able to pull it from RFC Read Table权限:RFC_Read_Table 使用非常不同的安全设置,然后使用 SAP t-Code,技术差异很难解释,但根据经验,如果您无法访问 SAP 表(t-Code SE16),您很可能无法访问从 RFC 读取表中提取它

Thirdly第三

If your company has Multiple SAP boxes (DEV, production, test) the Systemname would be EXACTLY what shows up on the box selection screen of SAP under name.如果你的公司有多个SAP盒(DEV,生产,试验)的SYSTEMNAME会正是SAP的名义下框选屏幕上显示出来。 assuming you were getting an RFC error from your second code block then the box name you used in that code would be the correct one.假设您从第二个代码块中收到 RFC 错误,那么您在该代码中使用的框名称将是正确的。

You can bypass RFC controls and just go for a normal scripting that imitates a human user and manually introduces username and password.您可以绕过 RFC 控制,只需使用模拟人类用户并手动引入用户名和密码的普通脚本即可。 Credit to The Script Man from the SAP forums : 归功于 SAP 论坛中的脚本人

 Sub SapLogin()
'Logs onto SAP system

Dim SapGuiApp As Object
Dim oConnection As Object
Dim session As Object
Dim SAPCon As Object, SAPSesi As Object
Dim SAPGUIAuto As Object, SAPApp As Object
Dim system As String

system = "XX" 'SAP system you will log on to like "01. ENGINEERING PRODUCTION [EG1]

If SapGuiApp Is Nothing Then
    Set SapGuiApp = CreateObject("Sapgui.ScriptingCtrl.1")
End If
If oConnection Is Nothing Then
    Set oConnection = SapGuiApp.OpenConnection(system, True)
End If
If SAPSesi Is Nothing Then
   Set SAPSesi = oConnection.Children(0)
End If

    Application.DisplayAlerts = FALSE

 With SAPSesi

    .FindById("wnd[0]/usr/txtRSYST-MANDT").Text = "100"
    .FindById("wnd[0]/usr/txtRSYST-BNAME").Text = "USERNAME"
    .FindById("wnd[0]/usr/pwdRSYST-BCODE").Text = "PASSWORD"
    .FindById("wnd[0]/usr/txtRSYST-LANGU").Text = "EN"
    .FindById("wnd[0]").SendVKey 0

    'start extraction

    .FindById("wnd[0]").Maximize
    .FindById("wnd[0]/tbar[0]/okcd").Text = "/TCODEYOUWANTTORUN"
    .FindById("wnd[0]").SendVKey 0

    '...
    'etc
    '...

    End With

     Application.DisplayAlerts = True
     MsgBox "After clicking OK, this SAP session is terminated."
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM