I am trying to fix a VBA that uses Selenium1.0.23 to open a site and login. My client said everything is working on his end except that the VBA must be putting the wrong Username and Password because the site is giving the wrong username-password message.
Now I got the script and try to run it but I got stuck on the functions giving "Argument Not Optional". Below is the script:
Here is the part with error. It highlights the ".setVariation" part.
Private Function setVariationOnPage(browser As SeleniumWrapper.WebDriver, variations As Scripting.Dictionary) As Long
Dim name
For Each name In variations.keys
ProductVariations.setVariation browser, variations(name)
Next name
End Function
I believe the above function is calling another function on a separate module because there is a module name ProductVariations and a function called setvariation in it.
Public Function setVariation(browser As SeleniumWrapper.WebDriver, ByVal name As String, value As String)
On Error GoTo setVariationError
Dim li As WebElement
Dim lis As Collection
Set lis = getVariationListItems(browser, name)
If lis Is Nothing Then
GoTo setVariationError
Exit Function
End If
For Each li In lis
Dim link As WebElement
Set link = getLinkFromVariationListItem(li)
If LCase(Trim(getVariationValueNameFromListItem(li))) = LCase(Trim(value)) Then
If InStr(li.getAttribute("class"), "active") = 0 Then
link.Click
ScrapingUtil.waitForPageToLoad browser
If InStr(li.getAttribute("class"), "active") = 0 Then
GoTo setVariationError
End If
End If
Exit Function
End If
Next li
setVariationError:
Dim msg As String
msg = "Unable to set variation: ""{name}"" = ""{value}"""
msg = Replace(msg, "{name}", name)
msg = Replace(msg, "{value}", value)
Err.Description = msg
Err.raise 1
End Function
Then obviously this function calls another function and that function calls another. It's hell to troubleshoot it and I really need help.
making parameter as optional..
Public Function setVariation(browser As SeleniumWrapper.WebDriver, ByVal name As String, optional value As String).
Related
I have been trying this out for a while now and I keep getting the same error of a mismatch, code is below.
Function listAvailableFiles(FolderPath As String, DropDown As ListBox)
Dim File As String
Dim j As Integer
File = Dir(FolderPath & "\*.cdr")
Do While File <> ""
DropDown.AddItem File
File = Dir
Loop
End Function
Private Sub UserForm_Activate()
Dim DimensionFolder As String
DimensionFolder = "C:\Users\cwats\OneDrive - Watson Memorials\Watson Memorials\Designs Template and Drawings\MD UK\Memorial Shapes\Foundations"
Call listAvailableFiles(DimensionFolder, ListDimensionFilesDropdown)
End Sub
I am assuming I am not calling the function correclty but any help on this would be greatly appreciated.
You'll need to change the declaration line for your function as follows...
Function listAvailableFiles(FolderPath As String, DropDown As msforms.ListBox)
I have a function where the intention is to return either a string to signify that the function has completed and retrieved the required value or an error to indicate there has been an issue.
To see whether I could do this, I bashed together the following:
Private Function one()
Debug.Print TypeName(two(False))
End Function
Private Function two(b As Boolean) As Variant
Dim e As New ErrObject
If (b) Then
two = True
Else
two = e.number
End If
End Function
Now, this fails at two = e.number because you don't appear to be able to set an error number in this way - the syntax is incorrect.
I could use err.raise but then the intention is to pass the whole error object back as I can then have a custom number and fill in my own description etc...
How do you actually pass the err object back? Can you actually do it and is this the most effective way in achieving what I am setting out to do?
Thanks
You don't. Err is a globally-scoped Function from the standard library VBA.Information module, that returns an ErrObject instance.
the intention is to pass the whole error object back as I can then have a custom number and fill in my own description
You don't need to pass the whole error object back to throw custom errors.
Const ERR_SomethingBad = vbObjectError + 42
Err.Raise ERR_SomethingBad, "MyModule.MyProcedure", "Uh-oh"
Err is already in global scope - you don't need to pass error objects around... and in fact, you simply can't. Because the ErrObject class isn't creatable. This:
Dim e As ErrObject
Set e = New ErrObject
Throws run-time error 429 "ActiveX can't create object".
The very idea behind errors (and exceptions, if you're any familiar with languages that feature exceptions), is that they replace the old archaic "return status code" way, e.g.:
''' DON'T DO THIS, IT's 2017!!!
Public Function DoSomething(ByVal p1 As Double, ByRef outResult As Double) As Long
If p1 = 0 Then
DoSomething = 11 'return code for division by zero error
Exit Function
End If
outResult = 42 / p1
DoSomething = 0 'return code for OK - everything went well!
End Function
With this pattern every procedure is a Function that returns an error code; the actual return value is passed as a ByRef / out parameter, and the caller would have to do this before they can trust the result:
Dim result As Double
Dim e As Long
e = DoSomething(42, result)
If e = 0 Then
' happy path
Else
MsgBox Error$(e) ' error path
End If
The On Error statement streamlines the code by taking most error-handling concerns out of the "happy path".
I am trying to be more "object oriented" in my VBA code. However, I am having trouble passing variables through to functions. Here, I get an invalid qualifier error message on the IsEmpty function.
How can I correct my code?
Sub test_too_much_data()
If toomuchdata("Data input", "B1018") = False Then
MsgBox ("Sorry, the tool can only accomodate 1000 rows.")
Exit Sub
End If
End Sub
Function toomuchdata(sheet As String, range As Variant) As Boolean
toomuchdata = IsEmpty(Sheets("String")).range(range)
End Function
Thank you!
Update your Function code to something like below:
Function toomuchdata(sheetStr As String, RngStr As String) As Boolean
toomuchdata = IsEmpty(Sheets(sheetStr).Range(RngStr).Value)
End Function
I am having a weird issue when I'm calling a function from another function inside an if statement. I defined a Sub, which I am using to test this function, and it calls on a function which relies on another function I use to compare values. The code is below, which should make things clear. Essentially, I don't understand how it's possible for the code to work fine in the print statement, and then throw an error in the GetMatch function. I appreciate any help.
Edit: All of a sudden everything works. Do debugging breakpoints affect the program? I haven't changed anything, but CStr() is no longer required when calling GetMatch. I haven't touched any of the subs or functions, but I did clear some breakpoints. If I find what caused it, I'll post a solution. Thanks for the help everyone.
Edit2: Maybe this is a bug with VBA? If I add the CStr() option to the indexOrder(...) calls, things work. Before, without the CStr() options, things did not work. Now, strangely enough, after using the CStr(), I am able to remove the CStr()'s entirely from the program, and things work again. It breaks if I undo to the point where they weren't there originally though. I don't know what this could be, but if anyone has an explanation, I'm very interested. Thanks
Sub testFind()
Dim SortOrder() As Variant
Dim indexOrder() As Variant
SortOrder = Array("Contact Email", "Last Name", "First Name", "Attempt #", "Customization", "Template #")
indexOrder = Array("First Name", "First Name", "Template #", "Customization")
findAndReplace(indexOrder, SortOrder)
End Sub
Function findAndReplace(indexOrder As Variant, list As Variant) As Variant
Dim indexLength As Integer
Dim listLength As Integer
Debug.Print TypeName(indexOrder(0)) ' Identifies as String
indexLength = getVariantLength(indexOrder)
listLength = getVariantLength(list)
Debug.Print GetMatch(CStr(indexOrder(1)), CStr(indexOrder(1))) ' This works fine. Returns 0 as it should
If GetMatch(indexOrder(1), indexOrder(1)) = 0 Then ' Fails with ByRef error
Debug.Print ("Why don't I work?")
End If
End Function
Function GetMatch(A As String, B As String) As Integer
A = Trim(A)
B = Trim(B)
If (IsEmpty(A) Or Trim(A) = "") Then
GetMatch = 1
Exit Function
ElseIf (IsEmpty(B) Or Trim(B) = "") Then
GetMatch = -1
Exit Function
End If
GetMatch = StrComp(A, B, vbTextCompare)
End Function
Function getVariantLength(vari As Variant) As Integer
If IsNull(index) Then
getVariantLength = 0
Else
getVariantLength = UBound(vari) - LBound(vari) + 1
End If
End Function
You don't have a Sub vartest() or Function vartest(), it's trying to call a sub/function that doesn't exist, or at least it isn't included.
Edit: You aren't doing anything with the function. A function will return a value, a sub will 'do' something. You need to assign a variable to whatever it returns, or do a MessageBox or some other way of returning the value.
The next issue is it's trying to call a function getVariantLength() that isn't defined or listed.
Just a very quick question: I want to create a function with an optional parameter because I can't find a need for a parameter in the function. As a result I have coded the following function in visual basic:
Sub characterListLength(ByVal Optional)
Dim rowCount As Integer
Dim endOfArray As Boolean
While endOfArray = False
If dataArray(0, rowCount) And dataArray(1, rowCount) = "" Then
arrayLength = rowCount
endOfArray = True
Else
rowCount += 1
End If
End While
End Sub
However on the first line:
Sub characterListLength(ByVal Optional)
There is an error where an identifier is expected where the code says (ByVal Optional). I am not sure how to fix this error and have the optional parameter. If anyone could explain what else I need to do to fix it, that would be very useful.
You need an actual variable, something like:
Sub characterListLength(Optional ByVal optionalNumber As Integer = 0)
If you said:
because I can't find a need for a parameter in the function
Then use method without parameters:
Sub characterListLength()
'Here your code
End Sub
You need to give the parameter a name and switch the order of the keywords
Sub characterListLength(Optional ByVal p = Nothing)
A better "dot-nettier" alternative to optional parameters is to use overloaded methods. Consider following:
Overloads Sub ShowMessage()
ShowMessage("This is the default alter message")
End Sub
Overloads Sub ShowMessage(ByVal Message As String)
Console.WriteLine(Message)
End Sub
Written like this you can call the above method both ways:
ShowMessage() 'will display default message
ShowMessage("This is custom message") 'will display method from the parameter
Demo: http://dotnetfiddle.net/OOi26i