Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now