Solved

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

Posted on 2015-02-24
8
113 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
  • 4
  • 4
8 Comments
 
LVL 51

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 51

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
 
LVL 51

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 51

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

Highfive Gives IT Their Time Back

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

Suggested Solutions

Title # Comments Views Activity
Spell check software 2 32
TT Status Chang 3 31
Changing absolute cell references 3 21
TT Add/ Update 2 12
Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
Check out this infographic on what you need to make a good email signature that will work perfectly for your organization.
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 …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

20 Experts available now in Live!

Get 1:1 Help Now