Im trying to code a script that read my excel sheets and compare the date with the date of appointments in Outlook.
I dont know why my code dont find any OLAppointment Item to compare their date with my dte on the sheet...
See the code below
Public Function CheckAppointment(ByVal argCheckDate As Date) As Boolean
Dim oApp As Object
Dim oNameSpace As Object
Dim oApptItem As Object
Dim oFolder As Object
Dim oMeetingoApptItem As Object
Dim oObject As Object
On Error Resume Next
Set oApp = GetObject(, "Outlook.Application")
If oApp Is Nothing Then Set oApp = CreateObject("Outlook.Application")
Set oNameSpace = oApp.GetNamespace("MAPI")
Set oFolder = oNameSpace.Session.GetDefaultFolder(9).Folders("aa")
CheckAppointment = False
For Each oObject In oFolder.Items
MsgBox oObject
If (oObject.Class = OLAppointment) Then
Set oApptItem = oObject
If oApptItem.Start = argCheckDate Then
CheckAppointment = True
End If
End If
Next oObject
Set oApp = Nothing
Set oNameSpace = Nothing
Set oApptItem = Nothing
Set oFolder = Nothing
Set oObject = Nothing
End Function
Public Sub Driver()
Dim dtCheck As Date
Dim sbCheck As String
dtCheck = DateValue("23/11/2013") + TimeValue("09:00:00")
If CheckAppointment(dtCheck) Then
MsgBox "Appointment found", vbOKOnly + vbInformation
Else
MsgBox "Appointment not found", vbOKOnly + vbExclamation
End If
End Sub
I have an appointment created on the calendar "aa" on 23/11/2013 but when i try to search it with my macro always give me "appointment not found". Also ive tried to show with "Msgbox" the properties of the appointments found with:
Set oFolder = oNameSpace.Session.GetDefaultFolder(9).Folders("aa")
CheckAppointment = False
For Each oObject In oFolder.Items
MsgBox oObject.Subject
but dont go anyway :\\
Sry for my poor english.
The issue is that you did not define what OLAppointment
is. Since this is macro in Excel, you need to define Outlook internal constants.
Public Function CheckAppointment(ByVal argCheckDate As Date) As Boolean
Const olAppointment = 26 ' <== Added this line and your code worked.
Dim oApp As Object
Dim oNameSpace As Object
Dim oApptItem As Object
Dim oFolder As Object
Dim oMeetingoApptItem As Object
Dim oObject As Object
On Error Resume Next ' No appointment was found since you have this line and olAppointmnet wasn't defined.
Set oApp = GetObject(, "Outlook.Application")
If oApp Is Nothing Then Set oApp = CreateObject("Outlook.Application")
Set oNameSpace = oApp.GetNamespace("MAPI")
Set oFolder = oNameSpace.Session.GetDefaultFolder(9).Folders("aa")
CheckAppointment = False
For Each oObject In oFolder.Items
MsgBox oObject
If (oObject.Class = olAppointment) Then ' <== This is why you need to define it first
Set oApptItem = oObject
If oApptItem.Start = argCheckDate Then
CheckAppointment = True
Exit For ' <== Added this exit for loop to improve performance
End If
End If
Next oObject
Set oApp = Nothing
Set oNameSpace = Nothing
Set oApptItem = Nothing
Set oFolder = Nothing
Set oObject = Nothing
End Function
Public Sub Driver()
Dim dtCheck As Date
Dim sbCheck As String
dtCheck = DateValue("4/11/2013") + TimeValue("09:00:00")
If CheckAppointment(dtCheck) Then
MsgBox "Appointment found", vbOKOnly + vbInformation
Else
MsgBox "Appointment not found", vbOKOnly + vbExclamation
End If
End Sub
Your code works, tested with calendar named aa
under the default Calendar
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.