Add Outlook Appointments through MS Outlook

T

Tee GEE

I am curious of how to add appointments to a shared calendar in MS
Outlook. I got the imformation from Article 209963 on Microsofts
website. The automation process works but, it adds the appointment to my
personal calendar. I can't seem to find the code to change where the
appointment is posted to. I think I need to replace Me! with the shared
calendar name, but not sure. Any help would be appreciated.

A link to the article is:
http://support.microsoft.com/default.aspx?scid=kb;en-us;209963

Thank
Tom

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
T

Tee GEE

Sorry, I meant "Add Outlook Appointments using Automation in MS Access
and VBA. This is a shot in the dark, But I think I need to add another
objoutlook line that points to the specified folder or calendar.
HELP...I'm trying.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
T

Tee GEE

I added the sample code provided by Sue Mosher, and it adds an
appointment to the shared calender, with a couple of problems.

1. The entry on the calendar is blank
2. The entry is only 15 minutes long no matter what the duration I set
on the access entry form.
3. The entry ignores the time entered on the access form and creates a
calendar entry for the current time.

below is the code how it is laid out in the access form.
Any help would be appreciated.
*********************begin code*************************
Private Sub cmdAddAppt_Click()
On Error GoTo Err_cmdAddAppt_Click

'Save record first to be sure required fields are filled.
DoCmd.GoToRecord , , acNewRec

'Exit the procedure if appointment has been added to Outlook.
If AddedToOutlook = True Then
MsgBox "This appointment is already added to MS Outlook"
Exit Sub
'Add a new Appointment.
Else
Dim objOutlook As Outlook.Application
Dim objAppt As Outlook.AppointmentItem
Dim objRecurPattern As Outlook.RecurrencePattern
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objDummy As Outlook.MailItem
Dim objRecip As Outlook.Recipient
Dim strMsg As String
Dim strName As String

On Error Resume Next

'## Change str to equal calendar you want to access##
strName = "(e-mail address removed)"


Set objOutlook = CreateObject("Outlook.Application")
Set objAppt = objOutlook.CreateItem(olAppointmentItem)
Set objNS = objOutlook.GetNamespace("MAPI")
Set objDummy = objOutlook.CreateItem(olMailItem)
Set objRecip = objDummy.Recipients.Add(strName)
objRecip.Resolve

If objRecip.Resolved Then
On Error Resume Next
Set objFolder = _
objNS.GetSharedDefaultFolder(objRecip, _
olFolderCalendar)
If Not objFolder Is Nothing Then
Set objAppt = objFolder.Items.Add
If Not objAppt Is Nothing Then
With objAppt
'.Subject = "Test Appointment"
.Start = ApptDate & "" & ApptTime
.Duration = ApptLength
.Subject = Appt
.Save
.Close (olSave)
If Not IsNull(ApptNotes) Then .Body = ApptNotes
If Not IsNull(ApptLocation) Then .Location =
ApptLocation
If ApptReminder Then
.ReminderMinutesBeforeStart = ReminderMinutes
.ReminderSet = True
End If

End With

End If
End If
Else
MsgBox "Could Not Find " & Chr(34) & strName & Chr(34), , _
"User not Found"
End If

End If
'Release the Appointment object variable.
Set objOutlook = Nothing
Set objNS = Nothing
Set objFolder = Nothing
Set objDummy = Nothing
Set objRecip = Nothing
Set objAppt = Nothing

'Set the AddedToOulook flag, save the record, display a message.
AddedToOutlook = True
DoCmd.RunCommand acCmdSaveRecord
MsgBox "Appointment Added!"


Exit_cmdAddAppt_Click:
Exit Sub

Err_cmdAddAppt_Click:
MsgBox Err.Description
Resume Exit_cmdAddAppt_Click
Add_Err:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description
Exit Sub

End Sub


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
S

Sue Mosher [MVP-Outlook]

