cancel
Showing results for 
Search instead for 
Did you mean: 

Referencing a variable in 'session findbyId' command (SAP GUI Scripting)

Former Member

Hi all - I was wondering if anybody knew a way to get the following to work:

I would like the script to type in a value in the components grid in SAP but I want ot put it in a loop so I can bump up the line number with each read. For it to work I would need a variable that I can control the contents of, such as the "Ct" (bolded-highlighted below):

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-POSTP[1,Ct]").text = "n"

However, when I run it it does not like it and stops there. Is there a way to make this happen?

Cheers

Umur

Accepted Solutions (1)

Accepted Solutions (1)

script_man
Active Contributor
0 Kudos

Hi umur,

I've had a similar problem. The following would not work:

session_ = 0

Set session = connection.Children(session_)

But when I did this, everything ran perfectly:

Session_ = 0

Set session = connection.Children(int(session_))

Perhaps it is a way to solve the problem.

Regards,

ScriptMan

Former Member
0 Kudos

Hi Scriptman - Thanks for the response. I am not sure I understand the solution you offered. I basically have a screen with bunch of lines to feed data into. I want to be able to cycle through these lines (say 16 of them) and populate the fields. I would like to put this process into a loop where I will say something like this:

for Ct =1 to 16

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-POSTP1,Ct").text = "n"

next

which will enter "n" into a field on each of the 16 lines.

But when I do this, the script chokes on the counter "Ct" embedded in the sessionfindById command.

That was the variable I was trying to control.

Cheers.

Umur

script_man
Active Contributor
0 Kudos

Hi Umur,

I do not know if it works, but you could try:

for Ct =1 to 16

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-POSTP1," & int(Ct) & ").text = "n"

next

or simply

for Ct =1 to 16

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-POSTP1," & Ct & ").text = "n"

next

Regards,

ScriptMan

Former Member
0 Kudos

Hi ScriptMan - I tried every possible combination (almost) including CInt, Int and nothing seems to work. I believe the issue is that before it tries to do the substitution it tries to interpret the field and fails when it sees the "&", as that combination does not exist as a field. In your first example with the Session:

Set session = connection.Children(int(session_)), I think it works because it is a parameter that it calculates first.

Looks like I have to continue using blocks of 16, repeating the set in the code. It is an ugly solution but as long as it works, it is ok.

Thanks for your comments on this.

Regards

Umur

script_man
Active Contributor
0 Kudos

Hi Umur,

my last proposal on this issue would be as follows:

for Ct =1 to 16

Script_Line = "session.findById(" & chr(34) & "wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-POSTP1," & int(Ct) & chr(34) & ").text = " & chr(34) & "n" & chr(34)

Script_Line

next

Regards,

ScriptMan

Former Member
0 Kudos

ScriptMan - we are almost there. I got it to pack the statement correctly based on your code (because the statement was in array form:POSTP[1,1], I had to add the extra "]") but when the line you recommend :"Script_Line" is processed, it returns error "Type mismatch: 'Script_line' ". I guess it does not like being called like that. If we can get the script to process the packaged statement with "Script_Line = " on the following line, this will work beautifully.

Regards

Umur

script_man
Active Contributor
0 Kudos

Hi Umur,

You will notice already, I can not let go. The only way I see the parameters in a file to write, and then processed.

For Example:

for Ct =1 to 16

Script_Line = "session.findById(" & chr(34) & "wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-POSTP1," & int(Ct) & chr(34) & ").text = " & chr(34) & "n" & chr(34)

set Wshell = CreateObject("WScript.Shell")

Set fso = CreateObject("Scripting.FileSystemObject")

set TextFile = fso.CreateTextFile("c:\tmp\Script_Line.vbs", True)

TextFile.WriteLine "If Not IsObject(application) Then"

TextFile.WriteLine "Set SapGuiAuto = GetObject(" & chr(34) & "SAPGUI" & chr(34) & ")"

TextFile.WriteLine "Set application = SapGuiAuto.GetScriptingEngine"

TextFile.WriteLine "End If"

TextFile.WriteLine "If Not IsObject(connection) Then"

TextFile.WriteLine "Set connection = application.Children(0)"

TextFile.WriteLine "End If"

TextFile.WriteLine "If Not IsObject(session) Then"

