How do I reference an ItemProperty of a generic VBA Object variable by the property name?
I have an Outlook 2003 VBA application that includes something like the following code in a particular class:
Sub SaveAttributes(objMail as Outlook.MailItem)
Dim Field As String
Dim SaveForLater As String
Field = "Subject"
SaveForLater = objMail.ItemProperties(Field).Value
...
End Sub
I'd like to extend the app to handle MeetingItems, etc, and I thought to modify the code like so:
Sub SaveAttributes(objGeneric as Object)
...
This modification generates a runtime error 91, 'Object variable or With block not set', at the line assigning SaveForLater.
The runtime error disappears if I modify that line like so:
SaveForLater = objGeneric.ItemProperties.Item("Subject").Value
But it oddly reappears if I modify that line like so:
SaveForLater = objGeneric.ItemProperties.Item(Field).Value
Why do these lines behave differently?开发者_开发问答 And what is the proper way to reference a generic Object's item properties by name, where the name is a string variable?
This worked for me:
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
精彩评论