Your code uses a lot of variables like AppDate and AppTime, but I don't see
that you're setting those variables' values anywhere. My guess is that they
have no values, which would explain the behavior you're seeing.

Also note: The newsgroup interface you are using apparently does not quote
earlier messages in the thread, making your latest message so short on
detail that you risk not getting the answer you're looking for. Please take
the time to quote the original message.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
T

Tee GEE

Sue-
Are the unset variables that you are referring to in the sample code
below or somewhere else in my code. The .Start = ApptDate & "" &
ApptTime, etc. referes to the fields on a form created in access.
ApptDate, ApptTime are the field names in that form. I thought it would
pull the info from that form and send it to the calendar. I appreciate
your help. I really have not formal training in VBA as you have probably
guessed.
Thanks,
Tom
***************Begin Code********************
'Release the Appointment object variable.
Set objOutlook = Nothing
Set objNS = Nothing
Set objFolder = Nothing
Set objDummy = Nothing
Set objRecip = Nothing
Set objAppt = Nothing


******Previous Message***********************
Your code uses a lot of variables like AppDate and AppTime, but I don't
see
that you're setting those variables' values anywhere. My guess is that
they
have no values, which would explain the behavior you're seeing.

Also note: The newsgroup interface you are using apparently does not
quote
earlier messages in the thread, making your latest message so short on
detail that you risk not getting the answer you're looking for. Please
take
the time to quote the original message.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Tee GEE said:
I added the sample code provided by Sue Mosher, and it adds an
appointment to the shared calender, with a couple of problems.

1. The entry on the calendar is blank
2. The entry is only 15 minutes long no matter what the duration I set
on the access entry form.
3. The entry ignores the time entered on the access form and creates a
calendar entry for the current time.

below is the code how it is laid out in the access form.
Any help would be appreciated.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
S

Sue Mosher [MVP-Outlook]

Two tips:

1) Add an Option Explicit statement to the top of your code module. That
will make it much easier to know when you have an undeclared variable.

2) Use Me.ApptDate instead of ApptDate to refer to the value of a field on
an Access form.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
N

Nyla K

Hello Sue,

I have been trying for days to make this work and so far no luck. What I
would like to do is be able to schedule someone elses' appointments in
Outlook via MS Access. I posed this question in the Access discussion group
and I was told to try this group.

What I've been able to do successfully is to schedule my own appointment,
but scheduling someone's appointment has been difficult. I'm not good at VB
and I've used the same codes in this thread, but when I click on the command
button on my form, nothing happens. No error message, no appointments were
updated in Outlook, nothing. Can you please help me? I have to have this
completed by tomorrow for a presentation on Monday and I have no idea on how
to resolve this.
Please help.

Here are the codes that I've entered in the module:
---
Option Compare Database

Option Explicit ' Force explicit variable declaration.
Dim MyVar ' Declare variable.
MyInt = 10 ' Undeclared variable generates error.
MyVar = 10 ' Declared variable does not generate error.
Private Sub cmdAddAppt_Click()
On Error GoTo Err_cmdAddAppt_Click
' Save record first to be sure required fields are filled.
DoCmd.GoToRecord , , acNewRec

' Exit the procedure if appointment has been added to Outlook.
If AddedToOutlook = True Then
MsgBox "This appoiment is already added to Microsoft Outlook"
Exit Sub
' Add a new appointment.
Else
Dim objOutlook As Outlook.Application
Dim objAppt As Outlook.AppointmentItem
Dim objRecurPattern As Outlook.RecurrencePattern
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objDummy As Outlook.MailItem
Dim objRecip As Outlook.Recipient
Dim strMsg As String
Dim strName As String

On Error Resume Next

'# # Change str to equal calendar you want to access# #
strName = "(e-mail address removed)"

Set objOutlook = CreateObject("Outlook.Application")
Set objAppt = objOutlook.CreateItem(olAppointmentItem)
Set objNS = objOutlook.GetNamespace("MAPI")
Set objDummy = objOutlook.CreateItem(olMailItem)
Set objRecip = objDummy.Recipients.Add(strName)
objRecip.Resolve

