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

LVL 1
jmarkfoleyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KimputerCommented:
You're probably missing this:

Debug.Print propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")

Open in new window


This will display your Internet Headers. I have not come across this "Keywords: Red Category" in my headers yet though, but if you're sure it's in your headers, it should work.
If it didn't work (but you see the other headers), then the question is, how did you get the header you copy & pasted in your original question (probably not through Outlook, message properties? If you got it from another person, after you sent it to them, you can already see the dillema you're in, right?)?
There's a big chance then you won't get it through this method.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmarkfoleyAuthor Commented:
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).
0
jmarkfoleyAuthor Commented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Bill PrewCommented:
Try this:

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

~bp
0
jmarkfoleyAuthor Commented:
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.
0
Bill PrewCommented:
How about:

headerLines() = Split(header, vbCrLf)

If that doesn't work try vbCR and vbLf also, not sure what the end of line terminator will be in the headers...

~bp
0
jmarkfoleyAuthor Commented:
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*)
0
jmarkfoleyAuthor Commented:
No thoughts on this variant syntax question? Not worth posting a new question just on that.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
jmarkfoleyAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.