In scripts that create objects automatically, it is important to check whether such objects have already been created in the application, in order to avoid duplicated objects or even script errors.
There is no specific method to check this directly; to do so, one possibility is to search the object via Item method.
Set testObj = folder.Item(“DemoTag1”)
There are two possible outcomes:
- If a script error appears on the row, it means the object does not exist.
- I no script errors appear, it means the object exists.
Then, you will only need to handle errors in this script to be able to use this information and proceed with code development. Example:
'points to the desired location set folder = Application.GetObject("Data") on error resume next 'looks for desired object set testObj = folder.Item("DemoTag5") 'if a script error occurred If Err.Number <> 0 then MsgBox "The object doesn't exist!" 'if no script errors occurred Else MsgBox "The object exists!" End If on error goto 0
Since this verification can repeat at several points in the script, you may want to create a specific Function for this, in order to keep the script tidier and more organized.
Before proceeding to Lesson 7, we recommend reading the following articles:
Handling script errors.
KB-30665: Working with functions in E3 scripts.
Related articles
- Automation in code generation: Introduction.
- Automation in code generation: Lesson 1 – Working with vectors.
- Automation in code generation: Lesson 2 – Reading information from an Excel file.
- Automation in code generation: Lesson 3 – Changing a string into a vector.
- Automation in code generation: Lesson 4 – Scanning specific objects in a location.
- Automation in code generation: Lesson 5 – Adding objects via scripts.
- Automation in code generation: Lesson 6 – Checking for pre-existing objects.
- Automation in code generation: Lesson 7 – Events for script execution.