cancel
Showing results for 
Search instead for 
Did you mean: 

Why my "rowCount" don't work?

Former Member
0 Kudos

Hello everyone.

I recorded a script with the "Record and Playback" in the Sap.

Well, I need to create a script to save emails on transaction "XD02". The emails must be written at last row after I click on button "Add new line"

My script recorded:

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

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

session.findById("wnd[0]/tbar[0]/okcd").text = "xd02"

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

session.findById("wnd[1]/usr/ctxtRF02D-KUNNR").text = "156008760"

session.findById("wnd[1]/usr/ctxtRF02D-KUNNR").caretPosition = 9

session.findById("wnd[1]").sendVKey 0

session.findById("wnd[0]/usr/subSUBTAB:SAPLATAB:0100/tabsTABSTRIP100/tabpTAB07").select

session.findById("wnd[0]/usr/subSUBTAB:SAPLATAB:0100/tabsTABSTRIP100/tabpTAB07/ssubSUBSC:SAPLATAB:0201/subAREA1:SAPMF02D:7360/btnPUSH-PARTNER").press

session.findById("wnd[0]/usr/subADDRESS:SAPLSZA5:0900/btnG_ICON_SMTP").press

session.findById("wnd[1]/tbar[0]/btn[13]").press

session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0,1]").text = "test@test.com"

session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0,1]").caretPosition = 13

session.findById("wnd[1]/tbar[0]/btn[0]").press

session.findById("wnd[0]/tbar[0]/btn[11]").press

So, my script must add a new line and then insert email in the last row.

I can't count rows with the command "rowCount".

I tried insert the code below after the step session.findById("wnd[1]/tbar[0]/btn[13]").press (Insert new line)

Rows = session.FindById("wnd[1]/usr/shellcont/shell").rowCount - 1

session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0,Rows]").text = "test@test.com"

When I execute the script a message box shows "The control could not be found by id. - "

Anyone could help me please?

PS: Sorry for my BAD English.


Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Thanks all for the answers. I got a solution by doing this:

If Not IsObject(Application) Then

          Set SapGuiAuto = GetObject()

          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

          session.findById("wnd[0]/tbar[0]/okcd").Text = "/NXD02"

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

          On Error Resume Next

                    session.findById("wnd[1]/usr/ctxtRF02D-KUNNR").text =  "100202030"

                    If Err.Number > 0 Then

                              session.findById("wnd[1]/tbar[0]/btn[0]").press

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

                              session.findById("wnd[1]/usr/ctxtRF02D-KUNNR").text =  "100202030"

                    End If

          On Error GoTo 0

          session.findById("wnd[1]/usr/ctxtRF02D-KUNNR").text =  "100202030"

          session.findById("wnd[1]").sendVKey 0

          session.findById("wnd[0]/usr/subSUBTAB:SAPLATAB:0100/tabsTABSTRIP100/tabpTAB07").select

          session.findById("wnd[0]/usr/subSUBTAB:SAPLATAB:0100/tabsTABSTRIP100/tabpTAB07/ssubSUBSC:SAPLATAB:0201/subAREA1:SAPMF02D:7360/btnPUSH-PARTNER").press

          session.findById("wnd[0]/usr/subADDRESS:SAPLSZA5:0900/btnG_ICON_SMTP").press

          session.findById("wnd[1]/tbar[0]/btn[13]").press 'Add new blank row

          Rows = session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6").rowCount - 1 'Count the number of rows in the grid

          For j = 0 To Rows

                    If (session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0," & CStr(j) & "]").Text = "") Then

                              session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0," & CStr(j) & "]").Text = "test@mail.com"

                              Exit For

                    End If

          Next

          session.findById("wnd[1]/tbar[0]/btn[0]").press 'Press button OK

          session.findById("wnd[0]/tbar[0]/btn[11]").press 'Press button SAVE

Former Member
0 Kudos

Hi Wagner - I had an issue like that some time ago and it was resolved (Cheers, ScriptMan!)  this way:

---------------------------------

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

--------------------------------------

Keep in mind this is just an example and the reference is to a different screen. The key is to look at the variable 'Ct' and observe how it was used in a line/field reference.

You can read the discussion if you search the forum with this text: "Referencing a variable in 'session findbyId' command (SAP GUI Scripting)"

Good luck

Best regards

Umur

Former Member
0 Kudos

Thanks Umur, but I need to do something else.

I have some emails registered. When I click "Add new line", the script needs to count the rows that already have records and then enter the e-mail.

I never know which is the line number where the email will be inputted.

I'm using Excel to generate the vbs code.

-------------------------------------------------

My code should be something like this:

For i = 0 to 16

