How to run a macro with parameters from a toolbar button in Outlook 2003
November 30, 2009 • JP • No Comments • Rate This Article
• Links 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
to
and change
to
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:
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.
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:

Click each button and you'll see meetings of 30 and 60 minutes, respectively. Enjoy!
↑ Scroll to topPrevious Post: Are there too many Office forums?
Next Post: ExcelUser.com 50% Off Sale, Three Days Only!



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].