VBA - SAP GUI Scripting - Active Row number - user-interface

I am struggling with the code to read out the active row-number. I searched this forum (and others) and tried several things but I am not getting it running... I am doing a kind of find in my code, but then I will need the row number (actually the value of the 1st column) of the reference number I searched for.
test1 = session.FindById("wnd[0]/usr/tblSAPLV60PTCTRL_KOPIEREN").RowCount
' this returns the total number of records which is perfectly fine
test1 = session.FindById("wnd[0]/usr/tblSAPLV60PTCTRL_KOPIEREN").SetFocus
test1 = session.FindById("wnd[0]/usr/tblSAPLV60PTCTRL_KOPIEREN").CurrentRow
test1 = session.FindById("wnd[0]/usr/tblSAPLV60PTCTRL_KOPIEREN").VerticalScrollbar.Position
test1 = session.FindById("wnd[0]/usr/tblSAPLV60PTCTRL_KOPIEREN").getAbsoluteRow
test1 = session.FindById("wnd[0]/usr/tblSAPLV60PTCTRL_KOPIEREN").CurrentCellRow
getting this error message:
run-time error 438 - Reference SAP GUI scripting is enabled
Please let me know what I am missing or doing wrong.
Thanks in advance for your guidance
Flooo

As far as I am aware you cannot directly get the row number of the selected row(s).
The basic approach I use is to loop all the rows and then check if they have been selected or not, then action from there, something like:
Dim objTable as Object
Dim objRow As Object
Dim numbRows As Long
Set objTable = session.findById("wnd[0]/usr/tblSAPLCMDITCTRL_3500")
With objTable
numbRows = objTable.RowCount
For r = 0 To (numbRows - 1)
Set objRow = .GetAbsoluteRow(r)
If objRow.Selected Then
' your action here ....
End If
Next r
End With
Remember; you need to be able to handle scrolling additional pages otherwise you will get an error

Related

Assigning CSV values to structure

I'm creating what should be a simple program but I'm having some difficulty assigning values from a file into a structure and it's variables. Visual Basic.
Structure:
Public Structure Teams
Dim teamName As String
End Structure
Function:
Function getAvailableTeams() As Teams()
Dim rec As Teams
Dim index As Integer
Dim recCount As Integer = 0
'Count how many teams exist
FileOpen(1, "teamConfig.csv", OpenMode.Input)
Do Until EOF(1)
LineInput(1) 'Read document line by line
recCount += 1 'Increment team count by 1
Loop
'store team names in array
Dim teamNames(recCount - 1) As Teams
index = 0
Do Until EOF (1)
Input(1, rec.teamName)
teamNames(index).teamName = rec.teamName
index +=1
Loop
FileClose(1)
Return teamNames
End Function
Simple subroutine to test values are available and being picked up.
Dim availableTeams() As Teams
availableTeams = getAvailableTeams()
lbltest.text = availableTeams(1).toString
The file is stored as a CSV file and there are 11 available team names.
team1 \r\n
team2 \r\n
etc...
I appreciate this is probably something simple but I can't work out where I'm going wrong with this.
One of the comments was on the right track. You need to close and re-open the file for input to start at the beginning again. Since you were already at end-of-file, the second attempt fails immediately unless you re-start from the beginning.

Find bottom of a column in Excel using VBScript

