Macro Code Count

Is there a way I can add a command button to outlook?

All of my emails are altered once processed in  the subject bar to read something like -

"hughesr - GE - s. 09/11/08 10:45 am e. 09/11/08 06:00 pm"

What I am hoping to do is count the occurrence of "GE" which are my initials... so anything in the subject bar that wold have "ge" or "GE"  amongst what is typed there.

So ideally I would highlight a range and then hit the command button and it would count the occurrence of "GE"

Please can you help, I have never added a macro to outlook so your help is much appreciated.

Who is Participating?
Antagony1960Connect With a Mentor Commented:
This is fairly straightforward: add the macro below, then right-click your toolbar and select 'Customize...' Select the 'Commands' tab and click on 'Macros' in the category list. The macro (Project1.Count_Instances) will appear in the 'Commands' list, drag it onto the toolbar and it will create the button. You can then right-click the button and change its name to something more succinct.

Sub Count_Instances()
Dim mi As MailItem, i As Integer
    For Each mi In Application.ActiveExplorer.Selection
        If InStr(1, mi.Subject, "GE") > 0 Then i = i + 1
    MsgBox "There are " & i & " emails with GE in the subject line"
End Sub

Open in new window

Ah, I didn't read your question fully... you want to find "ge" as well as "GE". That's fine, just change the Instr line to this:

        If InStr(1, UCase(mi.Subject), "GE") > 0 Then i = i + 1
And it's occurred to me that this may be unreliable, as it will also pick out "ge" from any words with those two letters in sequence. If you can guarantee there will always be a space before and after the "GE", as in your example above, you should change the search parameter in the Instr command to " GE "
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.

All Courses

From novice to tech pro — start learning today.