Link to home
Start Free TrialLog in
Avatar of Mark
Mark

asked on

Outlook VBA how to scan email headers

I'm trying to made an Outlook VB module to scan through an email's header for a particular header. For example, given the headers:
From: "Dan Simon" <dsimon@xxx.com>
To: "'Matt Gregory'" <MGREGORY@yyy.com>
Cc: "'Lucy'" <bthrock4@zzzl.com>
References: <004a01cfaf57$5a4ec140$0eec43c0$@apex-title.com>    <CA+59raJ69dK7C5uyyvztMs=7iDNw-wkPoSC0gbcRyecAY
p8bKg@mail.gmail.com>   <CABLLg6=dfpMttb5HbvxZ+GbPzLRx37cxKpcSOichhFCCVDT7Sw@mail.gmail.com>    <f7577a674c7b47
8eb538fa0ad75cdce9@CSC-EX13-01.cscloud.priv>    <007b01cfb111$94314110$bc93c330$@apex-title.com>        <72de16
bde0304b42b16a7d1d21478985@CSC-EX13-01.cscloud.priv>    <FC177A271ECF104D8FECF73F8795FE8B32D3FAC060@MAIL.hprs.l
ocal>,<CAG7ouXd_DPAnTMoxqPOwvjjQQ7529KH1s_4jNmOi36Lm4Y4k8A@mail.gmail.com> <28144A4F-8CE1-4AC8-BF61-A919F8D69CE
3@ohioequities.com>,<019b01cfb5a2$6dbb3af0$4931b0d0$@apex-title.com> <658FD438-1257-4728-B603-2D64F444E952@ohio
equities.com>
In-Reply-To: <658FD438-1257-4728-B603-2D64F444E952@ohioequities.com>
Subject: RE: 6500 Busch Boulevard, Columbus, OH 43229 - Updated Commitment and Draft HUD
Date: Tue, 12 Aug 2014 09:24:42 -0500
Keywords: Red Category
Message-ID: <003c01cfb639$2933de90$7b9b9bb0$@apex-title.com>
MIME-Version: 1.0
Content-Type: multipart/mixed;
        boundary="----=_NextPart_000_4854_01D04966.D56544E0"
X-Mailer: Microsoft Office Outlook 12.0
X-Virus-Status: Clean
X-Virus-Scanned: clamav-milter 0.98.1 at webserver
X-Spam-Status: No, score=0.0 required=3.0 tests=HTML_MESSAGE,   RCVD_IN_DNSWL_NONE autolearn=disabled version=3
.3.2
X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on webserver.ohprs.org
Thread-Index: AQEhggqaBNb5ghRXTh9S+faTbwOVOgJhrekRAgxwxSwCS9PzwwIjJokmA1Ot+fkCCGD68QIZo2iuAoEZoiEBvag7SgIuePaXn
HNtfuA=
Content-Language: en-us
x-ms-exchange-organization-authas: Internal
x-ms-exchange-organization-authsource: MAIL.hprs.local
X-OlkEid: B1044D21BD509E663B48F84C9AA4C079FDE11715
x-ms-exchange-organization-authmechanism: 10
x-vipre-scanned: 4AADA05C007F564AADA1A9

This is a multi-part message in MIME format.

------=_NextPart_000_4854_01D04966.D56544E0
Content-Type: multipart/alternative;
        boundary="----=_NextPart_001_4855_01D04966.D56544E0"


------=_NextPart_001_4855_01D04966.D56544E0
Content-Type: text/plain;
        charset="Windows-1252"
Content-Transfer-Encoding: 8bit

Open in new window

I'd like to scan through looking for the header "Keywords: Red Category" (line 14). I've looked at an example at http://www.slipstick.com/developer/read-mapi-properties-exposed-outlooks-object-model/, but I don't know if this "Schema" method is going to work for me as the "Complete List" of Email Messages Properties doesn't appear to have one for "Keywords", nor does it appear to have a bunch of others such as spam headers.

