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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| 'Declaration des variables pour se connecter à SAP
Public SapGuiAuto, WScript
Public objGui As GuiApplication
Public objConn As GuiConnection
Public objSess As GuiSession
Dim W_System
Dim W_Ret As Boolean
Dim Variante As Variant
Dim Transaction As Variant
Dim LancementAuto As Variant
Dim BtnTab As String
'Fonction pour se connecter à l'interface SAP
Function Connexion_SAP() As Boolean
Dim il, it, W_conn, W_Sess, TransactionSession
'Nom de la session SAP à laquelle on veut se connecter sous la forme AAABBB : AAA est le nom du système et BBB est le numéro
W_System = "PRD200"
'Si connecté à rien, installer les objet
If objGui Is Nothing Then
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
End If
On Error GoTo Erreur 'Si erreur alors message
'Boucle sur les sessions SAP Ouvertes jusqu'à trouver celle de notre variable "W_system"
For il = 0 To objGui.Children.Count - 1
Set W_conn = objGui.Children(il + 0)
For it = 0 To W_conn.Children.Count - 1
Set W_Sess = W_conn.Children(it + 0)
If W_Sess.Info.SystemName & W_Sess.Info.Client = W_System Then
Set objConn = objGui.Children(il + 0)
Set objSess = objConn.Children(it + 0)
Exit For
End If
Next
Next
'Explication : Il faut vérifier les sessions et se connecter à la premiere qui est sur la transaction SESSION MANAGER.
TransactionSession = "SESSION_MANAGER"
If Not IsObject(sapApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set sapApplication = SapGuiAuto.GetScriptingEngine()
End If
If Not IsObject(sapConnection) Then
Set sapConnection = sapApplication.Children(0)
End If
If Not IsObject(sapsession) Then
Set sapsession = sapConnection.Children(0)
End If
'Si que une session dans une transaction alors ouvrir une autre
If W_conn.Children.Count < 2 Then
objSess.CreateSession
End If
'Test choix d'une transaction sur le menu d'accueil
For i = 1 To W_conn.Children.Count
If sapsession.Info.Transaction = TransactionSession Then
Set objSess = objConn.Children(i)
Set objConn = objGui.Children(i)
Set sapsession = sapConnection.Children(i)
Exit For
Else
sapConnection.logoff
End If
Next
Erreur:
If objSess Is Nothing Then
MsgBox "Pas de session active dans le système " + W_System + ", ou le scripting n'est pas autorisé.", vbCritical + vbOKOnly
Connexion_SAP = False
Exit Function
End If
'Active le scripting
If IsObject(WScript) Then
WScript.ConnectObject objSess, "on"
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
Connexion_SAP = True
End Function
Function RECHERCHEV(Valeur_Cherchee As Variant, Table_matrice As Range, No_index_col As Single, Optional Valeur_proche As Boolean)
On Error GoTo RECHERCHEVerror
RECHERCHEV = application.VLookup(Valeur_Cherchee, Table_matrice, No_index_col, Valeur_proche)
If IsError(RECHERCHEV) Then RECHERCHEV = "#N/A"
Exit Function
RECHERCHEVerror:
RECHERCHEV = "#N/A"
End Function[/QUOTE]
La fonction Connexion_SAP() me permet de me connecter à la bonne session et au bon système SAP mais je n'arrive pas à selectionner la session qui se trouve dans le menu (Transaction : SESSION_MANAGER).
J'ai essayer ce bout de code mais rien à faire :
[QUOTE]'Explication : Il faut vérifier les sessions et se connecter à la premiere qui est sur la transaction SESSION MANAGER.
TransactionSession = "SESSION_MANAGER"
If Not IsObject(sapApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set sapApplication = SapGuiAuto.GetScriptingEngine()
End If
If Not IsObject(sapConnection) Then
Set sapConnection = sapApplication.Children(0)
End If
If Not IsObject(sapsession) Then
Set sapsession = sapConnection.Children(0)
End If
'Si que une session dans une transaction alors ouvrir une autre
If W_conn.Children.Count < 2 Then
objSess.CreateSession
End If
'Test choix d'une transaction sur le menu d'accueil
For i = 1 To W_conn.Children.Count
If sapsession.Info.Transaction = TransactionSession Then
Set objSess = objConn.Children(i)
Set objConn = objGui.Children(i)
Set sapsession = sapConnection.Children(i)
Exit For
Else
sapConnection.logoff
End If
Next |
Partager