Question Outlook subject criteria

sandriekus

Member
Joined
Apr 20, 2009
Messages
9
Programming Experience
Beginner
Hello,

i want to delete automaticly some appointments in Outlook. this is the code that i have:

VB.NET:
Dim objOutlook As New Outlook.Application
            Dim objNS As Outlook.NameSpace
            Dim Appt As Object
            Dim objInboxItems As Outlook.Items
            Dim Criteria As String

            objNS = objOutlook.GetNamespace("MAPI")
            objInboxItems = objNS.GetDefaultFolder(9).Items
            Criteria = "[subject]=" & textbox1.Text
            Appt = objInboxItems.Find(Criteria)

            Do While Not (Appt Is Nothing)
                Appt.delete()
                Appt = objInboxItems.FindNext
            Loop

            Appt = Nothing
            objInboxItems = Nothing
            objNS = Nothing
            objOutlook = Nothing

now it work good but the program only deletes de appointments where the subject is exactly textbox1.text. for example:

textbox1 = hello
appointment subject = hello i'm 21 years old

then the appointment will not remove!

so what i need is something like this (but it doesnt work):

Criteria = "[subject.indexof(textbox1.text)]<> -1"

can someone help me please
 
It helps to read help, here's from help for Find Method [Outlook 2007 Developer Reference] :
help said:
you cannot use Find or Restrict to search for items that have a particular word in the Subject field. Instead, you can use the AdvancedSearch method, or you can loop through all of the items in the folder and ... perform a search within a field
To delete from a collection you are looping you must loop by index from last to first, for example
VB.NET:
Dim App As New Outlook.Application
Dim NS As Outlook.NameSpace = App.GetNamespace("MAPI")

Dim appointments As Outlook.MAPIFolder = NS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)

For i As Integer = appointments.Items.Count To 1 Step -1
    Dim ai As Outlook.AppointmentItem = CType(appointments.Items.Item(i), Outlook.AppointmentItem)
    If ai.Subject.Contains("word") Then
        ai.Delete()
    End If
Next
 
i'll do it in this way:

VB.NET:
Do While Not (Appt Is Nothing)
                If Appt.subject.indexof(someone.Text) = 0 And Appt.location.indexof(someroom.text) = 0 Then
                    Appt.delete()
                ElseIf Appt.subject.indexof(someone.Text) = 0 And Appt.location.indexof(someroom.text) = 0 Then
                    Appt.delete()
                ElseIf Appt.subject.indexof(someone.Text) = 0 And Appt.location.indexof(someroom.text) = 0 Then
                    Appt.delete()
                ElseIf Appt.subject.indexof(someone.Text) = 0 And Appt.location.indexof(someroom.text) = 0 Then
                    Appt.delete()
                ElseIf Appt.subject.indexof(someone.Text) = 0 And Appt.location.indexof(someroom.text) = 0 Then
                    Appt.delete()
                End If

                Appt = objInboxItems.FindNext
            Loop
 
Back
Top