Problem with a Search-Object resulting from an AdvancedSearch

Status
Not open for further replies.

progger

Member
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:




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
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
In a first loop you could just get the objects and store them in a

collection. Then iterate through the collection, and work that.

BTW: There's an AdvancedSearchComplete event. You might want to use that

instead of the While-Wend loop.

Best regards

Michael Bauer

Am Wed, 20 Jan 2010 17:08:47 -0500 schrieb progger:


> 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:

> 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
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
S Outlook 2007 Calendar instant search problem. Windows 7 Using Outlook 4
S Outlook 2007 Calendar instant search problem. Windows 7 Using Outlook 0
W Outlook 2016 search problem persists after applying all known solutions Using Outlook 12
D Icloud Outlook 2010 Task/Reminder Search Problem Using Outlook 1
P Outlook calendar and contacts sync problem-outlook disconnects Using Outlook.com accounts in Outlook 2
S Archiving and Likely Modified Date Problem Using Outlook 3
R Problem moving file “Email folders.pst” to new PC Using Outlook 5
S Problem Checking the available stores in my Inbox (Outlook VBA) Outlook VBA and Custom Forms 0
witzker GetAssignedView Problem Outlook VBA and Custom Forms 2
M Outlook 2010 Problem with OutLook 2010 32 bit, after Windows Auto Update Using Outlook 3
Marc2019 Outlook 2016 Font Problem Using Outlook 5
X I have met my waterloo trying to resolve embedded graphics problem with outlook 2007 and now 2016 Using Outlook 1
D Problem with custom form including _DocSiteControl1 Outlook VBA and Custom Forms 0
D Sort Problem with Sent Folders Using Outlook 1
S Conditional formatting problem with "is not empty" and categories Using Outlook 2
Mark Foley The upload of "Calendar" failed. There was a problem with the request. Using Outlook 6
avant-guvnor Import csv problem Using Outlook 7
katehawkins Outlook 2013 Ost to pst conversion problem Using Outlook 1
AbbieWhite The problem with a fairly large file. Using Outlook 3
I Outlook 2013 Send problem - 'Not Responding' forever Using Outlook.com accounts in Outlook 10
EmelineGueguen Help to understand the problem of work Using Outlook 1
S problem with convert Using Outlook 1
S SendFromAccount - Problem trying to test existing value in open email Outlook VBA and Custom Forms 2
DruinaBiscardi unexpected problem in outlook Using Outlook 1
V Outlook 2003 problem with Windows 10 Creators Update 1709 Using Outlook 0
G Windows Update Causes BCM Database Access Problem? BCM (Business Contact Manager) 4
Grimev66 problem with conversion ost to pst Using Outlook 2
R Problem with searching public folders Exchange 2013/16 Exchange Server Administration 2
J Problem with Outlook 2016 new mail tray icon alert (envelope in the systems tray) Using Outlook.com accounts in Outlook 0
S Problem running Command button code Outlook VBA and Custom Forms 2
A .ost file problem Using Outlook 4
G PROBLEM REGARDING OUTLOOK STORAGE LANGUAGES Using Outlook 4
PetraTech Odd Folder View Problem Using Outlook 3
V iCloud problem Using Outlook 9
D Reply Problem with Outlook.com account Using Outlook.com accounts in Outlook 0
MattC Problem with Outlook 2007 & iCloud / duplicate tasks Using Outlook 1
V Problem moving folders Using Outlook 4
C Problem tracking meeting responses Using Outlook 3
Mark Foley Problem subscribing to shared calendar Using Outlook 1
K Problem with FreeBusy (before 1pm only) Outlook VBA and Custom Forms 7
skylark53 Advanced recurrence problem Using Outlook 2
E RSS export as OPML problem Using Outlook 4
D outlook 2013 contact list problem Using Outlook 0
T Problem with .Recipients.Add("alias") Outlook VBA and Custom Forms 3
P Problem with Outlook 2013 Attachments Using Outlook 4
E Outlook 2007: Problem with "Move to Folder >" button on toolbar Using Outlook 6
D No problem ... at this minute, just thanking you for your help. Using Outlook 1
T Random problem in VBA macro Outlook VBA and Custom Forms 6
R Outlook won't run except in Safe mode problem Using Outlook 5
J outlook 2010 delegation problem Using Outlook 3

Similar threads

Top