none
AdvancedSearch Returning Search Results Intermittently in VB.NET app RRS feed

  • Question

  • I've run into an odd problem, not sure what I'm doing wrong.  

    I have a VB.NET 2008 application that is using the Outlook 2007 COM to search for a specific email, then delete the attachment.  The part that is not working consistently is the AdvancedSearch.  It does however work intermittently.  

    Here is how I'm doing this.  

    1.  I create an excel file, save it, then take the file name string (example 2936_20120404_024143_Customer.xls)  and store it in the Outlook email field billinginformation so I have something to search for.  

    2.  I then send the email with attachment, and immediately following is the a call to the  routine to search for the file name string in the billinginformation field.  Once found, I loop through the SearchResults and delete any attachment, and save the email.  

    The delete part works fine.  What I'm finding is the AdvancedSearch is only occasionally successful.  To test this, I hard code the file name of an email with attachment that is sitting in the Sent Folder in a variable, put a button on the form to call the AdvancedSearch routine and put a break point just after the If SearchResults.Count > 0.  I can press the button 10 or maybe 15 times or so to search for the exact file name and get nothing.  Then for no apparent reason on the 11th or 16th time, it finds it!  I'm clearly doing something wrong.  

    One other thing that seems to consistently work.  If I step through the code, and pause between each line prior to SearchResults.Count, it will be pretty consistent in finding my value.  

    Have I missed something here.  

    I'm using Outlook 2007.  The email account I'm searching is a Microsoft Live email account that is in my local Outlook, that may have some impact, not sure.  

    Below is the code.  

    Thanks for any help on this.  I'm open to a better way to search for the sent email than populating billinginformation.  The goal is to find the email that was just sent, or be able to identify it at some later point so attachments can be deleted and not stored.  

    Tim 

    Imports Microsoft.Office.Core
    Imports Microsoft.Office.Interop
    Imports Microsoft.Office.Interop.Outlook
    Imports System
    Imports System.Runtime.InteropServices

    Public Shared Sub DeleteAttachmentFromOutlookSent(ByVal oApp As Outlook.Application, ByVal AttachmentFileName As String)

            Dim oSentFolder As Outlook.MAPIFolder
            Dim oSrch As Outlook.Search
            Dim oSrchRslts As Results = Nothing
            Dim sScope As String
            Dim sFilter As String
            Dim oAttachments As Outlook.Attachments
            Dim oAttachmentDetails As Outlook.Attachment
            Dim oMailItem As Outlook.MailItem


            oSentFolder = oApp.GetNamespace("Mapi").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
            sScope = "'" & oSentFolder.FolderPath.Replace("'", "''").ToString & "'"
            sFilter = "urn:schemas:contacts:billinginformation LIKE '%" & AttachmentFileName & "%'"


            oSrch = oApp.AdvancedSearch(sScope, sFilter, False, "SearchForAttachment")
            If oSrch.Tag = "SearchForAttachment" Then
                oSrchRslts = oSrch.Results
            End If


            If oSrchRslts.Count > 0 Then
                For i As Integer = 1 To oSrchRslts.Count


                    oMailItem = oSrchRslts(i)
                    oAttachments = oMailItem.Attachments
                    If oAttachments.Count > 0 Then
                        For j As Integer = oAttachments.Count To 1 Step -1
                            oAttachmentDetails = oAttachments(j)
                            oAttachments.Remove(oAttachmentDetails.Index)
                            Marshal.ReleaseComObject(oAttachmentDetails)
                        Next
                        oMailItem.Save()
                        'Exit For
                    End If
                    Marshal.ReleaseComObject(oMailItem)
                    Marshal.ReleaseComObject(oAttachments)


                    System.Threading.Thread.Sleep(200)
                Next


            End If
            Marshal.ReleaseComObject(oSrchRslts)
            Marshal.ReleaseComObject(oSrch)
            Marshal.ReleaseComObject(oSentFolder)



            oSrch = Nothing
            oSrchRslts = Nothing
            oMailItem = Nothing
            oAttachments = Nothing
            oAttachmentDetails = Nothing
            oSentFolder = Nothing


        End Sub



    Tim Conway

    Thursday, April 5, 2012 1:46 PM

