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
| Function logonSAP() As Boolean
#If VBA7 Then
Dim lhWnd As LongPtr
#Else
Dim lhWnd As Long
#End If
Dim Temps As Double
On Error GoTo Erreur
'Vérifier si la fenêtre Logon est ouverte - sinon l'ouvrir
Temps = Timer
lhWnd = FindWindow(vbNullString, Titre)
If lhWnd = 0 Then
Call Shell("C:\Program Files (x86)\SAP\FrontEnd\SAPGUI\saplogon.exe", vbMinimizedFocus)
Do
lhWnd = FindWindow(vbNullString, Titre)
If Timer - Temps > 10 Then '10 secondes de délai pour ouvrir la fenêtre sinon on sort
MsgBox "Impossible d'ouvrir la fenêtre SAP Logon", vbExclamation, ""
Exit Function
End If
Loop Until lhWnd > 0
End If
If AppliSAP Is Nothing Then
Set SapGuiAuto = GetObject("SAPGUI")
Set AppliSAP = SapGuiAuto.GetScriptingEngine
End If
'Ouverture d'une session PRD - SSO
'Si SAP est déjà ouvert, on cherche une session libre
If AppliSAP.Connections.Count > 0 Then
For Each Connection In AppliSAP.Children
If Not Connection.DisabledByServer And Left(Connection.Description, 3) = "PRD" Then
For Each TmpSession In Connection.Children
If TmpSession.Busy = False Then
Set Session = TmpSession
logonSAP = True
Exit Function
End If
Next
End If
Next
Else
'Si SAP est fermé
Set Connection = AppliSAP.OpenConnection("PRD - SSO") 'Changer au besoin
Temps = Timer
Do
Set Session = Connection.Sessions(0)
If Timer - Temps > 10 Then
MsgBox "Impossible de créer une session", vbExclamation, ""
Exit Function
End If
Loop While Session Is Nothing
End If
If Session Is Nothing Then
MsgBox "Impossible de trouver une session libre", vbExclamation, "?!?"
Set TmpSession = Nothing
Set Connection = Nothing
Set AppliSAP = Nothing
Set SapGuiAuto = Nothing
Exit Function
Else
logonSAP = True
End If
Exit Function
Erreur:
MsgBox Err.Number & vbCrLf & Err.Description
Stop 'pour débogage - F8 passe à la ligne suivante
Resume 'F8 se rend sur la ligne en défaut
End Function |
Partager