wnd[1] not found with .findById() - vba

I'm new to SAP scripting. Someone told me it is possible to automatize my work with VBA, so I looked into it. I looked on the internet how to connect, it works fine. I use the recorder to know what code to write. Everything works very well until the last line of this code:
session.findById("wnd[0]/usr/ctxtCAUFVD-AUFNR").Text = "000000000" 'write service order number
session.findById("wnd[0]").sendVKey 0 'Confirm (enter)
session.findById("wnd[0]/mbar/menu[0]/menu[9]/menu[2]/menu[4]").Select 'execute a function to close the order
session.findById("wnd[1]/usr/btnSPOP-OPTION1").press 'A popup appears (always) with "No" in autoselect. I want to click on "Yes"
.findById("wnd[1]") should allow me to use the confirmation popup window, but it always returns Error 619, Control not found. I have no idea why, since this line was written by the recorder. I explored for many hours the Internet and I can't find the answer.
Thank you in advance for your help.

Of course, I needed to ask to have the resolution idea...
The was I set up my connection was that session was actually the SAP session .ActiveWindow(), therefore only wnd[0]. It couldn't find wnd[1]. I execute the findById() from the SAP session and it works.
If Not IsObject(SAPGuiApp) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPGuiApp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = SAPGuiApp.Children(0)
End If
If Not IsObject(SAP_session) Then
Set SAP_session = Connection.Children(0)
End If
Set session = SAP_session.ActiveWindow()
session.findById("wnd[1]") 'Bad. session is only wnd[0]
SAP_session.findById("wnd[1]") 'Good it can find wnd[0] and wnd[1]

Related

SAP GUI scripting VBA - Button press is ignored

I am using SAP GUI 7.50 and running the script from Excel. I recorded a script for transaction ZC9_SE16_MAT table MARA. I want to select multiple material numbers, but pressing the button with the recorded script is ignored (does not open new window). The button is marked in the image. How can I open that window?
' Connect to SAP
Set SapGuiAuto = GetObject("SAPGUISERVER")
Set SAPApp = SapGuiAuto.GetScriptingEngine
If Not IsObject(Connection) Then
Set Connection = SAPApp.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If Not IsObject(Connection) Then
Set Connection = SAPApp.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject Application, "on"
End If
' Start Transaction
session.StartTransaction "ZC9_SE16N_MAT"
' Confirm Mara
session.findById("wnd[0]").sendVKey 8
' Press the button
session.findById("wnd[0]/usr/tblSAPLZ_C9_SE16NSELFIELDS_TC/btnPUSH[4,1]").SetFocus
' Bellow line does not work
session.findById("wnd[0]/usr/tblSAPLZ_C9_SE16NSELFIELDS_TC/btnPUSH[4,1]").press
The last line does not work.
I saw similar problem in this post, but the answer did not work for me.
I found a solution for my problem, but is not perfect. Press Shift + F11, which turns Technical View On. Then there appears option Ctrl + F9, which open window Multiple Entry. This new window looks very similar to the first window, but now recorded press on the button works.
This solved my problem, but I would still like to know how to press on the first button so if anyone knows please add answer.

SAP Logon 760 via VBA

since the SAP update my vba application fails to connect to the sap system.
My previous code does not work anymore. I guess it is because of the new subfolder. The subfolder "All Urt SAP Systems" and "01 Test System" did not exist in the past.
However this code worked in the past without the new subfolders:
Set rotentry = GetObject("SAPGUI")
Set App = rotentry.GetScriptingEngine
App.Application.DisplayAlerts = False
Set Connection = App.OpenConnection("TE1 (Test - Development System)") '<-- Here comes the error
Set Session = Connection.Children(0)
Does anybody know what I am missing here? I tried a few SAP forums without success.
I am getting the following error message:
SAP Logon connection entry not found

Does anyone know how to automate SAP Login in Automation Anywhere 11

