Private Sub btnAddApptToOutlook_Click()
Dim olNS As Object
Dim olApptFldr As Object
If Me.Dirty Then Me.Dirty = False
If Me.chkAddedToOutlook = True Then
MsgBox "This appointment has already added to Microsoft Outlook.", vbCritical
Exit Sub
Else
Dim olApp As Object
Dim olAppt As Object
If isAppThere("Outlook.Application") = False Then
Set olApp = CreateObject("Outlook.Application")
Else
Set olApp = GetObject(, "Outlook.Application")
End If
Set olAppt = olApp.CreateItem(1)
With olAppt
If Nz(Me.chkAllDayEvent) = True Then
.Alldayevent = True
Me.txtStartDate = FormatDateTime(Me.txtStartDate, vbShortDate)
Me.txtEndDate = FormatDateTime(Me.txtEndDate, vbShortDate)
Me.cboStartTime = ""
Me.cboEndTime = ""
Dim dteTempEnd As Date
Dim dteStartDate As Date
Dim dteEndDate As Date
dteStartDate = CDate(FormatDateTime(Me.txtStartDate, vbShortDate))
dteTempEnd = CDate(FormatDateTime(Me.txtEndDate, vbShortDate))
dteEndDate = DateSerial(Year(dteTempEnd + 1), Month(dteTempEnd + 1), Day(dteTempEnd + 1))
.Start = dteStartDate
.End = dteEndDate
Dim lngMinutes As Long
lngMinutes = CDate(Nz(dteEndDate)) - CDate(Nz(dteStartDate))
lngMinutes = lngMinutes * 1440
Me.txtApptLength.Value = lngMinutes
.Duration = lngMinutes
Else
If Len(Me.cboStartTime & vbNullString) = 0 Then
Me.cboStartTime = vbNullString
End If
.Start = FormatDateTime(Me.txtStartDate, vbShortDate) _
& " " & FormatDateTime(Me.cboStartTime, vbShortTime)
If Len(Me.txtEndDate & vbNullString) > 0 Then
If Len(Me.cboEndTime & vbNullString) = 0 Then
Me.cboEndTime = vbNullString
Else
.End = FormatDateTime(Me.txtEndDate, vbShortDate) _
& " " & FormatDateTime(Me.cboEndTime, vbShortTime)
End If
End If
If Len(Me.txtApptLength & vbNullString) = 0 Then
Dim timStartTime As Date
Dim timEndTime As Date
timStartTime = FormatDateTime(Me.txtStartDate, vbShortDate) _
& " " & FormatDateTime(Me.cboStartTime, vbShortTime)
timEndTime = FormatDateTime(Me.txtEndDate, vbShortDate) _
& " " & FormatDateTime(Me.cboEndTime, vbShortTime)
.Duration = Me.txtApptLength
End If
End If
If Nz(Me.chkAllDayEvent) = False Then
.Alldayevent = False
End If
If Len(Me.cboApptDescription & vbNullString) > 0 Then
.Subject = Me.cboApptDescription
End If
If Len(Me.txtApptNotes & vbNullString) > 0 Then
.Body = Me.txtApptNotes
End If
If Len(Me.txtLocation & vbNullString) > 0 Then
.Location = Me.txtLocation
End If
If Me.chkApptReminder = True Then
If IsNull(Me.txtReminderMinutes) Then
Me.txtReminderMinutes.Value = 30
End If
.ReminderOverrideDefault = True
.ReminderMinutesBeforeStart = Me.txtReminderMinutes
.ReminderSet = True
End If
.Save
End With
Me.chkAddedToOutlook = True
If Me.Dirty Then Me.Dirty = False
MsgBox "New Outlook Appointment Has Been Added!", vbInformation
End If
ExitHere:
Set olApptFldr = Nothing
Set olNS = Nothing
Set olAppt = Nothing
Set olApp = Nothing
Exit Sub
ErrHandle:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description _
& vbCrLf & "In procedure btnAddApptToOutlook_Click in Module Module1"
Resume ExitHere
End Sub
Function isAppThere(appName) As Boolean
On Error Resume Next
Dim objApp As Object
isAppThere = True
Set objApp = GetObject(, appName)
If Err.Number <> 0 Then isAppThere = False
End Function