Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook Programming Questions

Status
Not open for further replies.

Quehay

Programmer
Feb 6, 2000
804
US
I'm using VBA to create a new calendar in Outlook and populate it with dates from an Access recordset.

Problem: I want to create a new calendar, which works just fine with folders.add method. However, once it's been created already I want to be able to trap the error and resume with code that addresses the existing calendar. When I step through I get a different error # every time for the same situation--how can this be?

Another problem: I did a workaround for the first, which was to take the existing calendar and try to iterate through the appointment items and remove them using a FOR...NEXT loop. This works fine except that the indices of the items don't appear to be in straight numerical sequence (not to mention that they also begin with #1!!!).

I'll stop and the code and get an item count in the debug window, then continue stepping through. It deletes item #1 and #2 but I get an "index outside of array" error when it gets to #3 (item count was three). Is it possible that the items in this collection are not sequentially numbered?

I haven't found good documentation on the Outlook object model yet. Any hints for good sources?


Thanks
 
you can (should?) iterate through the items using

[tt]
dim anobj as object

for each anobj in folderobject.items
...
next
[/tt]

you can check that the folder doesn't exist with

[tt]
set thefolder = application.getnamespace(&quot;MAPI&quot;).folders(<foldername>)
if thefolder is null then
application.getnamespace(&quot;MAPI&quot;).folders.add ...
end if
[/tt]

hope this helps.



mr s. <;)


why does it never do what is says it does in the manual?
 
Thanks for your response. The FOR...EACH was what I first tried and it didn't work. Let me try it again...

It's so dumb that I can use a string in folders.add to create the folder and use a string in folders.item to get a reference, but I have to use the actual index in folders.remove(). Also there's no actual folder item object that I can reference to find out what the index for that item is. Looks like MSoft was not on the ball when they create this object structure!!!
 
Now I remember the problem with the FOR EACH loop:

How do you address the item? I get a type mismatch error with this:
[tt]
Private Function GetCalendar()
On Error GoTo Error_GetCalendar
Dim objItems As Object
If Len(m_strCalTitle) Then

Set objVisitCalendar = objCalendar.Folders(m_strCalTitle)

End If

For Each objItems In objVisitCalendar.Items
objVisitCalendar.Items.Remove objItems
Next


Error_GetCalendar:
MsgBox Err.Number & &quot;: &quot; & Err.Description
End Function [/tt]
 
Here's my solution:

[tt]
Private Const c_NoCalendarExists As Long = -2147221233

Private Sub GetCalendar()
On Error GoTo Error_GetCalendar
Dim objItem As Outlook.AppointmentItem

If Len(m_strCalTitle) Then

Set objVisitCalendar = objCalendar.Folders(m_strCalTitle)

End If

For Each objItem In objVisitCalendar.Items

objItem.Delete

Next

NewCalendar:

m_blnCalendarSet = True

Exit_Error_GetCalendar:
Exit Sub

Error_GetCalendar:
If Err.Number = c_NoCalendarExists Then
Set objVisitCalendar = objCalendar.Folders.Add(m_strCalTitle)
Resume NewCalendar
End If
MsgBox Err.Number & &quot;: &quot; & Err.Description
Resume Exit_Error_GetCalendar
End Sub
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top