# Testing existance of items in VBA

**URL:** <https://tech-community.robotics.abb.com/t/testing-existance-of-items-in-vba/187>\
**Category:** RobotStudio S4\
**Created:** [August 31, 2004, 10:32am UTC](https://tech-community.robotics.abb.com/t/testing-existance-of-items-in-vba/187 "2004-08-31T10:32:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jan-jaap](https://avatars.discourse-cdn.com/v4/letter/j/6bbea6/32.png) [@jan-jaap](https://tech-community.robotics.abb.com/u/jan-jaap)\
**Post date:** [August 31, 2004, 10:32am UTC](https://tech-community.robotics.abb.com/t/testing-existance-of-items-in-vba/187/1 "2004-08-31T10:32:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![John\_Wiberg](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/john_wiberg/32/1485_2.png) [@John\_Wiberg](https://tech-community.robotics.abb.com/u/John_Wiberg)\
**Post date:** [September 2, 2004, 4:20pm UTC](https://tech-community.robotics.abb.com/t/testing-existance-of-items-in-vba/187/2 "2004-09-02T16:20:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Per\_Svensson](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/per_svensson/32/3412_2.png) [@Per\_Svensson](https://tech-community.robotics.abb.com/u/Per_Svensson)\
**Post date:** [September 6, 2004, 10:25am UTC](https://tech-community.robotics.abb.com/t/testing-existance-of-items-in-vba/187/3 "2004-09-06T10:25:44Z")

</div>

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