I'm writing a script that will extract the contents of a column in an Excel spreadsheet. Since I don't know how many rows will have data, I need to be able to find the end of the column in the course of the script.
In VBA, I would use something like
sht.Range(sht.Cells(row, col), sht.Cells(row, col)).End(xlDown).row
When I tried using that line in the VBScript file (I knew it probably wouldn't work, but I was hoping for some good luck), a message came back saying that 'xlDown' is undefined. I could easily create an Excel macro to do this, but I want the program to run without having to alter the original spreadsheet.
Here's part of my current script where I define the Excel objects
Set fso = CreateObject("Scripting.FileSystemObject")
set xlapp = CreateObject("Excel.Application")
Set wkbk = xlapp.Workbooks.Open(path)
'xlapp.Visible = False
Set sht = wkbk.Sheets(sheetname)
Set myfile = fso.OpenTextFile(txtfile, 2, True)
lrow = sht.Range(sht.Cells(row, col), sht.Cells(row, col)).End(xlDown).row
path, sheetname, and txtfile are previously defined strings, and row and col are previously defined integers.
xlDown is actually a constant. It's value is -4121 as mentioned HERE.
So try something like,
const xlDown = -4121
lrow = sht.Range(sht.Cells(row, col), sht.Cells(row, col)).End(xlDown).row
OR you can make use of xlUp also in the following code to get the rowcount of a column say Column A:
const xlUp = -4162
lrow = sht.Range("A" & sht.Rows.Count).End(xlUp).Row
Glad that it worked. Here is simple one which will give you the count of rows in the used range for sheet sht
lrow = sht.UsedRange.Rows.Count

Looping error, too many records added

Ive been trying to write Access VBA code to automate the addition of replicates for germination tests.
Basically I have a form where I enter the total number of Reps (NoofReps) and the number of seeds per rep (RepSize) (e.g. 50 seeds). For each record added I want it to automatically add a record for each rep and automatically calc the Rep Number (i.e if i have 4 reps then it should add 4 records, numbered 1-4 reps) as well as the RepSize (e.g 50).
I have been trying out various loops based on information from this forum and other but am still getting errors with the number of records that it generates. I have tried both the "Do while" and "Do Until" but get the same result below either way.
Could someone please let me know where I am going wrong?...If i want 2 reps then it adds 2, If i want 3 then its 246, and if i want 4 it adds >30,000!!!
For the purposes of trying to fix the code I have started to type the number of reps manually into the code in the iNoofReps so that I know the error is in the code and not from the form.
Private Sub CmdAddReps3_Click()
Dim iRepNo As Integer ' stores the current value in the series
'Open the table
Set db = CurrentDb()
Set rstGReps = db.OpenRecordset("tblGReplicates")
' Initialise the variables
iRepNo = 1
iNoofReps = 3 'iNoofReps = Me.txtNoofReps
' Add the records using a loop
rstGReps.movefirst
Do 'Until rstGReps("RepNo") = (iNoofReps + 1) ' always want to include at least 1 repNo
rstGReps.AddNew
rstGReps("GTestID") = Me.GTestID
rstGReps("RepNo") = iRepNo
rstGReps("NoofSeed") = Me.txtNoOfSeeds
' Calculate the next RepNo value in the loop
iRepNo = iRepNo + 1
rstGReps.Update
rstGReps.moveNext
Loop Until rstGReps("RepNo") = (iNoofReps) + 1 ' so that the loop includes the final repNo.
MsgBox "Finished Looping"
rstGReps.Close
Set rstGReps = Nothing
Set db = Nothing
End Sub
Any help would be appreciated!!!
Well, you're moving next here: rstGReps.moveNext, and then you're comparing rstGReps("RepNo") = (iNoofReps) + 1 after moving next, thus being on an empty record, thus always equating to false.
Loop Until iRepNo = (iNoofReps) + 1 should fix it, then you're no longer referring to the recordset, which has already been set to the next record by the time you're referring to it.
You could also fix it by just eliminating this line:
rstGReps.moveNext
Since rstGReps.AddNew already moves the recordset to a new blank record, moving it forward after adding the record doesn't make much sense. If you remove it, you might want to remove the + 1 in Loop Until rstGReps("RepNo") = (iNoofReps) + 1

Search WebList for particular item count from Excel sheet

I need a help in VBScript or either in QTP for the below case.
For example:
I have nearly 40 items in the weblist. I have only one item in the Excel sheet that is one among the 40 in the weblist. If I run the script, the one in the Excel should be select in the weblist. How do I perform this? I tried many scenarios, but couldn't get it to work.
Below are some of the sample pieces of code I tried in QTP:
ocount=Browser("name:=brw").Page("title:=brw").WebList("htmlid:=tabContainerBrandSite_123&rtyoh").GetROProperty("items count")
msgbox ocount
var7=mySheet2.Cells(2,"C")
For k=2 to ocount
ocount2=Browser("name:=brw").Page("title:=brw").WebList("html id:=tabContainerBrandSite_123&rtyoh").GetItem(k)
msgbox ocount2
merchantName = DataTable("Merchant_Name","Global") 'an example if value is saved in global sheet
items_count = Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").GetROProperty("Items Count") 'This will get all the items from your weblist.
i = 1
Do
webListName = Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").GetItem(i)
'this will get first value from the web list
If merchantName = webListName Then 'comparing first value from your value from global sheet
Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").Select(i) 'selects that value
Exit do 'because it has found your only value from the local sheet, it exits
else
i = i + 1
End If
Loop While i <= items_count

VBScript - Putting Array Element into GetElementById

I am writing a VBScript that automatically interacts with some web pages. I am having trouble at the final step where the script needs to click on a link to make a booking. The link for each time will only be available if that time is free. The idea of my code is to simply select the first time available (I originally though I could do this by using Mid() and GetElementId as I know the first 7 chars of each link ID but couldn't get this working). The array contains the IDs for all possible times available in a day. Some will already have been taken so that ID will no longer exist on the form.
I have 2 problems:-
1) Neither getElementBy Id or the Document.All.Item().Click commands will accept an element from the array - I get an Object Required run time error.
2) If getElementId doesn't find a matching ID it simply throws an Object required error. I wasn't expecting this, I thought that my elem variable would be nothing or null and that I could test for this.
Can anyone give me any pointers?
'This is a shortened version of my array- there are lots more times!
Times(0)="bookBtn0810"
Times(1)="bookBtn0818"
Times(2)="bookBtn0826"
Dim TimeAvail
Dim i
Dim elem
TimeAvail = "No"
i = 0
Do While (TimeAvail = "No") or (i<3)
Set elem = IE.Document.GetElementById(Chr(34) & Times(i) & Chr(34)) 'Chr(34) is to add ""
if elem is nothing then
TimeAvail = "No"
i=i+1
else
TimeAvail = "Yes"
IE.Document.All.Item(Chr(34) & Times(i) & Chr(34)).click
end if
Loop
Now, unless I'm being very silly, you won't be able to sit a variable to a non-existent element.
The only thing I can think of is to add:
On Error Resume Next
At the beginning, so it skips the error message. You may need to handle the error separately yourself.

Resources