on 06-24-2016 1:12 AM
Hi,
I'm having trouble with my script in picking the correct system to run. For example I have A6P430 and ANP430 system open at the same time. When I run my macro it choose the wrong system.
Im using SSO to open sap. Would it be better to include sap logon before macro code?
Please try to improve my script. Thank you.
What I have is this:
Public SapGuiAuto, WScript, msgcol
Public objGui As GuiApplication
Public objConn As GuiConnection
Public objSess As GuiSession
Public objSBar As GuiStatusbar
Public objSheet As Worksheet
Dim W_System
Function Attach_Session() As Boolean
Dim il, it
Dim W_conn, W_Sess
If W_System = "" Then
Attach_Session = False
Exit Function
End If
If Not objSess Is Nothing Then
If objSess.Info.SystemName & objSess.Info.Client = W_System Then
Attach_Session = True
Exit Function
End If
End If
If objGui Is Nothing Then
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
End If
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
If objSess Is Nothing Then
MsgBox "No active session to system " + W_System + ", vbCritical + vbOKOnly"
Attach_Session = False
Exit Function
End If
If IsObject(WScript) Then
WScript.ConnectObject objSess, "on"
WScript.ConnectObject objGui, "on"
End If
Set objSBar = objSess.findById("wnd[0]/sbar")
objSess.findById("wnd[0]").maximize
Attach_Session = True
End Function
Here is how I call the system:
Sub StartExtractA6P()
Dim currentline As Integer
W_System = "A6P430"
RunGUIScriptA6P currentline
End Sub
Hello,
Can you please help me, I have this code:
-------
Public SapGuiAuto, WScript
Public objGui As GuiApplication
Public objConn As GuiConnection
Public objSess As GuiSession
Dim W_System
Dim W_Ret As Boolean
'Fonction pour se connecter à l'interface SAP
Function Connexion_SAP() As Boolean
Dim il, it
Dim W_conn, W_Sess
'If objGui Is Nothing Then Set objGui = GuiApplication.GetApplication
'Nom de la session SAP à laquelle on veur se connecter sous la forme AAABBB : AAA est le nom du système et BBB est le numéro
W_System = "P01400"
'Si on est déjà connecté à une session SAP, n'essaie pas de se reconnecter à nouveau
If W_System = "" Then
Connexion_SAP = False
Exit Function
End If
'Si l'objet de la session n'est pas nul, utilise cette session (en supposant que connecté à la bonne session)
If Not objSess Is Nothing Then
If objSess.Info.SystemName & objSess.Info.Client = W_System Then
Connexion_SAP = True
Exit Function
End If
End If
'Si connecté à rien, installer les objets
If objGui Is Nothing Then
Set SapGuiAuto = GetObject("SAPGUI")
Set objSui = SapGuiAuto.GetScriptingEngine
End If
'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
'Si aucune session SAP correspondante n'est trouvée, quitte la macro et donne un message d'erreur
If objSess Is Nothing Then
MsgBox "Pas de ssession active dans le syteme " + W_System + ", ou le scriptin n'est pas autorisé.", vbCritical + vbOKOnly
Connxion_SAP = False
Exit Function
End If
'active le scripting
If IsObject(WScirpt) Then
WScirpt.ConnecObject objSess, "on"
WScirpt.ConnecObject session, "on"
WScirpt.ConnecObject Application, "on"
End If
-----
I have a vba error on this line "For il = 0 To objGui.Children.Count - 1" runtime error 91 object variable or block variable with not defined
Thanks for your help
Olivier
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Harris,
your code is good and it works with a few small changes:
For il = 0 To objGui.Children.Count - 1
Set W_conn = objGui.Children(CLng(il))
For it = 0 To W_conn.Children.Count - 1
Set W_Sess = W_conn.Children(CLng(it))
If W_Sess.Info.SystemName & W_Sess.Info.Client = W_System Then
Set objConn = objGui.Children(CLng(il))
Set objSess = objConn.Children(CLng(it))
Exit For
End If
Next
Next
If objSess Is Nothing Then
'MsgBox "No active session to system " + W_System + ", _
' vbCritical + vbOKOnly"
MsgBox "No active session to system " + W_System, vbCritical + vbOKOnly
Attach_Session = False
Exit Function
End If
Let us know your results.
Cheers
Stefan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Stefan,
Thank you for the reply but the adjustment in code did not help. My old code and your edited version is still having the same error in picking the correct system. The code works if all sap systems are open and runs on the correct system (old code), what I need is it will display an error if the sap system is not open. But what It does is run the VBA into the wrong system and displays the error. It shouldstop VBA from executing if the correct SAP system is not open.
Thanks,
Harris
Hi Harris and Stefan,
Sorry, that I interfere. But one could still try the following:
. . .
on error resume next
'''If objGui Is Nothing Then
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
'''End If
if Err.Number <> 0 then
Err.Clear
MsgBox "No active session in all systems " , vbCritical + vbOKOnly
Attach_Session = False
Exit Function
end if
On Error GoTo 0
set objSess = Nothing
For il = 0 To objGui.Children.Count - 1
. . .
Regards,
ScriptMan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.