Solved

Messing with Outlook VBA - how to loop though messages in current folder

Posted on 2015-02-24
8
119 Views
Last Modified: 2016-02-10
As a prelude to greater things, I'm trying to loop through the messages in the current folder and display the sender email address. I've cobbled the following together from various other samples. My code does nothing at all when run. Can someone tell me what's wrong?
Public Sub scanFolder()
Dim src As Folder
Dim item As Object
Set src = Application.ActiveExplorer.CurrentFolder

For Each item In src.Items
    If TypeOf item Is Outlook.MailItem Then
       Dim oMail As Outlook.MailItem: Set oMail = item
       Debug.Print oMail.SenderEmailAddress
    End If
Next
End Sub

Open in new window

0
Comment
Question by:jmarkfoley
[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
  • 4
  • 4
8 Comments
 
LVL 55

Expert Comment

by:Bill Prew
ID: 40629787
That ran fine for me.  Are you sure you have a folder selected?

Try adding a Debug after your set src and make sure it has the folder name.

Debug.Print src.Name

~bp
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 40630957
Hmmm, absolutely nothing happens at all. Not even your suggested Debug.print src.name. I click F5, select this scanFolders module, click 'run'. That's it, nothing. What should the output look like? I've not done Debug.print before.

even with:

Public Sub scanFolder()
Debug.Print "joe"
End sub

Nothing happens.
0
 
LVL 55

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40631162
Sounds like it may be working, you just can't see the Immediate Window.  In the VBA editor mode, use the View menu and then select Immediate Window.  You should see a small panel show up under where the VBA code edits, and that's where the messages will display.

~bp
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 55

Expert Comment

by:Bill Prew
ID: 40631195
I know it's about Access, but VBA is common across the MS Office products, so it's still relevant.  Here's a link to some info that might help you as you need to work through code that you create and determine what it's doing (or isn't).

http://www.techonthenet.com/access/tutorials/vbadebug2007/debug01.php

~bp
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 40633820
Ah ha! Yes, all the output is in the Immediate windows!

VBA is common across the MS Office products
Believe it or not, I spent years doing VB programming in Access 2003 and SQL Server 2005. I'm familiar with the "syntax" per se, but Outlook's "F11" looks nothing like the Access' developer interface. And of course, it's really all about the method libraries. I can do queries and loop through datasets, but am clueless about Outlook objects.

I'll play a bit with this and get back
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 40634192
OK, things are working better now. One last question then I'll close this one and open lots more.

Instead of simply showing the SenderEmailAddress, I'd like to examine the mail headers. Specfically, if I pseudo code:

dim header as something
For Each item In src.Items
    If TypeOf item Is Outlook.MailItem Then
       Dim oMail As Outlook.MailItem: Set oMail = item
        For each header in oMail.Headers
             If Instr(header,"Keyword:") > 0 Then
                 Debug.Print header
             End If
        Next
    End If
Next
0
 
LVL 55

Expert Comment

by:Bill Prew
ID: 40634538
I don't believe the Outlook Object Model exposes the header information.  Take a look at the link below as it demonstrates how to access some of these hidden attributes.  I haven't done this myself, but happy to help work something up or troubleshoot an attempt if you want.

http://www.slipstick.com/developer/read-mapi-properties-exposed-outlooks-object-model/

~bp
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 40634758
Whoa!!! Leave it to Microsoft to add exponential layers of complexity! I thought it would be something simple like a object containing a text list of header lines, but Nooooo!

OK, you've answered my original question quite nicely. This add-on is far more complicated. I'll close this one. If you want to continue on this header scanning issue, I've posted a new question:

http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/Q_28625697.html
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
This article will help to fix the below errors for MS Exchange Server 2013 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
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…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

691 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