Excel / VBA, Connecting to SAP, What type of object returns GetObject ("SAPGUI")?

TL DR ---------------

If I correctly declare the variables in the initialization script, I cannot connect to the "connection" objects of SAPFEWSELib.GuiApplication. The MyApplication.Children (0) collection is empty, but if I comment out the ad 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 problem is connecting. The SAP client provides a vbscript sample when using the script write 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 stackoverflow question is close to answer, but it was short stackoverflow.com/questions/24738998/vba-using-variables-that-were-not-declared

The source code is here , I modified it a bit to make it work in excel / vba. (I reset IsObject (Wscript) and I had to replace MyApplication application)

The first thing I want to do is explicitly declare all the variables.

Using the code I received at the following address stackoverflow.com/questions/19783180/get-list-of-all-properties-for-an-object I used the "TypeLib Information" and the TypeName () function to determine each type of object. I tried to declare as follows

 Dim session as GuiSession Dim MyApplication as GuiApplication Dim Connection as ISapConnectionTarget Dim SapGuiAuto as object 

I get a User Defined Unacknowledged Type Error.

After a short search, I found a partial answer at this address.

scn.sap.com/thread/3254335

I feel that the link "C: \ Program Files (x86) \ SAP \ FrontEnd \ SAPgui \ sapfewse.ocx" is manually added. If there is a better way to do this, please let me know.

Now I can declare this, and it works.

 Dim MyApplication As SAPFEWSELib.GuiApplication Dim Connection As SAPFEWSELib.GuiConnection Dim session As SAPFEWSELib.GuiSession Dim SapGuiAuto As Object 

I do not like the SapGuiAuto As object, because it really does not say what it is, and I can not use CTRL + SPACE to view the available properties and functions of this object (one of the VB functions I can not live without now!)

All I know about this object is its .GetScriptingEngine function (method?).

During the study, to write this question, I came across this topic on the sap.com forums. scn.sap.com/thread/3448120

It is mentioned here that "GetScriptingEngine is a method of the GuiApplication class."

So, I tried the following declarations and seemed 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”. a list of possible properties appears. But there is a problem!

If I type the following line

 Debug.Print session.FindById("wnd[0]").Text 

This gives an error (while it worked just fine!). Error: "Object variable or With bock variable not set".

If I comment on variable declarations, it works just fine!

Using the TLI function when the session is not declared, I get the following members

 DumpProperties session.FindById("wnd[0]") 

output here -> pastebin.mozilla.org/8882551

but if I run the same command with a correctly declared session, I get .. the same error

So, after a bit more research, it turned out that the start of the script is now not working.

 If Not IsObject(MyApplication) Then 

Will not run if MyApplication was declared using Dim MyApplication As SAPFEWSELib.GuiApplication

So I tried to execute commands without IFs.

 Set MyApplication = SapGuiAuto.GetScriptingEngine Set Connection = MyApplication.Children(0) Set session = Connection.Children(0) 

This fails with the error "Object variable or With bock variable not set" in Set MyApplication = SapGuiAuto.GetScriptingEngine

The solution to this problem was to create a new instance of SapGuiAuto similar to this.

 Set SapGuiAuto = New SAPFEWSELib.GuiApplication 

Now the above code is executed until it completes with the error Set Connection = MyApplication.Children (0)

Error: "The collection enumerator cannot find the en element with the specified index"

A quick test with the following line shows that the .Children collection is empty.

 For Each chld In MyApplication.Children: Debug.Print "one exists": Next 

This is the same error that I usually get if I am currently disconnected from SAP, but I am connected and commenting on the ad unit, fixes the problem.

This answer to stackoverflow is an ominous stackoverflow.com/questions/36751819/sap-gui-scripting-error-the-enumerator-of-the-collection-cannot-find-an-elemen

I am not an administrator, he is at 10 o’clock on Friday night and asks that all this is a nightmare. I hope I don’t have to resort to this.

I will try on another computer.

Just tried, I get the same. At this moment I have to throw a towel, I can not get it to work without any help or at least a good night's rest!

Thanks for any tips or comments.

Some additional links that I found very useful for people on the same path as me.

API GUI API

Using the VBA Debugger to Detect SAP GUI Properties and Functions scn.sap.com/docs/DOC-39696

SAP GUI Script API documentation (I could not open this file, but it was filled with good things) www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/a034a16b-3bfe-2a10- e2bb-8bd880db0b3c

SAP GUI scripting API: automation of user interaction (unfortunately, there are no Script development tools in my system) scn.sap.com/docs/DOC-4614

Also, the BIBS function may be useful to you, however, it does not work on my scn.sap.com/docs/DOC-4612 system

+7
source share
2 answers

I just came to this problem and found a solution for the "enumerator" error. Also posted it in the specified stackoverflow article that you mentioned.

Change this: Set Connection = Sap_Application.Children (0)

To this: Set connection = Sap_Application.Children (1)

As I explained there, I don’t know why this is happening, or what it means, I just messed up the numbers and worked.

Hope this is not too late, or maybe it will help someone else.

Hi

+1
source

When I use this, I also get 0 children as if they are not connected to SAP:
Set sapCon = New SAPFEWSELib.GuiApplication

Using this instead, it actually works:
Set sapCon = GetObject("SAPGUI").GetScriptingEngine

According to this: https://answers.sap.com/questions/12487790/what-are-the-differences-in-vba-methods-to-connect.html?childToView=12494892

0
source

All Articles