Solved

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

Posted on 2004-08-27
2
178 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

773 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