[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

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.

  • 3
1 Solution
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 "

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now