Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

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

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 58

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
Technology Partners: 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 58

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 58

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
This article describes how to import Lotus Notes Contacts into Outlook 2016, 2013, 2010 and 2007 etc. with a few manual steps. You can easily export and migrate Lotus Notes contacts into Microsoft Outlook without having to use any third party tools.
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

650 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