Help - GetItemFromID returns empty appointment item

  • Thread starter Thread starter Alan Silver
  • Start date Start date
A

Alan Silver

Hello,

I am automating Outlook 97 from Word 97. I have the EntryID of an
appointment item stored in a custom property in Word, and use this to
get hold if the appt item so I can update it.

I have code like ...

Set omNamespace = omOutlookApplication.GetNamespace("MAPI")
On Error Resume Next
Set omApptItem = omNamespace.GetItemFromID(slCalID)

The "On Error" line is there in case the item doesn't exist.

The problem is that the GetItemFromID call returns an empty item. That
means that it doesn't raise an error, implying that it found the appt
item, but the returned item object does not have any of the correct
data. The subject is empty, the start and end dates are set to
"00:00:00" and so on.

Any idea why ? The appt item definitely exists and the ID is correct.

For those who wish to see it, the full code is below. This code lives in
Word and is supposed to ensure that the user cannot close the Word doc
without having a pending calendar item for the document. All seems to
work fine, except for this one problem of getting an existing item.

TIA for any help.

Full code ...

(note that frmGetDate is a user form that gets a subject and date for
the appt item. These are stored in the module variables smSubject and
tmDueDate)

Private Sub Document_Close()
Dim olItem As Variant
Dim i As Long, j As Long

Dim slCalID As String
Dim flGetDate As frmGetDate

' check the properties for the calendar ID
Set omDocProps = ActiveDocument.CustomDocumentProperties
On Error Resume Next
' see if the property exists. If not, an error will be raised
slCalID = omDocProps.Item("CalendarItemID").Value
' delete the item (if it exists). Errors will be ignored if it doesn't exist.
omDocProps.Item("CalendarItemID").Delete
ActiveDocument.Saved = False ' force a save
ActiveDocument.Save
On Error GoTo 0
' so now we know there isn't a property for the ID.
' If there is already a calendar item, slCalID will be non-empty
' Only do the business if we are not editing the template
If Right(ActiveDocument.Name, 4) <> ".dot" Then
' editing a real document.
On Error Resume Next
ActiveDocument.Save
If Err.Number <> 0 Then
' the reason for the error-checking is that if they click Cancel on the Save dialog when it is
' a new (ie not yet saved) file, the previous line will raise an error. If this is the case,
' we don't want to do the bits below, we want to jump out of the code.
Exit Sub
End If
On Error GoTo 0
' get the Outlook object
Set omOutlookApplication = GetObject("", "Outlook.Application")
If slCalID = "" Then
' we don't have a calendar ID. Prompt for a subject and date and create a calendar item
CreateCalItem
Else
' we have an existing calendar event
' get the calendar folder
Set omNamespace = omOutlookApplication.GetNamespace("MAPI")
On Error Resume Next
Set omApptItem = omNamespace.GetItemFromID(slCalID)
' sometimes the above line can't find the item. Could be that the item was deleted.
If Err.Number = -2147221233 Then
' can't find item, create a new one
On Error GoTo 0
CreateCalItem
Else
On Error GoTo 0
If omApptItem.Start <= Now Then
Set flGetDate = New frmGetDate
With flGetDate
.Caption = "Set due date for this file"
.txtSubject = omApptItem.Subject
.dtpDueDate.Value = DateAdd("d", 1, Now)
.Show
End With
' the next If is there in case they click the X button to close the form. This avoids the
' validation step
If smSubject <> "" And tmDueDate <> "00:00:00" Then
With omApptItem
.Subject = smSubject
.Start = tmDueDate
.End = tmDueDate
.Save
End With
End If
'Else
' MsgBox "Calendar item """ & omApptItem.Subject & """ is OK", vbOKOnly
End If
End If
End If
End If
End Sub
 
"Eric said:
Is this supposed to create/locate an AppointmentItem in the default
Calendar for the user who is saving the Word document?
Yes.

If it is not their Calendar but a Shared Calendar from another
users's mailbox, ensure that they have permissions to access it.

