Solved

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

Posted on 2004-08-27
2
179 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
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

820 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