Answers

  • If a delay solves the problem that implies either a cached value that takes time for it to be cleared, or that it takes some time for the Live Mail data to be updated on the server and the refreshed locally.
     
    Can you set up a delay if that seems to solve the problem?
     
    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    Reminder Manager, Extended Reminders, Attachment Options
    http://www.slovaktech.com/products.htm
     
     
    "tconway" <=?utf-8?B?dGNvbndheQ==?=> wrote in message news:9e99dbee-3032-44c0-9dec-12b92e33e8be...

    I've run into an odd problem, not sure what I'm doing wrong.  

    I have a VB.NET 2008 application that is using the Outlook 2007 COM to search for a specific email, then delete the attachment.  The part that is not working consistently is the AdvancedSearch.  It does however work intermittently.  

    Here is how I'm doing this.  

    1.  I create an excel file, save it, then take the file name string (example 2936_20120404_024143_Customer.xls)  and store it in the Outlook email field billinginformation so I have something to search for.  

    2.  I then send the email with attachment, and immediately following is the a call to the  routine to search for the file name string in the billinginformation field.  Once found, I loop through the SearchResults and delete any attachment, and save the email.  

    The delete part works fine.  What I'm finding is the AdvancedSearch is only occasionally successful.  To test this, I hard code the file name of an email with attachment that is sitting in the Sent Folder in a variable, put a button on the form to call the AdvancedSearch routine and put a break point just after the If SearchResults.Count > 0.  I can press the button 10 or maybe 15 times or so to search for the exact file name and get nothing.  Then for no apparent reason on the 11th or 16th time, it finds it!  I'm clearly doing something wrong.  

    One other thing that seems to consistently work.  If I step through the code, and pause between each line prior to SearchResults.Count, it will be pretty consistent in finding my value.  

    Have I missed something here.  

    I'm using Outlook 2007.  The email account I'm searching is a Microsoft Live email account that is in my local Outlook, that may have some impact, not sure.  

    Below is the code.  

    Thanks for any help on this.  I'm open to a better way to search for the sent email than populating billinginformation.  The goal is to find the email that was just sent, or be able to identify it at some later point so attachments can be deleted and not stored.  

    Tim 

    Imports Microsoft.Office.Core
    Imports Microsoft.Office.Interop
    Imports Microsoft.Office.Interop.Outlook
    Imports System
    Imports System.Runtime.InteropServices

    Public Shared Sub DeleteAttachmentFromOutlookSent(ByVal oApp As Outlook.Application, ByVal AttachmentFileName As String)

            Dim oSentFolder As Outlook.MAPIFolder
            Dim oSrch As Outlook.Search
            Dim oSrchRslts As Results = Nothing
            Dim sScope As String
            Dim sFilter As String
            Dim oAttachments As Outlook.Attachments
            Dim oAttachmentDetails As Outlook.Attachment
            Dim oMailItem As Outlook.MailItem


            oSentFolder = oApp.GetNamespace("Mapi").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
            sScope = "'" & oSentFolder.FolderPath.Replace("'", "''").ToString & "'"
            sFilter = "urn:schemas:contacts:billinginformation LIKE '%" & AttachmentFileName & "%'"


            oSrch = oApp.AdvancedSearch(sScope, sFilter, False, "SearchForAttachment")
            If oSrch.Tag = "SearchForAttachment" Then
                oSrchRslts = oSrch.Results
            End If


            If oSrchRslts.Count > 0 Then
                For i As Integer = 1 To oSrchRslts.Count


                    oMailItem = oSrchRslts(i)
                    oAttachments = oMailItem.Attachments
                    If oAttachments.Count > 0 Then
                        For j As Integer = oAttachments.Count To 1 Step -1
                            oAttachmentDetails = oAttachments(j)
                            oAttachments.Remove(oAttachmentDetails.Index)
                            Marshal.ReleaseComObject(oAttachmentDetails)
                        Next
                        oMailItem.Save()
                        'Exit For
                    End If
                    Marshal.ReleaseComObject(oMailItem)
                    Marshal.ReleaseComObject(oAttachments)


                    System.Threading.Thread.Sleep(200)
                Next


            End If
            Marshal.ReleaseComObject(oSrchRslts)
            Marshal.ReleaseComObject(oSrch)
            Marshal.ReleaseComObject(oSentFolder)



            oSrch = Nothing
            oSrchRslts = Nothing
            oMailItem = Nothing
            oAttachments = Nothing
            oAttachmentDetails = Nothing
            oSentFolder = Nothing


        End Sub



    Tim Conway


    Ken Slovak MVP - Outlook
    • Marked as answer by tconway Thursday, April 5, 2012 4:51 PM
    Thursday, April 5, 2012 4:34 PM
    Moderator

