如何将具有默认成员的对象添加到 Scripting.Dictionary 作为对象引用,而不是作为默认成员值?
How to add object with default member into Scripting.Dictionary as object reference, not as default member value?
这个 VBA 问题也可能适用于 Microsoft Office,尽管我在 autodesk-inventor 对象 Inventor.Sheet
中遇到过。所以不要犹豫回答也有VBA+Office经验。
已初始化Inventor.Sheet
可用作:
Debug.Print oSheet.TitleBlock.Name ' prints "Title Block 1"
以及
Debug.Print oSheet ' prints 11234869 long integer, value of default member
这种二元行为是由对象的默认 属性 引起的。
问题是每当我使用
Dim TitleBlocksLargestSheet As Scripting.Dictionary
TitleBlocksLargestSheet.Add oTitleBlock, oSheet
然后添加到字典中插入长整数值而不是对 oSheet
的对象引用。
如何将对象引用插入到字典中?
我怀疑字典 Add
方法在 Set
操作之前更喜欢 =
操作,当两者都可能时。
所以在下面的示例中,我总是在字典项而不是对象中以整数结尾:
'*** 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
更新:
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)
Dictionary.Items()
是一个 method 返回一个 Array of Variant
[*]。仅当迭代变量也是 Variant
时,您才可以使用 For Each ...
进行迭代,或者您可以使用 For ... To ...
结构。
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
[*] 您可以使用 Debug.Print TypeName(TitleBlocksLargestSheet.Items)
进行检查,它会打印 Variant()
这个 VBA 问题也可能适用于 Microsoft Office,尽管我在 autodesk-inventor 对象 Inventor.Sheet
中遇到过。所以不要犹豫回答也有VBA+Office经验。
已初始化Inventor.Sheet
可用作:
Debug.Print oSheet.TitleBlock.Name ' prints "Title Block 1"
以及
Debug.Print oSheet ' prints 11234869 long integer, value of default member
这种二元行为是由对象的默认 属性 引起的。
问题是每当我使用
Dim TitleBlocksLargestSheet As Scripting.Dictionary
TitleBlocksLargestSheet.Add oTitleBlock, oSheet
然后添加到字典中插入长整数值而不是对 oSheet
的对象引用。
如何将对象引用插入到字典中?
我怀疑字典 Add
方法在 Set
操作之前更喜欢 =
操作,当两者都可能时。
所以在下面的示例中,我总是在字典项而不是对象中以整数结尾:
'*** 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
更新:
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)
Dictionary.Items()
是一个 method 返回一个 Array of Variant
[*]。仅当迭代变量也是 Variant
时,您才可以使用 For Each ...
进行迭代,或者您可以使用 For ... To ...
结构。
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
[*] 您可以使用 Debug.Print TypeName(TitleBlocksLargestSheet.Items)
进行检查,它会打印 Variant()