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
Jan-Jaap
Comments
-
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 SubBut 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 SubJohn
Developer Center0 -
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 FunctionSub testObject()
MsgBox CheckIfObjectExist(rsObjectTypePath, "Path1")
MsgBox CheckIfObjectExist(rsObjectTypeTarget, "Target1:1")
End SubPer Svensson
Company Specialist
ABB Automation Technology Products0
Categories
- All Categories
- 5.5K RobotStudio
- 396 UpFeed
- 18 Tutorials
- 13 RobotApps
- 297 PowerPacs
- 405 RobotStudio S4
- 1.8K Developer Tools
- 250 ScreenMaker
- 2.8K Robot Controller
- 314 IRC5
- 59 OmniCore
- 7 RCS (Realistic Controller Simulation)
- 794 RAPID Programming
- AppStudio
- 3 RobotStudio AR Viewer
- 18 Wizard Easy Programming
- 105 Collaborative Robots
- 5 Job listings