How to run a macro with parameters from a toolbar button in Outlook 2003

November 30, 2009JPNo CommentsRate This ArticlenewLinks to this article


    Previously we showed how to add a toolbar button to Outlook programmatically, and use it to call a procedure that creates meetings from emails (a la the Meeting Reply button in Outlook 2010).

    But our procedure has 60 minutes hardcoded as the duration of each appointment. Sure, you can always change it manually, or add another InputBox to the procedure, but what if you want to be able to pass the duration as a parameter?

    The problem is that once you turn the procedure into a function, with duration as the parameter, it disappears from the list of available macros. Furthermore, even if you could assign it to a toolbar button, how would you pass the parameter to the function?

    Here's how to do it. First, we'll turn the MeetingReply procedure into a function as follows.

    Change

Sub MeetingReply()

    to

Function MeetingReply(mtgDuration As Long)

    and change

    .Duration = 60

    to

    .Duration = mtgDuration

    Now when you try and click the Meeting Reply button, nothing happens! So we'll need to create new procedures, ones that don't take parameters, and adjust the pointer accordingly. First you'll want to remove the Meeting Reply button by holding down the Alt key and dragging it off the toolbar.

    Add two new procedures to the same module as the function:

Sub MeetingReply60()
  Call MeetingReply(60)
End Sub

Sub MeetingReply30()
  Call MeetingReply(30)
End Sub

    These procedures (which have no parameters) create meeting replies with durations of 30 and 60 minutes (which I assume are the usual amounts of time most meetings last). These are the procedures that will be called by toolbar buttons.

    In short, you can use functions with parameters, as long as you call them from parameter-less procedures, which themselves would be called from toolbar buttons.

    Now we'll run the maketoolbarbutton again, slightly modified to call the two new procedures we just created.

Sub maketoolbarbutton()
  Call AddToolbarButton("Meeting Reply (30 mins)", "Meeting Reply (30 mins)", "MeetingReply30", , , msoButtonIconAndCaption)
  Call AddToolbarButton("Meeting Reply (60 mins)", "Meeting Reply (60 mins)", "MeetingReply60", , , msoButtonIconAndCaption)
End Sub

    And here is the result:

Meeting Reply Function

    Click each button and you'll see meetings of 30 and 60 minutes, respectively. Enjoy!

About JP
I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space if you want to learn more about VBA. Keep Reading »

↑ Scroll to top
Previous Post:

Next Post:

Speak Your Mind

Tell us what you're thinking...

Certain comments (including first-time comments) are subject to moderation and will not appear immediately. Please view the Comment Policy for more information. To post VBA code in your comment, use tags like this: [cc lang='vb']Code goes here[/cc].




Site last updated September 2, 2010 @ 7:03 pm