Discussion:
Write to UserProperty Failing
(too old to reply)
McKilty
2010-08-26 17:42:04 UTC
Permalink
I'm trying to write to an Outlook Calendar item user property. The
calendar is the local user's calendar and not a public calendar. The
purpose of writing to the calendar item is to later read that value
back... which seems obvious I guess. It can be an item property or a
folder property, I don't care.

When I write the value, there seems to be no problem, but when I read
the value back, Outlook says "Huh? A what now?" The error I get is
91: Object variable or With block variable not set.

I need help with this. I've been working on this project and had this
working at one point, but it's like Outlook is evolving to prevent my
code from working. lol. I'm losing my mind over here...

I pulled my code out to a simple function to test. The code is below:


Public Function WriteToCalendarTest()

'OUTLOOK
Dim olOutlook As outlook.Application
Dim nsNameSpace As NameSpace
Dim mItemCollection As Items
Dim myProp_R As ItemProperty

'OTHER
Dim sFilter As String

Set olOutlook = CreateObject("Outlook.Application")
Set nsNameSpace = olOutlook.GetNamespace("MAPI")
Set myFolder = nsNameSpace.GetDefaultFolder(olFolderCalendar)
Set myItems = myFolder.Items

myItems.Sort "[Start]", False
myItems.IncludeRecurrences = True

sFilter = "[End] >= '" & Format(Date - 7, "yyyy/mm/dd") & "' AND
[Start] <= '" & Format(Date + 10 & " 11:59 PM", "yyyy/mm/dd hh:nn") &
"'"
Set mItemCollection = myItems.Restrict(sFilter)

'LOOP CALENDAR ITEMS AND SET VALUE
For Each Item In mItemCollection
Set myProp_R = Item.UserProperties.Add("RAPPID", olText)
myProp_R.Value = "test text"
Item.Save
Next

Set mItemCollection = myItems.Restrict(sFilter)
For Each Item In mItemCollection
Set myProp_R = Item.UserProperties.Find("RAPPID")
MsgBox myProp_R.Value 'RETURNS ERROR 91 - Object variable or With
block variable not set
Next

Set mItemCollection = Nothing
Set myItems = Nothing
Set myFolder = Nothing
Set nsNameSpace = Nothing
Set olOutlook = Nothing

Set Utils = CreateObject("Redemption.MAPIUtils")
Utils.Cleanup

End Function
Ken Slovak
2010-08-27 17:18:24 UTC
Permalink
I'm not sure what is causing the problem or why.

I took your test code and modified it a bit to run in the Outlook VBA
project and to declare all the referenced object variables. In the time span
set by the filter I had 4 items in the filtered collection and was able to
add and then later retrieve the user property values.

I'm curious if you take your code and change your instantiation of
Outlook.Application to Set olOutlook = Application and run the code inside
of Outlook if you will have the same error results.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
Post by McKilty
I'm trying to write to an Outlook Calendar item user property. The
calendar is the local user's calendar and not a public calendar. The
purpose of writing to the calendar item is to later read that value
back... which seems obvious I guess. It can be an item property or a
folder property, I don't care.
When I write the value, there seems to be no problem, but when I read
the value back, Outlook says "Huh? A what now?" The error I get is
91: Object variable or With block variable not set.
I need help with this. I've been working on this project and had this
working at one point, but it's like Outlook is evolving to prevent my
code from working. lol. I'm losing my mind over here...
Public Function WriteToCalendarTest()
'OUTLOOK
Dim olOutlook As outlook.Application
Dim nsNameSpace As NameSpace
Dim mItemCollection As Items
Dim myProp_R As ItemProperty
'OTHER
Dim sFilter As String
Set olOutlook = CreateObject("Outlook.Application")
Set nsNameSpace = olOutlook.GetNamespace("MAPI")
Set myFolder = nsNameSpace.GetDefaultFolder(olFolderCalendar)
Set myItems = myFolder.Items
myItems.Sort "[Start]", False
myItems.IncludeRecurrences = True
sFilter = "[End] >= '" & Format(Date - 7, "yyyy/mm/dd") & "' AND
[Start] <= '" & Format(Date + 10 & " 11:59 PM", "yyyy/mm/dd hh:nn") &
"'"
Set mItemCollection = myItems.Restrict(sFilter)
'LOOP CALENDAR ITEMS AND SET VALUE
For Each Item In mItemCollection
Set myProp_R = Item.UserProperties.Add("RAPPID", olText)
myProp_R.Value = "test text"
Item.Save
Next
Set mItemCollection = myItems.Restrict(sFilter)
For Each Item In mItemCollection
Set myProp_R = Item.UserProperties.Find("RAPPID")
MsgBox myProp_R.Value 'RETURNS ERROR 91 - Object variable or With
block variable not set
Next
Set mItemCollection = Nothing
Set myItems = Nothing
Set myFolder = Nothing
Set nsNameSpace = Nothing
Set olOutlook = Nothing
Set Utils = CreateObject("Redemption.MAPIUtils")
Utils.Cleanup
End Function
Loading...