I've got the following code going, but am kind of stuck. Help?
Public Sub scanFolder()
Dim src As Folder
Dim oItem As Object
Dim propertyAccessor As Outlook.propertyAccessor
Set src = Application.ActiveExplorer.CurrentFolder
Dim cnt As Integer
Dim strHeader As String
cnt = 0

For Each oItem In src.Items
    If TypeOf oItem Is Outlook.MailItem Then
        Set propertyAccessor = oItem.propertyAccessor
        Debug.Print propertyAccessor.GetProperty("schemaName???") ' what goes here?
        
        cnt = cnt + 1
    End If
Next
Debug.Print cnt
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kimputer
Kimputer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mark
Mark

ASKER

Awesome! This worked! All the headers get returned by that one GetProperty() function which is fine. Now I'll just have to figure out how to examine them line by line to extract the "Keywords" header.

To answer your question, yes this *is* an Outlook header and probably Outlook only. It is put into the email if the user adds color categories to his/her message. Try it! Set a color category on one of your messages in Outlook then examine the headers (open message > File > Info >properties). You'll see "Keywords: Red Category" (or whatever color you set it to).
Avatar of Mark

ASKER

Any idea what's wrong with this code:
Public Sub scanFolder()
Dim src As Folder
Dim oItem As Object
Dim propertyAccessor As Outlook.propertyAccessor
Set src = Application.ActiveExplorer.CurrentFolder
Dim cnt As Integer
Dim strHeader As String
cnt = 0

For Each oItem In src.Items
    If TypeOf oItem Is Outlook.MailItem Then
Debug.Print ">>>>>>>>>>>>>>>>>>>NEW MESSAGE<<<<<<<<<<<<<<"
        Set propertyAccessor = oItem.propertyAccessor
        header = propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
        Dim headerLines() As String
        headerLines() = header.Split(Enviroment.NewLine)
        Dim thisHeader As Variant
        
        For Each thisHeader In headerLines
            Debug.Print thisHeader
        Next
        cnt = cnt + 1
Exit For
    End If
Next
Debug.Print cnt
End Sub

Open in new window

When I run I get "Run-time error '424'; Object required" at line 16. Not really descriptive enough for me to figure out the problem. I basically monkey-typed this from a web example.
Try this:

headerLines() = Split(header, Enviroment.NewLine)

~bp
Avatar of Mark

ASKER

Nope :( same error.

I tried this again later as: Split(header, "A") and it did split my string on the letter "A", so I guess my issues is Enviroment.NewLine. Can I fix that?

Another test shows that I can use chr(13), but I guess I'd like to be more generic if possible.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mark

ASKER

The mail is physically stored in the IMAP respository with just <LF>, but vbCrLf works. Thanks. Saved my life again.

OK, here is as good a place as any to ask this ... even though I've programmed VB/Access for years, I find VB the most idiosyncratic language of the dozen or so languages I routinely use. I run into this kind of thing all the time. Why do I have to use Split(header,delimiter) and not header.Split(delimiter) as pretty much every web example I run into has? Why do I have to use vbCrLf when web examples show Enviroment.NewLine?

And why can't I move my immediate window to the background? (not really related to above, but I'm *there*)
Avatar of Mark

ASKER

No thoughts on this variant syntax question? Not worth posting a new question just on that.
Well, VBA is Classic Basic, and still based on string functions instead of methods of string objects. Strings are a BASIC simple type like integers, and no object you can apply a method on. More recent languages usually extend the object model to those simple types; VB.Net for example.

I had exactly the same issue today when switching from PS to VBA. Used to use string methods, but have to use string functions instead ... Needed some time to "switch mode" in my head.
Avatar of Mark

ASKER

Gotcha! VB/Access was also "classic", which is what I'm used to. I guess I'll know when I see web examples using methods that they're talking about VB.net and I'll move on to a different example.

Although, the confusing bit is that VBA does use objects and methods, e.g. Application.ActiveExplorer.CurrentFolder.Items. Too bad they couldn't "objectify" and "methodize" strings so the language is more consistent and go ahead and support functions for legacy purposes; kind of like C++ being able to support ANSI C syntax.

Oh Well -- Java is my everyday language, so I'll put up with what I need to with VBA.

Thanks.