Check if titleblockdefiition exists

Check if titleblockdefiition exists

frederic.vandenplas
Collaborator Collaborator
670 Views
2 Replies
Message 1 of 3

Check if titleblockdefiition exists

frederic.vandenplas
Collaborator
Collaborator

How to avoid an error is thrown if titleblockdefinition does not exist?

 

I could use try catch, on error resume next and so on,  but this is a bad practice if you know that something can go wrong (in this case, the titleblockdef is not present) 

 

 

Sub testtitleblock()
 Dim oDrawDoc As DrawingDocument
 Set oDrawDoc = ThisApplication.ActiveDocument
 
 If oDrawDoc.TitleBlockDefinitions.Item("Notexistingtitleblockdef") Is Nothing Then
 MsgBox "not found"
 End If
 
End Sub

 

 

If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
0 Likes
Accepted solutions (1)
671 Views
2 Replies
Replies (2)
Message 2 of 3

MechMachineMan
Advisor
Advisor
Accepted solution

I dont think it's necessarily a bad practice as long as you understand what you are doing.

 

I've run into cases where finding if a value exists in a large collection or enum is done most simply and quickly by trying to call that specific item, but catching the error for it it fails and returning the results.

 

The other option is looping through the collection:

 

Function TBExists(oTBName) As Boolean
     Exists = False
     For Each oTB in oDrawDoc.TitleBlockDefinitions
               If oTB.Name = oTBName Then
                        Exists = True
               End if
      Next
      Return Exists
End Function

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 3

frederic.vandenplas
Collaborator
Collaborator

@MechMachineMan

 

Thank you for this alternative, i ended up with this code (for those who are interested)

 

Dim boolBorderFound As Boolean = False
For Each oBorder In oDrawDoc.BorderDefinitions If oBorder.Name = oNewBorder Then boolBorderFound = True End If Next oBorder If boolBorderFound = False Then oNewborderDefinition = oSourceDrawDoc.BorderDefinitions.Item(oNewBorder).CopyTo(oDrawDoc, True) End If
If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
0 Likes