Testing existance of items in VBA

Hello,

What is the best way to test for the existance of a certain object, for instance a path? I tried:

if activeStation.paths(“myPath”)=null then

Set myPath= ActiveStation.Paths.Add

end if

but this results in an error. Now I do it in an error handler, but is this the best way?

Regards,

Jan-Jaap Kostelijk

If you are searching for a named path then it’s using a for loop, like this:

Sub FindPath()
Dim strS As String
strS = InputBox(“Input path to search for”)
Dim pthP As Path
Dim pthFound As Path
Dim blFound As Boolean
For Each pthP In ActiveStation.Paths
If pthP.Name = strS Then
blFound = True
Set pthFound = pthP
Exit For
End If
Next
If blFound = True Then
MsgBox "Found the path: " & pthFound.Name
Else
MsgBox “Didn’t find the path”
End If
End Sub

But if you are just interested in if there is any paths at all then use count, like this:

Sub AreThereAnyPaths()
If ActiveStation.Paths.Count > 0 Then
MsgBox “There are paths in the station.”
Else
MsgBox “There are no paths in the station.”
End If
End Sub

Here’s another example

Function CheckIfObjectExist(ObjectType As RsObjectType, myObject As String) As Boolean

On Error Resume Next

Dim tmpObject As RsObject

Select Case ObjectType
Case RsObjectType.rsObjectTypePath
Set tmpObject = ActiveStation.Paths(myObject)
Case RsObjectType.rsObjectTypeTarget
Set tmpObject = ActiveStation.Targets(myObject)
End Select

If tmpObject Is Nothing Then
CheckIfObjectExist = False
Exit Function
End If

CheckIfObjectExist = True

End Function

Sub testObject()
MsgBox CheckIfObjectExist(rsObjectTypePath, “Path1”)
MsgBox CheckIfObjectExist(rsObjectTypeTarget, “Target1:1”)
End Sub