TextFile.WriteLine "Set session = connection.Children(0)"

TextFile.WriteLine "End If"

TextFile.WriteLine "If IsObject(WScript) Then"

TextFile.WriteLine "WScript.ConnectObject session, " & chr(34) & "on" & chr(34)

TextFile.WriteLine "WScript.ConnectObject application, " & chr(34) & "on" & chr(34)

TextFile.WriteLine "End If"

TextFile.WriteLine Script_Line

TextFile.Close

wshell.run "c:\tmp\Script_Line.vbs", 1, true

next

Regards,

ScriptMan

Former Member
0 Kudos

Hi ScriptMan - Success!! Your persistence is appreciated. Your approach is a smart way to build a script file from another script file and store the constructed code lines in there to be executed later.

First I tried it and ran into an error from Windows Script Host:

Script: c:\tmp\Script_Line.vbs

Line: 15 (see below)

Error: The control could not be found by id,

Code:800A0268,

Source: SAP Front end server.

Line 15 it complained about was:

session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/ctxtRESBD-POSTP[6,1]").text = "n"

Then I moved a few statements around and got rid of that error. Here's what I did based on your code:

If Not IsObject(application) Then

Set SapGuiAuto = GetObject("SAPGUI")

Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

Set connection = application.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

Dim Ct

session.findById("wnd[0]").maximize

set Wshell = CreateObject("WScript.Shell")

Set fso = CreateObject("Scripting.FileSystemObject")

set TextFile = fso.CreateTextFile("c:\tmp\Script_Line.vbs", True)

TextFile.WriteLine "If Not IsObject(application) Then"

TextFile.WriteLine "Set SapGuiAuto = GetObject(" & chr(34) & "SAPGUI" & chr(34) & ")"

TextFile.WriteLine "Set application = SapGuiAuto.GetScriptingEngine"

TextFile.WriteLine "End If"

TextFile.WriteLine "If Not IsObject(connection) Then"

TextFile.WriteLine "Set connection = application.Children(0)"

TextFile.WriteLine "End If"

TextFile.WriteLine "If Not IsObject(session) Then"

TextFile.WriteLine "Set session = connection.Children(0)"

TextFile.WriteLine "End If"

TextFile.WriteLine "If IsObject(WScript) Then"

TextFile.WriteLine "WScript.ConnectObject session, " & chr(34) & "on" & chr(34)

TextFile.WriteLine "WScript.ConnectObject application, " & chr(34) & "on" & chr(34)

TextFile.WriteLine "End If"

for Ct =0 to 16

Script_Line = "session.findById(" & chr(34) & "wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/ctxtRESBD-POSTP[6," & int(Ct) & "]" & chr(34) & ").text = " & chr(34) & "n" & chr(34)

TextFile.WriteLine Script_Line

next

TextFile.Close

wshell.run "c:\tmp\Script_Line.vbs", 1, true

ScriptMan - you are really the ScriptMan! This opens doors for many other scripts where I can use this or a variation of this scheme.

Thanks so much for taking the time and energy to get this to work. Much appreciated...

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi ScriptMan,

Like you, it's not natural for me to write in English! I'm a French Canadian, coming from Quebec.

In your last message, you mention interesting topics: Guixt and Excel interface. I learned a little bit on Guixt but didn't have to use it yet... But Excel interface, that's what I work with...

I integrate my scripts in Excel or ACCESS.

Advantages of Excel interface:

- easy programming with VBA

- easy input of values by the user

Advantages of ACCESS interface:

- easy programming with VBA

- extensive possibilities for validating and transforming data from SAP before uploading data to SAP (through normal transactions)

Surely, sharing what we all have learned is a good idea...

Former Member
0 Kudos

Hi Umur and ScriptMan,

As you have said, Umur, it is great to have multiple solutions. By reading on this forum and others, I used to try several solutions and my comprehension of scripting improved by the way.

This is the first time I write about scripts' topic. Usually, I only search for responses. I think now I can contribute by sharing what I learned.

Sap's scripts make work easier... for me and all my coworkers... I always want to understand more and find better solutions... I think about new solutions when I'm driving, in my shower, at home, etc... 🐵 No, I'm not insane.... I think...

script_man
Active Contributor
0 Kudos

Hi jogala2 and Umur,