enter image description hereI am using Vb script to launch SAP logon application and successfully able to launch and login into SAP, but the issue, the next line of code is not getting executes until the application is closed.
Any approach how to achieve please suggest.
VB Script Code
[set WshShell = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
Set proc = WshShell.Exec("C:\Program Files (x86)\SAP GUI_7.40_P10_LITE (VMware ThinApp)\saplogon.exe")
Do While proc.Status = 0
WScript.Sleep 100
Loop
Set SapGui = GetObject("SAPGUI")
Set Appl = SapGui.GetScriptingEngine
Set Connection = Appl.Openconnection("connectioname", True)
Set session = Connection.Children(0)
session.findById("wnd\[0\]/usr/txtRSYST-BNAME").Text = "username"
session.findById("wnd\[0\]/usr/pwdRSYST-BCODE").Text = "password"
session.findById("wnd\[0\]/usr/txtRSYST-LANGU").Text = "EN"
session.findById("wnd\[0\]").sendVKey 0][1]
Please refer https://botstore.automationanywhere.com/bot/sap-utilities/ , it is free. Just install it and you will be able to use the metabot.
I'd suggest not to use VBScript so extensively for SAP Automation. Metabots are highly compatible and reliable when it comes to SAP Automation. I can provide you SAP DLL (if you need), you can just import it and start using it. In this case, steps to Login to SAP would be as below.
Launch SAP (Program/File Command)
Wait for Window
The rest could be easily done with Metabot DLLs
Also, "the next line of code is not getting executes until the application is closed.", which line of code are you referring to exactly? Is it session.findById("wnd[0]").sendVKey 0][1]
Please try session.findById("wnd[0]").sendVKey [1] or session.findById("wnd[0]").sendVKey [0][1]
you will need the SAP DLL metbot if you are using v11 of AA
doc: https://docs.automationanywhere.com/bundle/enterprise-v11.3/page/enterprise/topics/aae-client/bot-creator/commands/using-bapi-to-automate-task.html

HP-ALM requirement status change alert script

I am using HP-ALM 12.21.
I want to set an Email notification alert whenever status is changes to "development" of my current requirements.
Just came across some solution where you can set alert on "customize" page but in my profile "Customize" page has only password change option.
is there any vbscript for this ?
First, you need to have administrator rights, or you can let your administrator do it.
Then add workflow script in Customization -> Workflow -> Requirement module script -> Req_FieldChange(FieldName).
For example,
Sub Req_FieldChange(FieldName)
On Error Resume Next
If TDConnection.UserName = "sa" Then
If FieldName = "RQ_REQ_STATUS" Then
Dim objReqFactory, objReq
Set objReqFactory=TDConnection.ReqFactory
Set objReq=objReqFactory.Item(Req_Fields("RQ_REQ_ID").Value)
objReq.Mail "sa#hpe.com","",0,"Requirement Change Notification","The user sa changed the status of the requirement"
End If
End If
On Error GoTo 0
End Sub
I hope my answer is helpful to you.
Thank you and regards,
Wenjuan

What type of object is returned by GetObject("SAPGUI")?

