Solved

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

Posted on 2004-08-27
2
182 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:Richard Kreidl
[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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…
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…
Suggested Courses
Course of the Month4 days, 13 hours left to enroll

636 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