If objRecip.Resolved Then
On Error Resume Next
Set objFolder = _
objNS.GetSharedDefaultFolder(objRecip, _
olFolderCalendar)
If Not objFolder Is Nothing Then
Set objAppt = objFolder.Items.Add
If Not objAppt Is Nothing Then
With objAppt
'.Subject = "Test Appointment"
.Start = Me.ApptDate & "" & ApptTime
.Duration = ApptLength
.Subject = Appt
.Save
.Close (olSave)
If Not IsNull(ApptNotes) Then .Body = ApptNotes
If Not IsNull(ApptLocation) Then .Location =
ApptLocation
If ApptReminder Then
.ReminderMinutesBeforeStart = ReminderMinutes
.ReminderSet = True
End If

End With

End If
End If
Else
MsgBox "Could Not Find" & Chr(34) & strName & Chr(34), , _
"User Not Found"
End If

End If


'Release the Appoint object variable.
Set objOutlook = Nothing
Set objNS = Nothing
Set objFolder = Nothing
Set objDummy = Nothing
Set objRecip = Nothing
Set objAppt = Nothing

'Set the AddedToOutlook flag, save the record, display a message.
AddedToOutlook = True
DoComd.RunCommand acCmdSaveRecord
MsgBox "Appointment Added!"

Exit_cmdAddAppt_Click:
Exit Sub

Err_cmdAddAppt_Click:
MsgBox Err.Description
Resume Exit_cmdAddAppt_Click
Add_Err:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description
Exit Sub

End Sub

--------
Thanks,
Nyla
 
S

Sue Mosher [MVP-Outlook]

Nothing jumps out at me. Does the recipient address resolve to an Exchange
mailbox user? Does Me.ApptDate show a valid date value? What happens if you
set a breakpoint and step through the code?

FYI, you can probably use Outlook's Application.CreateRecipient without even
resolving the recipient to get a Recipient object you can use with
GetSharedDefaultFolder.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
N

Nyla K

Thanks for responding, Sue.

Yes, the recipient address resides within our network exchange mailbox. I'm
using the short date value, so I'm not sure if that's considered valid or
not. I also don't know how to set break points to step through the code.
(I'm not very familiar with VB as you can tell. This is the first time that
I really had to work with it.)

I look forward to your response and suggestions.

Thanks,
Maria
 
S

Sue Mosher [MVP-Outlook]

You set a breakpoint by putting the cursor on a line where you want code
execution to pause and pressing F9. Once execution pauses, you can press F8
to step through the code line by line so you can see how the logic flow is
actually working.

You can also set a watch for a particular expression and use the Watches
window to watch its value change or use the Locals or Immediate windows to
check the value of different variables.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
N

Nyla K

Hi Sue,

I finally got the appointment to work, but the values for date, time,
length, etc., are not populating into the appropriate fields in Outlook. For
some reason they're defaulting to the current date and time. Here is the
code that I think is causing the problem. Any idea why it's doing this?

.Start = Me!ApptDate & " " & Me!ApptTime
.Duration = Me!ApptLength
.Subject = Me.Appt.Value
If Not IsNull(Me!ApptNotes) Then .Body =
Me!ApptNotes
If Not IsNull(Me!ApptLocation) Then .Location =
Me!ApptLocation
If Me!ApptReminder Then
.ReminderMinutesBeforeStart =
Me!ReminderMinutes
.ReminderSet = True
End If
.Save
.Close (olSave)
MsgBox "Appointment Added!"

Thanks,
Maria
 
S

Sue Mosher [MVP-Outlook]

You're trying to set Start to a string value, when it takes a date/time
value. Try this:

With objAppt
strDate = Me!ApptDate & " " & Me!ApptTime
If IsDate(strDate) Then
.Start = CDate(strDate)
Else
MsgBox strDate & " is not a valid date/time."
End If
' etc.
End With

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top