VBA piloter SAP Logon erreur session.Children(0)
Bonjour,
Je souhaite piloter SAP logon afin d'ouvrir automatiquement une session et lancer des transactions.
J'arrive à ouvrir SAP, la fenêtre de connexion (login, mdp) s'affiche à l'écran puis Paf, erreur sur la ligne
J'ai étudié le message de lenovo49 à cette adresse : http://www.developpez.net/forums/d14...a/#post8133962 mais je n'arrive pas à resoudre le problème.
Dans ce post, Biorx8 donne visiblement une solution. J'ai réussi à adapter son code, la macro m'indique donc que la connexion est "OK", mais ensuite, je n'arrive pas à afficher SAP et lancer des transactions.
Le code de Biorx8 :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| Function test()
Set functionCtrl = CreateObject("SAP.Functions")
Set sapConnection = functionCtrl.Connection
With Sheets("Feuil1")
If Sheets("Feuil1").Cells(2, z).Value <> "" Then
sapConnection.User = .Cells(2, z).Value 'nom d'utilisateur
sapConnection.Password = .Cells(3, z).Value ' mot de passe
sapConnection.System = .Cells(4, z).Value 'ID systeme (non obligatoire) fonctionne très bien sans... voir, ne pas renseigner
sapConnection.ApplicationServer = .Cells(5, z).Value 'IP du serveur
sapConnection.SystemNumber = .Cells(6, z).Value
sapConnection.Client = .Cells(7, z).Value
sapConnection.Language = .Cells(8, z).Value
If sapConnection.logon(0, True) <> True Then
.Cells(10, z).Value = "KO"
'End program
Else
.Cells(10, z).Value = "OK"
End If
Else
End If
End With
End Function |
Ci-dessous, le code utilisé pour lancer SAP. Mon problème c'est que Connection.Children.Count reste toujours à 0 alors que la fenetre de connexion SAP apparaît bien à l'écran.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Const INFINITE = -1&
Private Const SYNCHRONIZE = &H100000
Sub sVBScriptSAP()
On Error Resume Next
'Librairies ajoutées:
'Window script Host Object model
'Sap Remote Function call Control
'SAP GUI scripting API
Application.DisplayAlerts = False
If Not IsObject(SAP_applic) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set SAP_applic = SapGuiAuto.GetScriptingEngine
End If
er = Err.Number
On Error GoTo 0
If er <> 0 Then
er = 0
On Error Resume Next
Dim iTask As Long, ret As Long, pHandle As Long
iTask = Shell("C:\Program Files\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus)
pHandle = OpenProcess(SYNCHRONIZE, False, iTask)
ret = WaitForSingleObject(pHandle, INFINITE)
ret = CloseHandle(pHandle)
On Error GoTo 0
er = 0
On Error Resume Next
If Not IsObject(SAP_applic) Then
Set SapGuiAuto = GetObject("SAPGUI") 'Setting
Set SAP_applic = SapGuiAuto.GetScriptingEngine
End If
er = Err.Number
On Error GoTo 0
If er <> 0 Then
tmp = MsgBox("SAP Logon not installed on your system" & vbCrLf & _
"Please install it using eSupport!", vbInformation)
End
End If
End If
Set Connection = SAP_applic.OpenConnection("Mon choix")
If Connection.Children.Count < 1 Then
Set Session = Connection.Children(0) 'Connection.Children(clng0)
Exit Sub
Else
Set Session = Connection.Children(0)
End If
Session.FindById("wnd[0]/usr/txtRSYST-BNAME").Text = "user"
Session.FindById("wnd[0]/usr/pwdRSYST-BCODE").Text = "mdp"
Session.FindById("wnd[0]/usr/txtRSYST-LANGU").Text = "FR"
Session.FindById("wnd[0]").sendVKey 0
End if |
Avez-vous une idée concernant ce problème?
Merci de votre aide.