TL;DR ---------------
If I properly declare the variables in the initialisation script, I can't attach to the "connection" objects of a SAPFEWSELib.GuiApplication .
The collection MyApplication.Children(0), is empty but if I comment out the declaration block, it just works !?
TL;DR ---------------
I am trying to create a more reliable connection between my SAP client and my excel application.
My current issue is with the establishing of the connection. The SAP client provides a sample vbscript when using its script recording function.
If Not IsObject(MyApplication) Then
Debug.Print "yep"
Set SapGuiAuto = GetObject("SAPGUI")
Set MyApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = MyApplication.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
This other question on stackoverflow came close to answer, but fell short
stackoverflow.com/questions/24738998/vba-using-variables-that-were-not-declared
The original code is here, I have modified slightly so it would work in excel/vba.
(I dropped the IsObject(Wscript) and I had to replace Application with MyApplication)
First thing I want to do is explicitly declare all variables.
Using code I got at the following address
stackoverflow.com/questions/19783180/get-list-of-all-properties-for-an-object
I used the "TypeLib Information" and the function TypeName() to determine each object type. I tried declaring as follows
Dim session as GuiSession
Dim MyApplication as GuiApplication
Dim Connection as ISapConnectionTarget
Dim SapGuiAuto as object
I'm getting the User-defined type not defined error.
After a bit of searching, I found the partial answer at this address.
scn.sap.com/thread/3254335
I feel manual adding the "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapfewse.ocx" reference will break easily. If there's a better way to do this please let me know.
Ok now I can declare like this and it works.
Dim MyApplication As SAPFEWSELib.GuiApplication
Dim Connection As SAPFEWSELib.GuiConnection
Dim session As SAPFEWSELib.GuiSession
Dim SapGuiAuto As Object
I don't like the SapGuiAuto As Object as it doesn't really say what it is and I can't use CTRL+SPACE to view available properties and functions of this object (one of the VB features I can't live without now !)
All I know about this object is that it has a .GetScriptingEngine function (method ?).
While doing the research to write this question I came across this thread on the sap.com forums.
scn.sap.com/thread/3448120
Here it is mentioned that "GetScriptingEngine is a method of the class GuiApplication".
So I tried the following declarations and it seems to work.
Dim MyApplication As SAPFEWSELib.GuiApplication
Dim Connection As SAPFEWSELib.GuiConnection
Dim session As SAPFEWSELib.GuiSession
Dim SapGuiAuto As SAPFEWSELib.GuiApplication
Now if I type "session." the list of possible properties appear.
But there is a problem !
If I type the following line
Debug.Print session.FindById("wnd[0]").Text
It gives an error (while it used to work just fine !).
The error is "Object variable or With bock variable not set".
If I comment out the variable declarations, it works just fine !
Using the TLI function while session is undeclared I get the following members
DumpProperties session.FindById("wnd[0]")
output here -> pastebin.mozilla.org/8882551
but if I run the same command with session properly declared I get.. the same error
So after a bit more research, it turns out that the beginning of the script doesn't work the same now.
If Not IsObject(MyApplication) Then
Will not execute if MyApplication has been declared with Dim MyApplication As SAPFEWSELib.GuiApplication
So I tried executing the commands without the IFs.
Set MyApplication = SapGuiAuto.GetScriptingEngine
Set Connection = MyApplication.Children(0)
Set session = Connection.Children(0)
This fails with the "Object variable or With bock variable not set" error on Set MyApplication = SapGuiAuto.GetScriptingEngine
The solution to this issue was to create a new instance of SapGuiAuto like this.
Set SapGuiAuto = New SAPFEWSELib.GuiApplication
Now the above code executes, until it fails at Set Connection = MyApplication.Children(0)
The error : "The enumerator of the collection cannot find en element with the specified index"
A quick test with the following line reveals, the .Children collection is empty.
For Each chld In MyApplication.Children: Debug.Print "one exists": Next
This is the same error I would normally get if I am currently disconnected from SAP, but I am connected and commenting out the declaration block, fixes the issue.
This answer on stackoverflow is ominous
stackoverflow.com/questions/36751819/sap-gui-scripting-error-the-enumerator-of-the-collection-cannot-find-an-elemen
I'm not an administrator, it's 10 PM on a Friday and asking IT anything is a nightmare. Hopefully I don't have to resort to that.
I will try on another computer.
Just tried, I get the same thing. At this point I have to throw in the towel, I can't get this to work without some help or at least a good night's rest !
Thanks for any advice or comments
Some extra links I found very useful for the people on the same path as I am.
SAP GUI Scripting API PDF
Using the VBA debugger to discover SAP GUI properties and functions
scn.sap.com/docs/DOC-39696
SAP GUI Scripting API Documentation (I couldn't open this file but it's full of good stuff)
www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/a034a16b-3bfe-2a10-e2bb-8bd880db0b3c
SAP GUI Scripting API : How to Automate User Interaction (unfortunately my system lacks the "Script development tools")
scn.sap.com/docs/DOC-4614
Also the BIBS function might be helpful to you, however it was not working on my system
scn.sap.com/docs/DOC-4612
When I use this I also get 0 children as if not connected to SAP:
Set sapCon = New SAPFEWSELib.GuiApplication
By using this instead it's actually working:
Set sapCon = GetObject("SAPGUI").GetScriptingEngine
As per this: https://answers.sap.com/questions/12487790/what-are-the-differences-in-vba-methods-to-connect.html?childToView=12494892
I just came accross this issue myself, and found a solution for the "enumerator" error. Also posted it in the referenced stackoverflow post you mentioned.
Change this:
Set Connection = Sap_Application.Children(0)
Into this:
Set Connection = Sap_Application.Children(1)
As I explained there, not sure why this happens, or what it means, I just messed with the numbers and it worked.
Hope it's not too late, or maybe it will help someone else.
Regards