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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30983234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:42:22  来源:igfitidea点击:

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

vbasapsaprfc

提问by Eduardo Danon

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: 1-log into SAP, 2-run some transactions 3-export to excel.

最近几天我一直在寻找解决方案,但我真的没有成功:我正在尝试制作一个 vba 代码:1-登录到 SAP,2-运行一些事务 3-导出到 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. I Used CreateObject("Sapgui.ScriptingCtrl.1"):

我尝试了几个代码,下面的一个打开 SAP 登录屏幕,但没有填写任何字段。我用过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

ANother one, with CreateObject("SAP.Functions"), showed: "RFC error received. No RFC authorization for function module RFC PING"

另一个CreateObject("SAP.Functions")显示为:“收到 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!!

有人可以帮帮我吗?谢谢!!

回答by Mikael G

First of all, RFC is a perfectly fine method for interacting with SAP. It's not out of support.

首先,RFC 是一种非常好的与 SAP 交互的方法。这不是不支持。

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".Ask your SAP team to give you access to execute RFCs remotely. Ask for SAP_S_RFCACL.

其次,您没有足够的授权,因此即使您的语法正确,您的代码也无法运行。“收到 RFC 错误。功能模块 RFC PING 没有 RFC 授权”。请您的 SAP 团队授予您远程执行 RFC 的权限。请求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. Maybe you should just ask your SAP team to do it for you instead of developing it in VBA?

附带说明一下,运行一些事务并导出到 Excel 的主要对象在 SAP 中很容易完成。也许你应该让你的 SAP 团队为你做这件事,而不是在 VBA 中开发它?

回答by Pedro Martinez

You can bypass RFC controls and just go for a normal scripting that imitates a human user and manually introduces username and password. Credit to The Script Man from the SAP forums:

您可以绕过 RFC 控制,只需使用模拟人类用户并手动引入用户名和密码的普通脚本即可。归功于 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

回答by Jimmy Jazzx

I assume your pulling via an RFC read Table. This Connection will work fine for those.

我假设您通过 RFC 读取表进行拉取。这个连接对那些人来说很好用。

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 NOTsupported 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 EXACTLYwhat shows up on the box selection screen of SAP under name. 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.

如果你的公司有多个SAP盒(DEV,生产,试验)的SYSTEMNAME会确切地什么SAP的名义下框选屏幕上显示出来。假设您从第二个代码块中收到 RFC 错误,那么您在该代码中使用的框名称将是正确的。