如何通过属性名称引用通用VBA对象变量的ItemProperty?

时间:2011-01-21 17:50:14

标签: outlook-vba

我有一个Outlook 2003 VBA应用程序,其中包含类似以下代码的特定类:

Sub SaveAttributes(objMail as Outlook.MailItem)
Dim Field As String  
Dim SaveForLater As String  

Field = "Subject"  
SaveForLater = objMail.ItemProperties(Field).Value  

...  

End Sub  

我想扩展应用程序以处理MeetingItems等,我想像这样修改代码:

Sub SaveAttributes(objGeneric as Object)  
...  

此修改在分配SaveForLater的行生成运行时错误91,'对象变量或未设置块'。

如果我像这样修改那行,运行时错误就会消失:

SaveForLater = objGeneric.ItemProperties.Item("Subject").Value

但如果我这样修改那条线就会奇怪地重新出现:

SaveForLater = objGeneric.ItemProperties.Item(Field).Value

为什么这些线的行为不同?什么是通过名称引用通用Object的项属性的正确方法,其中名称是字符串变量?

1 个答案:

答案 0 :(得分:1)

这对我有用:

Sub SaveAttributes(objGeneric As Object)
    Dim Field As String
    Dim SaveForLater As String
    Dim itemProps As Outlook.ItemProperties
    Dim itemProp As Outlook.ItemProperty

    Field = "Subject"

    Set itemProps = objGeneric.ItemProperties
    Set itemProp = itemProps.Item(Field)

    SaveForLater = itemProp.Value

End Sub