Inventor Doesn't Recognize Try...End Try

Inventor Doesn't Recognize Try...End Try

tdant
Collaborator Collaborator
1,030 Views
2 Replies
Message 1 of 3

Inventor Doesn't Recognize Try...End Try

tdant
Collaborator
Collaborator

It's almost as if the VBA editor inside Inventor doesn't know what a Try/Catch is. Here's my code:

 

Sub HideBreakRepresentationsInDrawing()
    If ThisApplication.Documents.Count <> 0 Then
        Dim Doc As DrawingDocument
        Set Doc = ThisApplication.ActiveDocument
    Else
        Call MsgBox("No open documents")
        Exit Sub
    End If
    
    Dim Sheets As Sheets
    Set Sheets = Doc.Sheets
    
    'recurse all sheets in drawing document
    Dim Sheet As Sheet
    For Each Sheet In Sheets
        Dim Views As DrawingViews
        Set Views = Sheet.DrawingViews
        
        'recurse all drawing views in sheet
        Dim View As DrawingView
        For Each View In Views
            'assumes the drawing view references an assembly
            Try
                Dim RefDoc As AssemblyDocument
                Set RefDoc = View.ReferencedDocumentDescriptor.ReferencedDocument 'run-time error 13: mismatch
                'recurse all components in assembly referenced by view
                Call recurseOccurrences(RefDoc.ComponentDefinition.Occurrences)
            End Try
        Next
    Next
End Sub

I'm trying to except the times when RefDoc is not an assembly document. In those cases, I just want to move to the next iteration of the For loop. The "End Try" line is red and gives the compile error when I write it, then when I run the script I get "Sub or Function not defined" on the Try line. What am I doing wrong?

0 Likes
Accepted solutions (1)
1,031 Views
2 Replies
Replies (2)
Message 2 of 3

pball
Mentor
Mentor
Accepted solution

You basically answered your question. The VBA side of Inventor does not support the Try/Catch function. iLogic however does. The best thing to do would be check what type of document the view is. It's usually better to check before doing something if it's easy. Something like below.

 

        For Each View In Views
            'Check if the drawing view reference is an assembly instead of assuming 
            If (View.ReferencedDocumentDescriptor.ReferencedDocument.DocumentType = kAssemblyDocumentObject) Then
                Dim RefDoc As AssemblyDocument
                Set RefDoc = View.ReferencedDocumentDescriptor.ReferencedDocument 'run-time error 13: mismatch
                'recurse all components in assembly referenced by view
                Call recurseOccurrences(RefDoc.ComponentDefinition.Occurrences)
            End If
        Next
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
Message 3 of 3

tdant
Collaborator
Collaborator

Ah, that makes sense. I was also able to work it out with an On Error Resume Next  at the top of that loop.

0 Likes