|
|
|
|
|
|
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
|
Option Explicit
Dim TrapInspector As clsInspector
Private Sub Application_Quit()
Set TrapInspector = Nothing
End Sub
Private Sub Application_Startup()
Set TrapInspector = New clsInspector
End Sub
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
Private Sub Class_Initialize()
Set oAppInspectors = Application.Inspectors
End Sub
Private Sub oAppInspectors_NewInspector(ByVal Inspector As Inspector)
If Inspector.CurrentItem.Class <> olMail Then
Exit Sub
End If
Set oOpenMail = Inspector.CurrentItem
Set oMailInspector = Inspector
End Sub
Private Sub oOpenMail_Open(Cancel As Boolean)
Dim oMailBar As Office.CommandBar
Set oMailBar = oMailInspector.CommandBars("Standard")
Call DeleteButtons(oMailBar)
Set oMailButton = oMailBar.Controls.Add(Type:=msoControlButton)
oMailBar.Visible = True
With oMailButton
.Caption = "Say Hi"
.FaceId = 1000
.Style = msoButtonIconAndCaption
End With
Set oMailBar = Nothing
End Sub
Private Sub oOpenMail_Close(Cancel As Boolean)
Dim oMailBar As Office.CommandBar
Set oMailBar = oMailInspector.CommandBars("Standard")
Call DeleteButtons(oMailBar)
Set oMailBar = Nothing
Set oOpenMail = Nothing
End Sub
Private Sub oMailButton_Click(ByVal Ctrl As Office.CommandBarButton, _
CancelDefault As Boolean)
oOpenMail.Body = "Hi There"
End Sub
Private Sub DeleteButtons(oBar As Office.CommandBar)
Dim oCtl As Office.CommandBarControl
On Error Resume Next
For Each oCtl In oBar.Controls
If oCtl.Caption = "Say Hi" Then
oCtl.Delete
End If
Next
Set oCtl = Nothing
End Sub
Private Sub Class_Terminate()
Set oAppInspectors = Nothing
Set oOpenMail = Nothing
Set oMailButton = Nothing
End Sub
|
How to use:
|
- Open Outlook
- Open Menu/Tools/Macro/Security
- Set the Security level to "Medium"
- Open the Visual Basics Editor (ALT+F11)
- Press CTRL+R (The project Explorer will appear)
- Double click on "ThisOutlookSession" (The code window appears (Or F7))
- Paste the code to initialize the class module we are going to use (In code pane above where it says: "Code in ThisOutlookSession"
- Go back to the project Explorer and rightclick and Choose "Import"
- Browse to the Classmodule: "clsInspector.cls" (Attachment)
- Now go to the Error menu and choose "Project1 Compile"
- Press the Save button
- Close the Editor
- Close Outlook, Choose yes when you're asked to save the OTM-File
|
Test the code:
|
- Start Outlook
- Open a new E-mail
- 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 261 times.
|
|