带有VBA的SAP Logon

时间:2015-06-22 14:57:18

标签: vba sap

大家早上好!

我在过去几天一直在寻找解决方案,但我真的没有成功:我正在尝试制作一个vba代码:1-log into SAP,2-run some transactions 3-export优秀。

但即使是"登录SAP"部分不行!

我尝试了几个代码,下面是OPENS 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

另一个CreateObject("SAP.Functions"),显示:"收到RFC错误。没有功能模块RFC PING"

的RFC授权

代码是:

'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

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

3 个答案:

答案 0 :(得分:1)

首先,RFC是与SAP交互的完美方法。它没有失去支持。

其次,您没有足够的授权,因此即使您的语法正确,您的代码也无法正常工作。 &#34;收到RFC错误。没有RFC功能模块RFC PING&#34;。请求您的SAP团队授予您远程执行RFC的权限。要求 SAP_S_RFCACL

另外,在SAP中,运行某些事务并导出到Excel的主要对象非常容易。也许你应该让你的SAP团队为你做这件事而不是在VBA中开发它?

答案 1 :(得分:0)

我假设你通过RFC读取表。此连接适用于那些。

my_library

从这一点开始,您可以毫无问题地进行RFC调用。

但是说实话,上面几乎就是你的第二个例子。您的RFC错误似乎您没有为SAP提供安全设置,无法对您提供的任何表格进行RFC调用,而不是您的登录代码问题。

免责声明:SAP支持RFC_READ_TABLE NOT ,更像是一个后门,而不是日常的数据提取方法。

Edit1:要覆盖评论而不是将其转化为讨论,我将尝试在此总结它们。

<强>首先

弹出窗口:如果您想要登录弹出窗口,则需要更改此行代码

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

retcd = conn.Logon(0, True) 

其次

权限:RFC_Read_Table使用非常不同的安全设置然后SAP t-Code使用,技术差异难以解释但是根据经验,如果您无法访问SAP表(t-Code SE16),则很可能无法从RFC读取表中提取它

第三

如果您的公司有多个SAP盒子(DEV,生产,测试),则Systemname将完全显示在SAP名称框选择屏幕上的内容。假设您从第二个代码块中收到RFC错误,那么您在该代码中使用的框名称将是正确的。

答案 2 :(得分:0)

您可以绕过RFC控件,只需要模仿普通用户并手动引入用户名和密码的常规脚本即可。 Credit to The Script Man from the SAP forums

 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