jump if findelementbypartialtext=false - vba

how do I make it so that if there is no link to click on because it is not found it continues to jump?
if GC.FindElementByPartialLinkText("CANTFINDTHIS") =false
MsgBox ("false")
else
MsgBox ("true")
end if
i get
Runtime error 438: Object doesn't support this property or method
somehow there is a true false in the boolean but i haven't figured out how to use it yet
Function FindElementByPartialLinkText(partiallinktext As String, [timeout As Long = -1], [raise As Boolean = True])

Related

ListSubItems - Method or Data Member not found

I am getting the error "Method or Data Member not found" for ListSubItems whenever I try to compile the project. I have added the SP6 component, MSCOMCTL.OCX. But still I am getting this error.
Public Function SetBoldToListItemInListView(lv As ListView, lvwItem As ListItem, bBold As Boolean) As Boolean
Dim i As Integer
On Error GoTo EH
ResetBoldInListView Not bBold, lv
lvwItem.EnsureVisible
For i = 1 To lvwItem.ListSubItems.Count
lvwItem.ListSubItems(i).Bold = True
Next
lvwItem.Bold = True
lvwItem.Selected = True
lv.Refresh
SetBoldToListItemInListView = True
ExitProc:
On Error Resume Next
Exit Function
EH:
Error_Handler "Support / CheckUncheckItemsInListView"
Resume ExitProc
End Function
This is an old question but for anyone still looking for an answer you need to add a component Microsoft Windows Common Controls 6.0 (SP6).
Go to Projects -> Components (Ctrl+ T) and check the checkbox for it.
Once this is added, you will also see extra components in the available GUI elements.

Return Error on "function returning intrinsic value type without return value" with CodeDOM

When compiling VB.NET code, by default the compiler does not give error notification if a function is not returning a value.
You can change this under the project's compiler options to return error on "function returning intrinsic value type without return value".
Just wondering is it possible to achieve the same with the CodeDOM class using VBCodeProvider?
Dim provider As New Microsoft.VisualBasic.VBCodeProvider
Dim params As New CompilerParameters
Dim results As CompilerResults
With params
.GenerateExecutable = False
.GenerateInMemory = True
.IncludeDebugInformation = False
End With
'Compile
results = Provider.CompileAssemblyFromSource(params, Source)
Return results
You can specify error control via CompilerParams:
With params
.GenerateExecutable = False
.GenerateInMemory = True
.IncludeDebugInformation = False
' add:
.WarningLevel = 4
.TreatWarningsAsErrors = True
End With
The WarningLevel relates to that setting in the C# Compile tab option, but there is not a lot of documentation on it for CodeDOM. Those would be the most strict settings, so the one you are interested in should be there.

VBA Modules and passing arguments

I have a module that can be called from any part of my application to check if a particular font is installed on the users system, and if not - install the font and check again before continuing
Main Applicaton
if RequiredFont.Run(strFontName) = FALSE Then '(error message and exit sub)
Module "RequiredFont"
Public Function Run(Font As String) As Boolean
if check(Font) = FALSE Then
Run = FALSE
Install(Font)
if check(Font) = TRUE Then Run = TRUE
Else
Run = TRUE
End If
Private Function Check(Font as String) as Boolean
'code to check the font exists on the users localmachine, returns true/false
End Sub
Private Sub Install(Font as String)
'code to install the font on the users localmachine,
End Sub
My first question is:
Is the best way to make arguments available to all functions and subs, to pass them through each time I call? (as shown above).... or is there a simple way to declare an argument as variable to the whole module when Run() is called?
My Second Question is:
Is there a way I can avoid Run() alltogether and just call the module name "RequiredFont" directly, I remember that in other languages, calling a sub by a certain name would automatically run that sub when the module is called
Thank You
EDIT - This is how my code looks now:
Private FontName As String
Private FontFile As String
Public Function Run(strFontName As String, strFontFile As String) As Boolean
FontName = strFontName
FontFile = strFontFile
Run = False
If CheckFont() = False Then InstallFont
If CheckFont() = True Then
Run = True
Else
'message error"
End If
End Function
Private Function CheckFont() As Boolean
'code to check if the font is installed
On Error Resume Next
'Create a temporary StdFont object
With New StdFont
' Assign the proposed font name
.Name = FontName
' Return true if font assignment succeded
If (StrComp(FontName, .Name, vbTextCompare) = 0) = False Then
CheckFont = False
Else
CheckFont = True
End If
End With
End Function
Private Sub InstallFont()
' code to install the font
MsgBox "You need the following font installed to continue." _
& vbNewLine _
& vbNewLine & "'" & FontName & "'" _
& vbNewLine _
& vbNewLine & "Click OK to launch the font. Please click the INSTALL button at the top"
OpenFile (PATH_TO_FONTS & FontFile)
End Sub
Using function arguments is a good coding practise, that way you know exactly what goes in and what goes out a function.
You can however use a global variable, which would be set once when Run is called and still be accessible to the other functions.
'could also be Private to hide it from other modules
Public myFont As String
Public Function Run(Font As String) As Boolean
myFont = Font
'...
End Sub
Private Function Check() as Boolean
' you can access myFont here
End Sub
Private Sub Install()
'idem
End Sub
Regarding your second question, I don't think you can.
You can declare optional functions, and set defaults:
Public Function fxMyFunction _
(Optional lngProj As Variant, _
Optional strFruit As Variant = "banana", _
Optional booTest As Boolean = False) As String
'' IsMissing requires that lngProj be a Variant
booNoProject = IsMissing(lngProj)
fxMyFunction = strFruit
End Function
The Optional arguments must follow non-optional arguments.
About functions that "run on inclusion"
You do have to call functions and subs by name. There is no "self-running function" feature for a VBA standard module. VBA "includes" all modules on compiling.
VBA class modules are where you will find the equivalent of constructors. Investing in VBA's version of object-orientation doesn't seem helpful for your current need. If you do go that direction, some aspects will start looking familiar to you (though perhaps just enough to get frustrating, as OO remains a feature that was added later and looks the part).
As #z states, you can use a global variable, although this is bad practice.
Regarding question 2, you can give your function a unique name and omit naming your module to run it, e.g.
findOrInstallFont(Fontname)