All replies

  • If a delay solves the problem that implies either a cached value that takes time for it to be cleared, or that it takes some time for the Live Mail data to be updated on the server and the refreshed locally.
     
    Can you set up a delay if that seems to solve the problem?
     
    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    Reminder Manager, Extended Reminders, Attachment Options
    http://www.slovaktech.com/products.htm
     
     
    "tconway" <=?utf-8?B?dGNvbndheQ==?=> wrote in message news:9e99dbee-3032-44c0-9dec-12b92e33e8be...

    I've run into an odd problem, not sure what I'm doing wrong.  

    I have a VB.NET 2008 application that is using the Outlook 2007 COM to search for a specific email, then delete the attachment.  The part that is not working consistently is the AdvancedSearch.  It does however work intermittently.  

    Here is how I'm doing this.  

    1.  I create an excel file, save it, then take the file name string (example 2936_20120404_024143_Customer.xls)  and store it in the Outlook email field billinginformation so I have something to search for.  

    2.  I then send the email with attachment, and immediately following is the a call to the  routine to search for the file name string in the billinginformation field.  Once found, I loop through the SearchResults and delete any attachment, and save the email.  

    The delete part works fine.  What I'm finding is the AdvancedSearch is only occasionally successful.  To test this, I hard code the file name of an email with attachment that is sitting in the Sent Folder in a variable, put a button on the form to call the AdvancedSearch routine and put a break point just after the If SearchResults.Count > 0.  I can press the button 10 or maybe 15 times or so to search for the exact file name and get nothing.  Then for no apparent reason on the 11th or 16th time, it finds it!  I'm clearly doing something wrong.  

    One other thing that seems to consistently work.  If I step through the code, and pause between each line prior to SearchResults.Count, it will be pretty consistent in finding my value.  

    Have I missed something here.  

    I'm using Outlook 2007.  The email account I'm searching is a Microsoft Live email account that is in my local Outlook, that may have some impact, not sure.  

    Below is the code.  

    Thanks for any help on this.  I'm open to a better way to search for the sent email than populating billinginformation.  The goal is to find the email that was just sent, or be able to identify it at some later point so attachments can be deleted and not stored.  

    Tim 

    Imports Microsoft.Office.Core
    Imports Microsoft.Office.Interop
    Imports Microsoft.Office.Interop.Outlook
    Imports System
    Imports System.Runtime.InteropServices

    Public Shared Sub DeleteAttachmentFromOutlookSent(ByVal oApp As Outlook.Application, ByVal AttachmentFileName As String)

            Dim oSentFolder As Outlook.MAPIFolder
            Dim oSrch As Outlook.Search
            Dim oSrchRslts As Results = Nothing
            Dim sScope As String
            Dim sFilter As String
            Dim oAttachments As Outlook.Attachments
            Dim oAttachmentDetails As Outlook.Attachment
            Dim oMailItem As Outlook.MailItem


            oSentFolder = oApp.GetNamespace("Mapi").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
            sScope = "'" & oSentFolder.FolderPath.Replace("'", "''").ToString & "'"
            sFilter = "urn:schemas:contacts:billinginformation LIKE '%" & AttachmentFileName & "%'"


            oSrch = oApp.AdvancedSearch(sScope, sFilter, False, "SearchForAttachment")
            If oSrch.Tag = "SearchForAttachment" Then
                oSrchRslts = oSrch.Results
            End If


            If oSrchRslts.Count > 0 Then
                For i As Integer = 1 To oSrchRslts.Count


                    oMailItem = oSrchRslts(i)
                    oAttachments = oMailItem.Attachments
                    If oAttachments.Count > 0 Then
                        For j As Integer = oAttachments.Count To 1 Step -1
                            oAttachmentDetails = oAttachments(j)
                            oAttachments.Remove(oAttachmentDetails.Index)
                            Marshal.ReleaseComObject(oAttachmentDetails)
                        Next
                        oMailItem.Save()
                        'Exit For
                    End If
                    Marshal.ReleaseComObject(oMailItem)
                    Marshal.ReleaseComObject(oAttachments)


                    System.Threading.Thread.Sleep(200)
                Next


            End If
            Marshal.ReleaseComObject(oSrchRslts)
            Marshal.ReleaseComObject(oSrch)
            Marshal.ReleaseComObject(oSentFolder)



            oSrch = Nothing
            oSrchRslts = Nothing
            oMailItem = Nothing
            oAttachments = Nothing
            oAttachmentDetails = Nothing
            oSentFolder = Nothing


        End Sub



    Tim Conway


    Ken Slovak MVP - Outlook
    • Marked as answer by tconway Thursday, April 5, 2012 4:51 PM
    Thursday, April 5, 2012 4:34 PM
    Moderator
  • Yes, I believe that is it.  Adding this...

    System.Threading.Thread.Sleep(1000)

    ...just prior to the

    If SearchResults.Count > 0 then ....

    seems to do it.  The email has to appear in the Sent box of my Outlook Personal Folders first, but once there, that seems to fix the problem 

    Thanks so much for your help.

    Tim


    Tim Conway


    • Edited by tconway Thursday, April 5, 2012 4:51 PM
    Thursday, April 5, 2012 4:50 PM