Hi
I have a Sub "FindAndSendAppointments()" searching through Outlook Calendar using AdvancedSearch-method. Then I edit each found calendar item to be an appointment, save it and finally send it through e-mail.
The save causes the returned Search-Object from the AdvancedSearch to change because the calendar item does not meet the search criteria anymore and thus I'm having troubles looping through this always changing Search-Object.
Here is the code, hope it helps:
If the AdvancedSearch returns e.g. 3 objects, the loop works for the first 2 and then Set myItem = MySearch.Results.GetNext() will result in "Nothing", even if MySearch.Results.Count still returns the "1" remaining appointment.
How could I cope with the always changing Search-Object? Any ideas?
Thanks alot!
Best regards
progger
I have a Sub "FindAndSendAppointments()" searching through Outlook Calendar using AdvancedSearch-method. Then I edit each found calendar item to be an appointment, save it and finally send it through e-mail.
The save causes the returned Search-Object from the AdvancedSearch to change because the calendar item does not meet the search criteria anymore and thus I'm having troubles looping through this always changing Search-Object.
Here is the code, hope it helps:
Code:
Option Explicit
Public m_SearchComplete As Boolean
Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
If SearchObject.Tag = "MySearch" Then
m_SearchComplete = True
End If
End Sub
Sub FindAndSendAppointments()
Dim Scope As String
Dim Filter As String
Dim MySearch As Outlook.Search
Dim lCounter As Long
Dim myItem As Object
Dim myRequiredAttendee As Outlook.Recipient
m_SearchComplete = False
'Establish scope for default Calendar folder
Scope = "'" & Application.Session.GetDefaultFolder(olFolderCalendar).FolderPath & "'"
'Establish Filter, which finds all appointments containing "Test" in the subject
'and which do not already contain the required attendee "test@test.com"
Filter = _
"(NOT" & Chr(34) & "urn:schemas:httpmail:displayto" & Chr(34) & " LIKE '%test@test.com%' AND NOT" & _
Chr(34) & "urn:schemas:httpmail:displayto" & Chr(34) & " LIKE '%Test%' AND " & _
Chr(34) & "http://schemas.microsoft.com/mapi/proptag/0x0037001f" & Chr(34) & " LIKE '%Test%')"
Set MySearch = Application.AdvancedSearch(Scope, Filter, True, "MySearch")
While m_SearchComplete <> True
DoEvents
Wend
Set myItem = MySearch.Results.GetFirst()
Do While Not myItem Is Nothing
myItem.MeetingStatus = olMeeting
Set myRequiredAttendee = myItem.Recipients.Add("Test (test@test.com)")
myRequiredAttendee.Type = olRequired
myItem.Save
myItem.Send
Set myItem = MySearch.Results.GetNext()
MsgBox "Remaining items: " & CStr(MySearch.Results.Count)
Loop
End Sub
If the AdvancedSearch returns e.g. 3 objects, the loop works for the first 2 and then Set myItem = MySearch.Results.GetNext() will result in "Nothing", even if MySearch.Results.Count still returns the "1" remaining appointment.
How could I cope with the always changing Search-Object? Any ideas?
Thanks alot!
Best regards
progger