If this were the problem, wouldn't I get some sort of error ? The
problem I have is that the AppointmentItem object is returned, but is
empty.

Also, I can create items without problem, so I don't think it's a
permissions issue.
Otherwise, maybe try passing the StoreID along with the EntryID.

How do I find that out ? Sorry if this is a stupid question, but I've
never really automated Outlook before and I'm finding it extremely hard
to get to grips with it. I can't find any decent docs, nor examples.

I was under the impression that the EntryID uniquely identified an item.
If so, what does the StoreID add ?

Thanx for the reply, any further help greatly appreciated.
 
How do I find that out ? Sorry if this is a stupid question, but I've
never really automated Outlook before and I'm finding it extremely hard
to get to grips with it. I can't find any decent docs, nor examples.

I was under the impression that the EntryID uniquely identified an item.
If so, what does the StoreID add ?

The StoreID is a unique reference for the folder.

Dim MyStoreID As String
Dim MyContactsFolder As Outlook.MAPIFolder
Set MyContactsFolder = MyNameSpace.GetDefaultFolder(olFolderContacts)
MyStoreID = MyContactsFolder.StoreID

Set MyItem = MyNameSpace.GetItemFromID(MyEntryID, MyStoreID)

Gale.
 
How do I find that out ? Sorry if this is a stupid question, but I've
The StoreID is a unique reference for the folder.

Dim MyStoreID As String
Dim MyContactsFolder As Outlook.MAPIFolder
Set MyContactsFolder = MyNameSpace.GetDefaultFolder(olFolderContacts)
MyStoreID = MyContactsFolder.StoreID

Set MyItem = MyNameSpace.GetItemFromID(MyEntryID, MyStoreID)

Gale,

Thanx for the reply. It didn't help, although I realised that this was
not due to the Outlook code, it seems that the code I have to store the
EntryID in the Word document's custom properties wasn't working. Thanx
anyway.

Just out of interest, why would you need a StoreID ? If the entryID
uniquely identifies the item, why do you need to specify the store ?

Thanx
 
The EntryID guarantees that the message is unique within a specified store (Exchange mailbox, .pst file, Public Folder hierarchy). StoreID uniquely identifies the store, so using both ensures that a unique item is returned.

I'm just fishing here, but try passing StoreID just to see what happens. You might also want to post the code you are using to create/locate the Appointment.

For the best resources on programming with Outlook, see http://www.outlookcode.com/d/vb.htm.
 
"Eric said:
The EntryID guarantees that the message is unique within a specified
store (Exchange mailbox, .pst file, Public Folder hierarchy). StoreID
uniquely identifies the store, so using both ensures that a unique item
is returned.

Ah, I understand now. Thanx.

I am just working with a single .pst file which explains why I didn't
understand (plus the fact that I'm still trying to get my head around
stores in the first place !!).
I'm just fishing here, but try passing StoreID just to see what
happens.

Tried that, same problem. I then discovered (puts on Sherlock Holmes
type hat) that the problem didn't lie with the Outlook coding at all, it
was the way I was trying to store the EntryID in the Word doc's custom
properties that wasn't working. Once I sorted that out the code worked
fine.
You might also want to post the code you are using to create/locate the
Appointment.

Well, that bit works now, but I have another question if you know the
answer ... how do I tell if an item is in the Deleted Items folder ? I'm
sure this is easy for those in the know, but I'm finding it very tough
to find any good info on automating Office apps. The object models are
huge and the docs aren't very good.
For the best resources on programming with Outlook, see
http://www.outlookcode.com/d/vb.htm.

I spotted that link in someone else's sig just before and have already
looked into the site. It looks like a gold mine !!

Thanx
 
Sorry, having read Eric's post, I realise that I misled you there. As
I've always obtained the StoreID from a MAPIFolder object, I'd always
assumed that it was unique to the folder. I've just compared the
StoreID from my own Calendar and Inbox folders and they are the same
(so I've often used the StoreID when I needn't have bothered <sigh>).

Gale.
 
Back
Top