"For" loop throwing exception VB.net

"For" loop throwing exception VB.net

chrisw01a
Collaborator Collaborator
551 Views
9 Replies
Message 1 of 10

"For" loop throwing exception VB.net

chrisw01a
Collaborator
Collaborator

Would anyone mind sending me in the correct direction here? Attempting to make a sub to check for Ole files. The commented lines at the bottom work just fine but trying to use a For statement to simplify (lines at the top). On line 4, if the OLEFileDescriptors.Count > 0, I am getting an exception. I suspect it has something to do with the "oleFile" type but I'm lost.

 

Thank you!

 

 

Private Sub checkOle(doc As Inventor.ApprenticeServerDocument)
        'Check for 3rd party references
        Debug.Print(doc.DisplayName & ": " & doc.ReferencedOLEFileDescriptors.Count)
        For Each oleFile As Inventor.ApprenticeServerDocument In doc.ReferencedOLEFileDescriptors
            If Not oleFile Is Nothing Then
                'Handle embedded objects (They don't copy very well)
                If Not oleFile.StartsWith("Embedding") Then
                    'Add the file to the listbox
                    listboxFileView.Items.Add(oleFile)
                End If
            End If
        Next

'The code below is what I have been using and it works fine but trying to simplify...
        'Dim oleCount As Integer = doc.ReferencedOLEFileDescriptors.Count

        'If oleCount > 0 Then
        '    Dim z As Integer = 0
        '    For z = 1 To oleCount
        '        Dim tpDoc As String = doc.ReferencedOLEFileDescriptors.Item(z).FullFileName

        '        'Added 8-1-11 to handle corrupt? files that had embedded spread sheets
        '        If Not tpDoc = Nothing Then
        '            'Handle embedded objects (They don't copy very well)
        '            If Not tpDoc.StartsWith("Embedding") Then
        '                'Add the file to the listbox
        '                listboxFileView.Items.Add(tpDoc)
        '            End If
        '        End If
        '    Next
        'End If

    End Sub

 

 

0 Likes
Accepted solutions (1)
552 Views
9 Replies
Replies (9)
Message 2 of 10

Frederick_Law
Mentor
Mentor

@chrisw01a wrote:

On line 4, if the OLEFileDescriptors.Count > 0, I am getting an exception.

 


0 means no OLE Files.

 

Your old code has: 

If oleCount > 0 Then

 Use it before the for loop.

Message 3 of 10

chrisw01a
Collaborator
Collaborator

Same result. It actually passes it when the count = 0. It is when one exists that it throws the exception.

0 Likes
Message 4 of 10

WCrihfield
Mentor
Mentor

You are missing a step in this line:

If Not oleFile.StartsWith("Embedding") Then

If oleFile represents a ReferencedOLEFileDescriptor object, then that does not have a property called StartsWith.  StartsWith is a method of a String.  I assume you wanted to check the FullFileName or Displayname, or something like that, but that step is missing.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 10

Frederick_Law
Mentor
Mentor

What exception did you get?

 

My next guess is:

For Each oleFile As Inventor.ApprenticeServerDocument In doc.ReferencedOLEFileDescriptors

doc.ReferencedOLEDescriptors is not compatible with Inventor.ApprenticeServerDocument

Message 6 of 10

WCrihfield
Mentor
Mentor

Good catch.  I didn't even notice that we were dealing with an ApprenticeServerDocument here.  My earlier response was way off. 🙄  That object does appear to have the ApprenticeServerDocument.ReferencedOLEFileDescriptors property.  But I just assumed that the items within the ReferencedOLEFileDescriptors would be ReferencedOLEFileDescriptor type objects, not ApprenticeServerDocument objects.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 10

Frederick_Law
Mentor
Mentor

Will this work?

For Each oleFile In doc.ReferencedOLEFileDescriptors

 You still need to get to oleFile.Filename before you can use StartsWith.

0 Likes
Message 8 of 10

chrisw01a
Collaborator
Collaborator
No, that didn't work because now "oleFile" is not defined.
0 Likes
Message 9 of 10

Frederick_Law
Mentor
Mentor
Accepted solution

I'm guessing ReferencedOLEFileDescriptor is under Inventor:

For Each oleFile As Inventor.ReferencedOLEFileDescriptor In doc.ReferencedOLEFileDescriptors

 

0 Likes
Message 10 of 10

chrisw01a
Collaborator
Collaborator
Yes, that works! Thanks for taking the time.
0 Likes