VBA Selenium Edge open website based on class - vba

I'm trying to navigate to a website based on what class name can be found. Based on the given ID it opens the first website and searches for 1 of 3 different class names. Based on what class name matches I want to open a specific website. The first website always has 1 of 3 different class names. With my current code when VBA does not find the very first class name it give me the error "Element not found" and does not continue to ElseIf. If first class name is found then it works fine and gives no errors. How can I make VBA ignore this error or make it search for all 3 class names and decide what to do based on what class name was found?
Code:
Dim appME As New WebDriver
appME.Start "edge", ""
Dim id
id = 141765
appME.Get "website 1" & id
If (Not appME.FindElementByClass("Class1") Is Nothing) Then
appME.Get "website 2" & id
ElseIf (Not appME.FindElementByClass("Class2") Is Nothing) Then
appME.Get "website 3" & id
ElseIf (Not appME.FindElementByClass("Class3") Is Nothing) Then
appME.Get "website 4" & id
End If

I would add On Error Resume Next before calling FindElementByClass, and On Error GoTo 0 afterwards.
To make sure an error in appME.Get does not get masked, it will probably be best to put this into a function of its own:
Function MyFindElementByClass(appME as WebDriver, Byval classname as String) as WebElement
Set MyFindElementByClass=Nothing
On Error Resume Next
Set MyFindElementByClass=appME.FindElementByClass(classname)
On Error Goto 0
End Function

Solved the problem by adding Raise:=False after classname.
Dim appME As New WebDriver
appME.Start "edge", ""
Dim id
id = 141765
appME.Get "website 1" & id
If (Not appME.FindElementByClass("Class1", Raise:=False) Is Nothing) Then
appME.Get "website 2" & id
ElseIf (Not appME.FindElementByClass("Class2", Raise:=False) Is Nothing) Then
appME.Get "website 3" & id
ElseIf (Not appME.FindElementByClass("Class3", Raise:=False) Is Nothing) Then
appME.Get "website 4" & id
End If

Related

MS Word VBA Macro from validation

