Outlook

Dynamically add button to E-mail toolbar

Ease of Use

Intermediate

Version tested with

2000, 2003 

Submitted by:

MOS MASTER

Description:

On opening a window to send a new E-mail a button is created on the standard toolbar of the email window. This button is used to execute any VBA code (e.g. to write a standard introduction) 

Discussion:

Say you want to have some standard text inserted in a newly created E-mail. Then you could use a custom button to provide a solution. You can add whatever VBA code to the button so the scale of what you can do with the button is endless! You would need an Inspector Wrapper to catch each window being opened and check if that window is a new mail message. If it is, create the button. To have such a Wrapper you need a classmodule to Trap Inspector Events and trap Mailitem events. The complete Class "clsInspector" is attached so you can Import it! 

Code:

instructions for use

			

'Code in ThisOutlookSession Option Explicit Dim TrapInspector As clsInspector Private Sub Application_Quit() 'Event gets triggered when you quit Outlook 'Clean up Set TrapInspector = Nothing End Sub Private Sub Application_Startup() 'Event gets triggered when you start Outlook 'Initialize class "clsInspector" Set TrapInspector = New clsInspector End Sub ' End of this OutlookSession 'Code in Classmodule: clsInspector Option Explicit Dim WithEvents oAppInspectors As Outlook.Inspectors Dim WithEvents oMailInspector As Outlook.Inspector Dim WithEvents oOpenMail As Outlook.MailItem Dim WithEvents oMailButton As Office.CommandBarButton 'Class Initialize Private Sub Class_Initialize() 'oAppInspectors Returns a handle to the Inspectors collection Set oAppInspectors = Application.Inspectors End Sub Private Sub oAppInspectors_NewInspector(ByVal Inspector As Inspector) 'Event gets triggered every time a Window or Item is opened in Outlook Interface 'Like: E-mail, Contacts, Tasks If Inspector.CurrentItem.Class <> olMail Then 'Only deal with Email Items...else exit Exit Sub End If 'Set a reference to the e-mail to trap the Open event Set oOpenMail = Inspector.CurrentItem Set oMailInspector = Inspector End Sub Private Sub oOpenMail_Open(Cancel As Boolean) 'Event gets triggered if oOpenMail is opened! Dim oMailBar As Office.CommandBar 'Set a reference to commandbar Standard and add the commandbutton Set oMailBar = oMailInspector.CommandBars("Standard") 'Clean up left over buttons if any Call DeleteButtons(oMailBar) Set oMailButton = oMailBar.Controls.Add(Type:=msoControlButton) oMailBar.Visible = True 'Set properties of commandbutton With oMailButton .Caption = "Say Hi" .FaceId = 1000 .Style = msoButtonIconAndCaption End With 'Clean up Set oMailBar = Nothing End Sub Private Sub oOpenMail_Close(Cancel As Boolean) 'Event gets triggered if oOpenMail is Closed! Dim oMailBar As Office.CommandBar 'Set a reference to commandbar Standard Set oMailBar = oMailInspector.CommandBars("Standard") Call DeleteButtons(oMailBar) 'Clean up Set oMailBar = Nothing Set oOpenMail = Nothing End Sub Private Sub oMailButton_Click(ByVal Ctrl As Office.CommandBarButton, _ CancelDefault As Boolean) 'Event gets triggered if oMailButton is Clicked! oOpenMail.Body = "Hi There" End Sub Private Sub DeleteButtons(oBar As Office.CommandBar) 'Delete all buttons Dim oCtl As Office.CommandBarControl On Error Resume Next 'Loop through all controls to clear all "Say Hi" buttons For Each oCtl In oBar.Controls If oCtl.Caption = "Say Hi" Then oCtl.Delete End If Next 'Clean up Set oCtl = Nothing End Sub 'Class Terminate Private Sub Class_Terminate() 'Clean up Globals Set oAppInspectors = Nothing Set oOpenMail = Nothing Set oMailButton = Nothing End Sub 'End of Classmodule: clsInspector

How to use:

  1. Open Outlook
  2. Open Menu/Tools/Macro/Security
  3. Set the Security level to "Medium"
  4. Open the Visual Basics Editor (ALT+F11)
  5. Press CTRL+R (The project Explorer will appear)
  6. Double click on "ThisOutlookSession" (The code window appears (Or F7))
  7. Paste the code to initialize the class module we are going to use (In code pane above where it says: "Code in ThisOutlookSession"
  8. Go back to the project Explorer and rightclick and Choose "Import"
  9. Browse to the Classmodule: "clsInspector.cls" (Attachment)
  10. Now go to the Error menu and choose "Project1 Compile"
  11. Press the Save button
  12. Close the Editor
  13. Close Outlook, Choose yes when you're asked to save the OTM-File
 

Test the code:

  1. Start Outlook
  2. Open a new E-mail
  3. Click the button and some text wil be inserted in the body of the E-mail.
 

Sample File:

clsInspector.zip 1.16KB 

Approved by mdmackillop


This entry has been viewed 259 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2014 VBA Express