Macro Code Count

Posted on 2008-11-09
Last Modified: 2012-06-27
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.

Question by:Gazza83
    LVL 11

    Accepted 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

    LVL 11

    Expert Comment

    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
    LVL 11

    Expert Comment

    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

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Get an idea of what you should include in an email disclaimer with these Top 5 email disclaimer tips.
    Set OWA language and time zone in Exchange for individuals, all users or per database.
    Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now