Solved

Search in Outlook 'Subject' only finds a couple of emails?

Posted on 2004-08-27
2
181 Views
Last Modified: 2010-05-02
I have the following module that I want to search the subject field for 'Incident Alert' and save the .msg to a specified location. Let's say I have three emails in my 'inbox' with the subject of: Incident Alert. The module is only finding two of them and saving one of them to my 'C' drive. There is still one left in my inbox for some reason and only one is being saved. Any ideas??

Private Sub Main()
Dim oOutlook As Outlook.Application
Dim oNs As Outlook.NameSpace
Dim oFldr As Outlook.MAPIFolder
Dim oMessage As Outlook.MailItem
Set oOutlook = New Outlook.Application
Set oNs = oOutlook.GetNamespace("MAPI")
Set oFldr = oNs.GetDefaultFolder(olFolderInbox)

For Each oMessage In oFldr.Items
    If InStr(1, oMessage.Subject, "Incident Alert") > 0 Then
      oMessage.SaveAs "c:\Incident Alert\" & "Incident Alert.msg", olMSG
    oMessage.Delete
End If

    Next oMessage
 
    Set oMessage = Nothing
    Set oFldr = Nothing
    Set oNs = Nothing
    Set oOutlook = Nothing
   
End Sub
0
Comment
Question by:rkckjk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 125 total points
ID: 11916365
It's tough to say why without being able to see the subject lines as they actually appear.  My first thought is that the one item has an extra space, is misspelled, has an additional letter in upper case, or something like that.  Clearly the code must be working or it wouldn't find the two items.  So, it seems reasonable that it's something about the third item.  You might try forcing the test to either all upper case or all lower case.  Something like:

If InStr(1, UCase(oMessage.Subject), "INCIDENT ALERT") > 0 Then
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11919945
oMessage.SaveAs "c:\Incident Alert\" & "Incident Alert.msg", olMSG

I am not sure but, I think that all found messages wil be saved with same filename, so some data can be overwritten. Try to add counter of found messages and add counter to the file name
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question