Print #iFF, "session.findById(""wnd[1]/usr/ctxtRF02D-KUNNR"").text = """ & Sheet2.Range("$A$" & i).Value & """"

Print #iFF, "session.findById("wnd[1]/usr/ctxtRF02D-KUNNR").caretPosition = 9

Print #iFF, "session.findById("wnd[1]").sendVKey 0

Print #iFF, "session.findById("wnd[0]/usr/subSUBTAB:SAPLATAB:0100/tabsTABSTRIP100/tabpTAB07").select

Print #iFF, "session.findById("wnd[0]/usr/subSUBTAB:SAPLATAB:0100/tabsTABSTRIP100/tabpTAB07/ssubSUBSC:SAPLATAB:0201/subAREA1:SAPMF02D:7360/btnPUSH-PARTNER"").press"

Print #iFF, "session.findById(""wnd[0]/usr/subADDRESS:SAPLSZA5:0900/btnG_ICON_SMTP"").press"

Print #iFF, "session.findById(""wnd[1]/tbar[0]/btn[13]"").press"

Rows = session.FindById("wnd[1]/usr/shellcont/shell").rowCount - 1

Print #iFF, "session.findById(""wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0," & Rows & "]"").text = ""test@test.com""

Print #iFF, "session.findById(""wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0,1]"").caretPosition = 0"

Print #iFF, "session.findById(""wnd[1]/tbar[0]/btn[0]"").press"

Print #iFF, "session.findById(""wnd[0]/tbar[0]/btn[11]"").press"

Next i

-------------------------------------------------

script_man
Active Contributor
0 Kudos

Hi Wagner,

You could try the following:


. . .

session.findById("wnd[0]/tbar[0]/okcd").text = "xd02"

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

session.findById("wnd[1]/usr/ctxtRF02D-KUNNR").text = "156008760"

'session.findById("wnd[1]/usr/ctxtRF02D-KUNNR").caretPosition = 9

'It is not absolutely necessary.

session.findById("wnd[1]").sendVKey 0

session.findById("wnd[0]/usr/subSUBTAB:SAPLATAB:0100/tabsTABSTRIP100/tabpTAB07").select

session.findById("wnd[0]/usr/subSUBTAB:SAPLATAB:0100/tabsTABSTRIP100/tabpTAB07/ssubSUBSC:SAPLATAB:0201/subAREA1:SAPMF02D:7360/btnPUSH-PARTNER").press

session.findById("wnd[0]/usr/subADDRESS:SAPLSZA5:0900/btnG_ICON_SMTP").press

session.findById("wnd[1]/tbar[0]/btn[13]").press

i = 0

for i = 0 to 100

  session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6").verticalScrollbar.position = i

  ADDR = session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0,0]").text

  if ADDR = "" then exit for

next

session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0,0]").text = "test@test.com"

'session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0,1]").text = "test@test.com"

'see above

'session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0,1]").caretPosition = 13

'It is not absolutely necessary.

session.findById("wnd[1]/tbar[0]/btn[0]").press

session.findById("wnd[0]/tbar[0]/btn[11]").press

. . .

Regards,

ScriptMan

holger_khn
Contributor
0 Kudos

Hello.

As you only have three lines visible the rowcounter is 0,1 and 2. So an FOR-NEXT-LOOP will do this job:

'---------------------vbs start-----------------------------

session.findById("wnd[1]/tbar[0]/btn[13]").press

for i = 0 To 2

If session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0," & i & "]").text = "" then

  session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0," & i & "]").text = strSMTP ' "xxxx@somewhere.com"

  Exit For

End If

Next

session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0," & i & "]").caretPosition = 15

'---------------------vbs end-----------------------------

The issue in your code is that the variable 'Rows' is integrated in string. You need to open your string like:

Rows = session.FindById("wnd[1]/usr/shellcont/shell").rowCount - 1

'session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0,Rows]").text = "test@test.com"

session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0," & Rows & "]").text = "test@test.com"

But my code is enough to get it work as only thre lines are counted and visible when you add new line.

Br, Holger

script_man
Active Contributor
0 Kudos

Hi Holger,

although the solution for more than 3 addresses is not particularly complicated:

. . .

session.findById("wnd[1]/tbar[0]/btn[13]").press

for i = 0 to 100

  session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6").verticalScrollbar.position = i

  if session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0,0]").text = "" then exit for

next

session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0,0]").text = "test@test.com"

. . .

If the length of address is less than 15, the next command produces an error.

session.findById("wnd[1]/usr/tblSAPLSZA6T_CONTROL6/txtADSMTP-SMTP_ADDR[0," & i & "]").caretPosition = 15

For this reason, I would omit it completely.

Regards,

ScriptMan