View Full Version : Automation to Outlook


ijyoung
08-22-2008, 01:32 AM
Hi Guys
Have the following code set up.
Option Compare Database

Private Sub Check55_Click()
On Error GoTo Add_Err
'Save record first to be sure required fields are filled.
DoCmd.RunCommand acCmdSaveRecord
'Exit the procedure if appointment has been added to Outlook.
If Me!AddedToOutlook = True Then
MsgBox "This appointment 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
Set objOutlook = CreateObject("Outlook.Application")
Set objAppt = objOutlook.CreateItem(olAppointmentItem)
With objAppt
.Start = Me!ApptDate & " " & Me!ApptTime
.Duration = Me!ApptLength
.Subject = Me!FirstName & " " & Me!surname & " " & Me!NextAction

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
'Set objRecurPattern = .GetRecurrencePattern

With objRecurPattern
'.RecurrenceType = olRecursWeekly
'.Interval = 1
'Once per week
'You can hard-wire in these dates or get the
'information from text boxes, as used here.
'.PatternStartDate = #12/1/2003#
'.PatternStartDate = Me!ApptStartDate
'.PatternEndDate = #12/30/2003#
'.PatternEndDate = Me!ApptEndDate
End With
.Save
.Close (olSave)
End With
'Release the AppointmentItem object variable.
Set objAppt = Nothing
End If
'Release the object variables.
Set objOutlook = Nothing
Set objRecurPattern = Nothing
'Set the AddedToOutlook flag, save the record, display
'a message.
Me!AddedToOutlook = True
DoCmd.RunCommand acCmdSaveRecord
MsgBox "Appointment Added!"
Exit Sub
Add_Err:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description
Exit Sub
End Sub


Option Explicit

Function AddOutlookTask()
Dim OutlookApp As Outlook.Application
Dim OutlookTask As Outlook.TaskItem
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookTask = OutlookApp.CreateItem(olTaskItem)
With OutlookTask
.Subject = Me!FirstName & " " & Me!surname & " " & Me!NextAction
'.Body = Me
.ReminderSet = True
'Remind 2 minutes from now.

.ReminderTime = DateAdd("n", 2, Me!ApptDate)
'Due 5 minutes from now.
.DueDate = DateAdd("n", 5, Me!ApptDate)
.StartDate = DateAdd("n", 5, Me!ApptDate)
.ReminderPlaySound = True
'Modify path.
.ReminderSoundFile = "C:\WINNT\Media\Ding.wav"
.Save
End With
End Function

Private Sub Option98_Click()
Print AddOutlookTask()

End Sub

This based on MS's sample

Posting to Task works but generates at 438 errror and having the two scripts combined generates the "Only comments ..... after End Sub ..." error when trying to post to Outlook calandar.

The option 98 is a check box and click_55 a tick box. I need option of adding to Outlook only those records I indicate.

Anyone come up with tidier soultion?

Cheers

Ian

CyberLynx
08-23-2008, 02:15 AM
Your first Error (438) is a Object doesn't support this property or method which is exactly that. This Error falls on the code line

Print AddOutlookTask()

No good...the Print method doesn't support that. What you want to use instead is:

Call AddOutlookTask

As for your second Error: Only comments may appear after End Sub, End Function, or End Property.

Well...take a close look between the End Sub of the Check55_Click procedure and the Function AddOutlookTask() declaration. Do you notice the line Option Explicit sitting there? No can do. Option Explicit must reside within the declarations section of a code module. This line should be directly under the Option Compare Database line located at the top of a code module.

Taking care of the above items should clear things up for you.

As for a tidier solution......Looks tidy enough for me other than the fact that I wish you would have used the Code Tags ([ code ] and [ /code ]) when posting your code. :D

Good luck with your project.

.

ijyoung
08-23-2008, 03:01 PM
Many aplogies for delay in responding Cyberlynx. Been at Edinburgh Festival all day.

Have made the changes and, of course, they work. You can tell that I am somewhat inexperienced with VBA.

Thanks for your assistance. Much appreciated.

Ian