0

This VBA problem also maybe applies to Microsoft Office although I experience it with object Inventor.Sheet. So do not hesitate to answer also with VBA+Office experience.

Initialized Inventor.Sheet can be used as:

Debug.Print oSheet.TitleBlock.Name ' prints "Title Block 1"

and also as

Debug.Print oSheet ' prints 11234869 long integer, value of default member

This dualistic behavior is caused by default property of the object.

The problem is that whenever I use

Dim TitleBlocksLargestSheet As Scripting.Dictionary
TitleBlocksLargestSheet.Add oTitleBlock, oSheet

then adding into dictionary inserts long integer value instead of object reference to oSheet.

How object reference can be inserted into the dictionary?

I suspect that dictionary Add method prefers = operation before of Set operation when both are possible.

So in the following examples I always end up with integers in dictionary items instead of objects:

'*** get a dictionary of used title blocks (corner stamps) with largest sheet size for each
For Each oSheet In oDrawingDocument.Sheets

    '** get title block of active sheet
    Dim oTitleBlock As Inventor.TitleBlock
    Set oTitleBlock = oSheet.TitleBlock

    If Not oTitleBlock Is Nothing Then
        '** add or update title block usage
        If Not TitleBlocksLargestSheet.Exists(oTitleBlock) Then
            TitleBlocksLargestSheet.Add oTitleBlock, cobj(oSheet)
        Else
            Dim oLargestSheetSeen As Inventor.Sheet
            Set oLargestSheetSeen = TitleBlocksLargestSheet(oTitleBlock)
            If oSheet.Width * oSheet.Height > oLargestSheetSeen.Width * oLargestSheetSeen.Height Then
                TitleBlocksLargestSheet.Item(oTitleBlock) = oSheet
            End If
        End If
    End If
Next oSheet

-- *** usage - retrieval from the dictionary
For Each oSheet In TitleBlocksLargestSheet.Items 'ERROR 424: Object required.
    Set oTitleBlock = oSheet.TitleBlock
    '...some other code
Next oSheet

UPDATE:

Debug.Print TypeName(TitleBlocksLargestSheet.Item(oTitleBlock))
IRxSheet ' perhaps there's no problem with storage but with retrieval?
Debug.Print VarType(TitleBlocksLargestSheet.Item(oTitleBlock))
3  ' (Long Integer)
5
  • I don't have Autodesk, so I checked on Excel by adding Combobox (that have a default "Value" property) to a dictionnary, and I didn't get problems retrieving the combobox ref. I didn't cast the combobox with Cobj. Could you add the line Debug.Print TypeName(TitleBlocksLargestSheet(oTitleBlock)) after your Add to check the type of the data stored? Commented Sep 29, 2016 at 8:49
  • Thank you, I updated the question as well as code sample (see comment in code where error is thrown). By TypeName(), it seems that object is stored although VarType() reports Long Integer. Perhaps there is no problem on storage, but on retrieval (in For Each). Could you please try that retrieval in Excel? Commented Sep 29, 2016 at 9:14
  • 1
    Vartype(Object) always returns the type of the default property, so you cannot use it here to check the type of the object stored. (cf VarType ) Commented Sep 29, 2016 at 10:06
  • BTW, Dictionary.Items() is a method that returns an array of variant, you cannot iterate with For Each oSheet if oSheet is not a Variant. Commented Sep 29, 2016 at 10:24
  • It makes perfect sense. I changed For Each to For ... Next and it works. If you add an answer, I'll accept it. Strange this is that I cannot find any data type specification in Dictionary documentation. That added to my confusion. Commented Sep 29, 2016 at 10:37

1 Answer 1

1

Dictionary.Items() is a method returning an Array of Variant [*]. You can iterate through with For Each ... only if the iterating variable is also a Variant, or you can use a For ... To ... structure.

Dim oSheet as Inventor.Sheet
Dim vSheet as Variant
Dim iSheet as long
'Use this way
For Each vSheet In TitleBlocksLargestSheet.Items
    Set oSheet = vSheet ' you may want to check that vSheet is really a Sheet before
    Set oTitleBlock = oSheet.TitleBlock
    '...some other code
Next vSheet
'or this one
For iSheet = 0 to TitleBlocksLargestSheet.Count - 1
    Set oSheet = TitleBlocksLargestSheet.Item(iSheet)
    Set oTitleBlock = oSheet.TitleBlock
    '...some other code
Next iSheet

[*] You can check this with Debug.Print TypeName(TitleBlocksLargestSheet.Items) which prints Variant()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.