"User defined type not defined" when trying to define a new "process"

I am trying to redirect command line output to a list box in a vba macro, and I've found some code that I think might point me in the right direction, but I keep on getting the same error. When I use this code
Function ReadCmdOutput(ByVal applicationName As String,
Optional ByVal applicationArgs As String = "", Optional ByVal
workingDirectory As String = "", Optional ByVal showWindow As Boolean
= False) As String
Try
Dim processObj As New Process
processObj.StartInfo.UseShellExecute = False
processObj.StartInfo.RedirectStandardOutput = True
processObj.StartInfo.FileName = applicationName
processObj.StartInfo.Arguments = applicationArgs
processObj.StartInfo.WorkingDirectory = workingDirectory
If showWindow = True Then
processObj.StartInfo.CreateNoWindow = False
Else
processObj.StartInfo.CreateNoWindow = True
End If
processObj.Start()
processObj.WaitForExit()
Return processObj.StandardOutput.ReadToEnd
Catch ex As Exception
Return ""
End Try
End Function
It gives me the error in the title and highlights the first declaration line.
Question: What does it take to define a new "process".
Bonus points: Helping me with command line output redirect!
This can't be VBA since there is no Try/Catch in VBA or return statements. This looks like VB.NET. Either way, the compiler is telling you that there is currently no reference to any dll that contains the process object.
If this is VB.NET, you need to add Imports System.Diagnostics

VB6 debugging- doesn't go to error handling code

I'm maintaining a vb6 project(ActiveX DLL). When debugging, the app run into the following function:
Public Function HasValue(ByVal vValue) As Boolean
On Error GoTo Err
If IsMissing(vValue) Then
HasValue = False
ElseIf IsNull(vValue) Or Len(vValue) = 0 Then
HasValue = False
ElseIf isEmpty(vValue) Then
HasValue = False
Else
HasValue = True
End If
Exit Function
Err:
If IsArray(vValue) Or IsObject(vValue) Then
HasValue = True
Else
HasValue = False
End If
End Function
and it stops at the line
ElseIf IsNull(vValue) Or Len(vValue) = 0 Then
vValue is a custom object, contains some properties(obviously, not null).
Although I didn't put any break point there, the app stopped there and alerted error dialog saying that "Run-time error '438': Object doesn't support this property or method".
We had error handling code but the app didn't run to error handling code. It just stopped at the line causing the error and I had to stop the application.
Do you have any idea about that?
Thank you very much.
As far as getting the popup running in the debugger, it is probably related to your "Error Trapping" settings in the IDE. Go To Tools->Options->General and see what selected under "Error Trapping". At first glance it seems odd that your error handler is testing the vValue in the event of an error. It makes more sense to me based on my limited understanding of this method to move both the IsArray and IsObject conditions up into the main testing logic. Just my 2 cents :)
As far as i know vb6 does not support boolean short evaluation in
ElseIf IsNull(vValue) Or Len(vValue) = 0 Then
so Len(vValue) = 0 is executed even if IsNull(vValue) is true.
changing your code to
...
ElseIf IsNull(vValue) Then
HasValue = False
ElseIf Len(vValue) = 0 Then
HasValue = False
ElseIf ...
might solve the problem