I'm trying to do what, I think, ought to be the simplest of things, but I can't get it to work. i have an MS Word document with a number of legacy drop-down and text fields:
The first option in each drop-down is "Select ...", and if a user tabs out of one of the drop-downs without choosing something other than the first "Select ...", I want a msgbox to appear to tell them to make a selection, which works. What doesn't work, is that after the user dismisses the msgbox, I want the insertion point to return to the drop-down that they didn't select.
I understand that VBA has "timing issues", and from what I've read, one way to address these timing issues is to call the "return" macro from the validation macro. So I've written two macros:
Sub Validate()
' Dim strBookmark As String
' strBookmark = Selection.Bookmarks(1).Name
10: If (bool_debug) Then MsgBox ("NotSelected() - 10: strBookmark = " & strBookmark)
With ActiveDocument
If (strBookmark = "Locality") Then
Call Salary_Step
ElseIf (strBookmark = "Series") Then
20: If (bool_debug) Then MsgBox ("NotSelected() - 20: .FormFields(strBookmark).Name = " _
& .FormFields(strBookmark).Name)
If ((Len(.FormFields(strBookmark).Result) <> 4) Or (Not IsNumeric(.FormFields(strBookmark).Result))) Then _
MsgBox ("Please enter a 4 digit number.")
Call GoBackToPrevious(.FormFields(strBookmark).Name)
ElseIf (.FormFields(strBookmark).DropDown.Value = 1) Then
MsgBox ("Please select a " & Replace(Selection.FormFields(strBookmark).Name, "_", " ") & ".")
Call GoBackToPrevious(.FormFields(strBookmark).Name)
End If
End With
End Sub
and
Sub GoBackToPrevious(strPreviousField)
10: If (bool_debug) Then MsgBox ("GoBacktoPrevious - 10: strPreviousField = " & strPreviousField)
ActiveDocument.Bookmarks(strPreviousField).Range.Fields(1).Result.Select
End Sub
But when I tab out of any of the form fields, the insertion point jumps to the next form field and not back to the one that I just tabbed out of.
I know from the debug code that GoBackToPrevious is being passed the name of the current form field, but MS Word advances to the next field regardless.
I'd really appreciate it if someone can tell me how make MS Word return to and select the drop-down the user did not select appropriately instead of jumping to and selecting the next form field in the document.
Thank you.
P James Norris
EDIT:
Based on #TimothyRylatt comments, I have modified my macro and when they're called.
I have edited Validate as above (commenting out the Dim the strBookmark assignment, and I call it "on entry" to the next form field.
strBookmark is Dimed on the module's declaration section:
Option Explicit
Const bool_debug As Boolean = True
Const str_password As String = "###" ' I have a different password
Public strBookmark As String
and "on exit" from the "current" form field, I attempt to store the "current" bookmark name:
Sub StoreBookmark()
strBookmark = Selection.Bookmarks(1).Name
10: If (bool_debug) Then MsgBox ("StoreBookmark() - 10: strBookmark = " & strBookmark)
End Sub
which I call from the current form field "on exit".
But when I tab out of the current form field to the next form field, the insertion point doesn't go back to the "current" but instead stays in the next form field.
Anyone have any other suggestions/insights?
Thanks,
P James Norris

Use image to open record in another form

I am really struggling and wondered if someone could help please? I am trying to set up a database with images which I have done and used the file path of my pictures. It works well and that is great, but the next step is to have a form that just displays the pictures from each record to act as a catalogue and then When you click on a particular picture in that form it opens up another form that contains all the records and goes directly to that specified record. We have managed to get it to work for the first picture and record but can't work out how to make it go to the next record for the next image. I am trying to use a multi line form for this and only have the picture visible. I am just using a basic code as I have only just started with VBA but I believe this may not work. Can someone please help advise me?
I have used the following code which was suggested online for working with pictures and then there are a couple of other vba code to add to it. I need to create a catalogue inventory but we want to click on the picture of the inventory item which then opens a form with all the detail. I have found a work around with using the continous form and transparent buttons which take you to the specific record but you can't seem to change the layout. Ideally I am looking to have a form with just the images in a grid style going across the page and not just a list going down the page...if that makes sense?
Option Compare Database
Option Explicit
Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String
On Error GoTo Err_DisplayImage
Dim strResult As String
Dim strDatabasePath As String
Dim intSlashLocation As Integer
With ctlImageControl
If IsNull(strImagePath) Then
.Visible = False
strResult = "No image name specified."
Else
If InStr(1, strImagePath, "\") = 0 Then
' Path is relative
strDatabasePath = CurrentProject.FullName
intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
strDatabasePath = Left(strDatabasePath, intSlashLocation)
strImagePath = strDatabasePath & strImagePath
End If
.Visible = True
.Picture = strImagePath
strResult = "Image found and displayed."
End If
End With
Exit_DisplayImage:
DisplayImage = strResult
Exit Function
Err_DisplayImage:
Select Case Err.Number
Case 2220 ' Can't find the picture.
ctlImageControl.Visible = False
strResult = "Can't find image in the specified name."
Resume Exit_DisplayImage:
Case Else ' Some other error.
MsgBox Err.Number & " " & Err.Description
strResult = "An error occurred displaying image."
Resume Exit_DisplayImage:
End Select
End Function

Validating a textbox entry if duplicate display error message microsoft access

i have searched around and cant find an answer, so i am using Microsoft Access Office 2019 and cant seem to validate my textbox for a duplicate entry. The user will add a record and enter a short text primary key (which in this case is the new employee's ID) Field - [EMPID] in the [EMPDETAILS] table. The below code has worked for validating my Autonumber primary key but doesnt seem to work for a custom short text primary key and i am getting this error
"The expression you entered as a query parameter produced this error :
'VS123'
<= this is the Employee ID which is a custom short text primary key :
Private Sub unqidd_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Handler
Dim strMessage As String
'PartNum is the name of a textbox that contains the primary key, the rest are text to display.
strMessage = "Employee ID" & Me!unqidd & " already exists."
' confirm that part number doesn't already exist.
If (DLookup("[empid]", "[empdetails]", "[empID] = " & Forms![driverdetails]![unqidd])) Then
MsgBox strMessage, vbInformation, "Invalid Operation"
Cancel = True
End If
Exit_Here:
Exit Sub
Err_Handler:
MsgBox Err.Description
Resume Exit_Here
End Sub
Since you are trying to add a string into [empID] I am going to assume it is a string field, in which case you need to include quotes:
If (DLookup("[empid]", "[empdetails]", "[empID] = '" & Forms![driverdetails]![unqidd] & "'")) Then
The solution is as below :
1) Create an unbound textbox (Text1616) and set its control source as
=DLookUp("[empid]","[empdetails]","[empID] = '" & [Forms]![Driverdetails]![unqidd] & "'")
2) The textbox (Unqidd) that will need to be validated, on its beforeupdate property set the event procedure code as below :
Private Sub unqidd_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Handler
Dim strMessage As String
'PartNum is the name of a textbox that contains the primary key, the rest are text to
display.
strMessage = "Employee ID" & Me!unqidd & " already exists."
' confirm that part number doesn't already exist.
If Me.unqidd.Value = Me.Text1616 Then
MsgBox strMessage, vbInformation, "Invalid Operation"
Cancel = True
End If
Exit_Here:
Exit Sub
Err_Handler:
MsgBox Err.Description
Resume Exit_Here
End Sub
'References :
'Table Name : empdetails
'Table primary key : empid (Short text)
'Form Name : Driverdetails
'Textbox 1 : unqidd (no formats, control source : empid)
'Textbox 2 : Text1616 (Unbound Textbox)

How to get "Instance name" of the selected parts in tree

I'm writing macro that will make BOM list from the selected parts in assembly.
I can get a "Part Number" of the part in assembly, I can't get a "Instance name" of the selected parts.
Here code that call Selection tab and then try to get a names.
Set ItemSelection = CATIA.ActiveDocument.Selection
InputObjectType(0) = "Part"
SelectionStatus = ItemSelection.SelectElement3(InputObjectType, "Choose parts", false, CATMultiSelTriggWhenUserValidatesSelection, true)
If SelectionStatus = "Cancel" Then
Exit Sub
End If
If ItemSelection.Count >= 1000 Then
MsgBox "You select more then 1000 parts.", vbExclamation, MsgTextBox & "."
Exit Sub
End If
For i = 1 To ItemSelection.Count
k = k + 1
BOMTable(1,k) = ItemSelection.Item(i).PartNumber
BOMTable(2,k) = ItemSelection.Item(i).Value.Name
MsgBox BOMTable(1,k)
Next
What I do wrong?
You need to select Products if you want instance-ness.
So...
InputObjectType(0) = "Product"
...
sInstanceName = ItemSelection.Item(i).Value.Name
What happens when someone selects an Assembly/Sub-Assembly? Nothing different because Sub-Assemblies have instance names too.
However, if you want to include ONLY actual CATParts, then you have to filter value post-selection something like...
Dim oInstProd as product
set oInstProd = ItemSelection.Item(i).Value
if TypeName(oInstProd.ReferenceProduct.Parent) = "PartDocument" Then
.... do stuff with only parts...
end If
The ReferenceProduct property will give you trouble if you use cache mode (it will throw an error). But their is a workaround for that if you need it.

Input box with another single option in VBA

I have a simple inputBox to enter the ID of an employee:
id = InputBox("Enter ID NUMBER of the employee")
If id = "" Or Not IsNumeric(id) Then
MsgBox "No ID entered"
Call prepareToExit
Exit Sub
End If
Until now it was sufficient for me but now I need to be able to enter another parameter in the dialog box which will represent whether the user will use the default settings of the program or want to enter another menu to choose options (for example to change the default folder to user difined).
Can somebody explain how to add this single option in the input box.
I understood that it cannot be done with InputBox method.
All I need is another button like "Advanced..." to know if the user wants to go to additional settings.
Thanks!
Three options come to mind:
1. Use a tailored userform
2. Add another msgbox to ask about the extra option (yes/no)
3. Include the option in the entered string. This is rather simple here, but for such things, regular expressions can be very useful. It kind of depends on your target audience if this may seem too 'cryptical' for some users.
An example:
Dim rx, rxM
Dim ID As String
Dim IDvalue As Long
Dim optionFlag As Boolean
Set rx = CreateObject("vbscript.regexp")
rx.Pattern = "^(\d+)(\+)?$"
ID = InputBox("Enter ID NUMBER of the employee" & vbCrLf & _
"Add '+' for advanced options, e. g. 100345+")
If Not rx.test(ID) Then
'If ID = "" Or Not IsNumeric(ID) Then
MsgBox "No ID entered"
Call prepareToExit
Exit Sub
Else
Set rxM = rx.Execute(ID)
IDvalue = CLng(rxM(0).submatches(0))
optionFlag = (rxM(0).submatches(1) = "+")
MsgBox IDvalue & vbCrLf & optionFlag
End If