[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

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

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 59

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
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
LVL 59

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 59

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
There can be many situations demanding the conversion of Outlook OST files to PST format and as such, there is no shortage of automated tools to perform this conversion. However, what makes Stellar OST to PST converter stand above the rest? Let us e…
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…
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.
Suggested Courses

834 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