?
Solved

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

Posted on 2015-02-24
8
Medium Priority
?
121 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 56

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 56

Accepted Solution

by:
Bill Prew earned 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 56

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 56

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses

800 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