This VBA problem also maybe applies to Microsoft Office although I experience it with autodesk-inventor 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)
Debug.Print TypeName(TitleBlocksLargestSheet(oTitleBlock))after your Add to check the type of the data stored?TypeName(), it seems that object is stored althoughVarType()reportsLong Integer. Perhaps there is no problem on storage, but on retrieval (inFor Each). Could you please try that retrieval in Excel?Dictionary.Items()is a method that returns an array of variant, you cannot iterate withFor Each oSheetifoSheetis not aVariant.For EachtoFor ... Nextand 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.