It takes courage and experience to give someone an answer to his question. There is also the courage to ask a question at all. And for me it is also that it is courageous to communicate in English, although I can't speak English. Presently, however, there are good translation tools.

jogala2, as you write, it was for me a year ago. They want something back to the community. I was looking also and usually only found solutions. Today one can find all the things, if the query is formulated properly. The search for new solutions can I do empathize. Only in the car I do not try to do it.

I look forward to further meetings with both of you. There are so many interesting topics (eg GuiXT, Excel interface ...)

Regards,

ScriptMan

Former Member
0 Kudos

Oops, I made an error in my response...

It should have been:

for Ct =1 to 16
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-POSTP1," & Cstr(Ct)).text = "n"
next

In the () is the argument for findById function. In our case, the ID to search for will be the concatenation of the ".........." and Cstr(Ct). So there's no need to add an extra " at the end. " is used to start and end a string.

Again, hope it helps!

script_man
Active Contributor
0 Kudos

Hi jogala2,

Thank you for your solutions. I can't try it but Umur can it definitely. Thus the complex structure would no longer be necessary. I'd prefer to share the points with you.

Regards,

Scriptman

Edited by: ScriptMan on Nov 9, 2009 1:05 PM

Former Member
0 Kudos

Hi Jogala2 and ScriptMan - I tried the new method and at first it did not work but then I realized the "]" were missing. So when I changed it to:

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-POSTP[1," & Cstr(Ct) & "]").text = "n"

then it worked beautifully. This is an easier code to work with although I used ScriptMan's method successfully to load up bunch of records at the time.

It is great to have alternatives as these help us improve them even more.

Thanks to both of you....

Best regards

Umur

Former Member
0 Kudos

Hi Umar and Scriptman,

I would like to propose you another solution for this problem. When I started to script with SAPGUI Scripting, I used the last method Scriptman provided: construct a second script with the values I wanted.

I finally found a better way... Here's my solution:

for Ct =1 to 16
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-POSTP1," & Cstr(Ct) & ").text = "n"
next

The text within "wnd[0]........." is a string. This string is the ID that findById search for.

To insert a variable in a string, we have to insert a value that is a string too and use Cstr function.

Hope this could help,

Former Member
0 Kudos

Sorry the code did not come out clean when I pasted it. I will try again for those who could use the design (thanks again ScriptMan):

Regards

Umur

-


'This script creates another script file in C:\tmp folder, creates an executable line (add "n" in

' item category on the components tab of a work order 17 times and executes that script from tmp folder.

' new script file populates IC column with n for 17 lines.

If Not IsObject(application) Then

Set SapGuiAuto = GetObject("SAPGUI")

Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

Set connection = application.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

Dim Ct

session.findById("wnd[0]").maximize

set Wshell = CreateObject("WScript.Shell")

Set fso = CreateObject("Scripting.FileSystemObject")

set TextFile = fso.CreateTextFile("c:\tmp\Script_Line.vbs", True)

TextFile.WriteLine "If Not IsObject(application) Then"

TextFile.WriteLine "Set SapGuiAuto = GetObject(" & chr(34) & "SAPGUI" & chr(34) & ")"

TextFile.WriteLine "Set application = SapGuiAuto.GetScriptingEngine"

TextFile.WriteLine "End If"

TextFile.WriteLine "If Not IsObject(connection) Then"

TextFile.WriteLine "Set connection = application.Children(0)"

TextFile.WriteLine "End If"

TextFile.WriteLine "If Not IsObject(session) Then"

TextFile.WriteLine "Set session = connection.Children(0)"

TextFile.WriteLine "End If"

TextFile.WriteLine "If IsObject(WScript) Then"

TextFile.WriteLine "WScript.ConnectObject session, " & chr(34) & "on" & chr(34)

TextFile.WriteLine "WScript.ConnectObject application, " & chr(34) & "on" & chr(34)

TextFile.WriteLine "End If"

for Ct = 0 to 16

Script_Line = "session.findById(" & chr(34) & "wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/ctxtRESBD-POSTP[6," & int(Ct) & "]" & chr(34) & ").text = " & chr(34) & "n" & chr(34)

TextFile.WriteLine Script_Line

next

TextFile.Close

wshell.run "c:\tmp\Script_Line.vbs", 1, true