Button to put disclaimer on email.

G

Guest

I was able to use the following test example to make a button and have it
create textboxes when clicked. Any idea on how to have it make the button on
the main bar but on the EMAIL dialog box, instead of the main bar of outlook
itself?
Also how can I insert text into the email messagebox itself once I get the
button in the main bar of the email dialog box? I am using visual studio .NET
2003 with Visual Basic as the language. I am trying to make a COM object to
do this.

The example below basically creates a button and then pops open messageboxes
when clicked. But what I need is to have this button be placed on the email
dialog box, instead of the main outlook dialog. And then once clicked just
put in a disclaimer at the top of the email itself (need not worry about if
anything is in the email, users should click on this button first before
tying anything)

Any and all help would be greatly appreciated.


PS the code (this is in class that extends IDTExtensibility2)
--------------------------
Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnBeginShutdown
On Error Resume Next
' Notify the user you are shutting down, and delete the button.
MsgBox("Our custom Add-in is unloading.")
MyButton.Delete()
MyButton = Nothing

End Sub

Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnAddInsUpdate
'
End Sub

Public Sub OnStartupComplete(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnStartupComplete

Dim oCommandBars As CommandBars
Dim oStandardBar As CommandBar

On Error Resume Next
' Set up a custom button on the "Standard" command bar.
oCommandBars = applicationObject.CommandBars
If oCommandBars Is Nothing Then
' Outlook has the CommandBars collection on the Explorer object.
oCommandBars = applicationObject.ActiveExplorer.CommandBars
End If

oStandardBar = oCommandBars.Item("Standard")
If oStandardBar Is Nothing Then
' Access names its main toolbar Database.

oStandardBar = oCommandBars.Item("Database")

End If

' In case the button was not deleted, use the exiting one.
MyButton = oStandardBar.Controls.Item("My Custom Button")
If MyButton Is Nothing Then

MyButton = oStandardBar.Controls.Add(1)
With MyButton
.Caption = "My Custom Button"
.Style = MsoButtonStyle.msoButtonCaption

' The following items are optional, but recommended.
' The Tag property lets you quickly find the control
' and helps MSO keep track of it when more than
' one application window is visible. The property is required
' by some Office applications and should be provided.

.Tag = "My Custom Button"

' The OnAction property is optional but recommended.
' It should be set to the ProgID of the add-in, so that if
' the add-in is not loaded when a user clicks the button,
' MSO loads the add-in automatically and then raises
' the Click event for the add-in to handle.

.OnAction = "!<MyCOMAddin.Connect>"

.Visible = True
End With
End If

' Display a simple message to show which application you started in.
MsgBox("Started in " & applicationObject.Name & ".")


oStandardBar = Nothing
oCommandBars = Nothing


End Sub

Public Sub OnDisconnection(ByVal RemoveMode As
Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnDisconnection

On Error Resume Next
If RemoveMode <>
Extensibility.ext_DisconnectMode.ext_dm_HostShutdown Then _
Call OnBeginShutdown(custom)

applicationObject = Nothing


End Sub

Public Sub OnConnection(ByVal application As Object, ByVal connectMode
As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As
System.Array) Implements Extensibility.IDTExtensibility2.OnConnection


MsgBox("On Connection In MyAddin")
applicationObject = application
addInInstance = addInInst


' If you aren't in startup, manually call OnStartupComplete.
If (connectMode <> Extensibility.ext_ConnectMode.ext_cm_Startup)
Then _
Call OnStartupComplete(custom)

End Sub


Private Sub MyButton_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
Handles MyButton.Click
MsgBox("Our CommandBar button was pressed!")
End Sub
 
G

Guest

You need to add your custom command bar or buttons via the
Inspector.Commandbars collection, not Explorer.Commandbars. See the link
below for a good example on trapping when new Inspectors are created. Once
you have an Inspector object, you can set a reference to a MailItem object
and get access to the Inspector.Commandbars collection when the MailItem_Open
event fires, and then create your custom buttons.

Slovak Technical Services Code Samples:
http://www.slovaktech.com/code_samples.htm#InspectorWrapper
 
G

Guest

Hey, I have this done so far. I need to make a button on the inspector that
when clicked will put in a disclaimer into the email text. I have this so far
and it seems to make the button. How do I go from here. I need to put a
string text into the current email but lager font and bold type when clicked.
HELP!!! As I understand it, putting formatting on the text is not very
simple, but how can I access the Inspector's email textbox to put in a string
into it.

Function CreateSampleToolbar() As Office.CommandBar
Dim objApp As Outlook.Application
Dim colCB As Office.CommandBars
Dim objCB As Office.CommandBar
Dim objControls As Office.CommandBarControls
Dim objControl As Office.CommandBarControl
Set objApp = CreateObject("Outlook.Application")
Set colCB = objApp.ActiveInspector.CommandBars
Set objCB = colCB.Add("Circular230", msoBarTop)
Set objControls = objCB.Controls
Set objControl = objControls.Add(msoControlButton)
objControl.Caption = "Circular 230"
objControl.Visible = True
objCB.Visible = True

Set CreateSampleToolbar = objCB

Set objCB = Nothing
Set colCB = Nothing
Set objApp = Nothing
End Function
 
G

Guest

All you need to do is add this to the General Declarations section for that
module:

Dim WithEvents objCustomButton As Office.CommandBarButton

Then in your Function, add:

Set objCustomButton = objControl

Now you can handle the objCustomButton_Click event, where you can get a
MailItem object from the ActiveInspector.CurrentItem property, and access
MailItem.Body to get/set the message text.
 
G

Guest

I'm sorry, I'm a bit confused. This is what I was trying to make from your
suggestion. I am doing this in Outlooks built in VBA. Before I had a module
object and it didnt let me make the WithEvents so I made a class. But would I
need a class declaration. I'm more famaliar with .NET. Please assume I dont
know much of VBA because I dont. What does _Click() take in as parameters???
------------------------------------

Dim WithEvents objCustomButton As Office.CommandBarButton

Function CreateSampleToolbar() As Office.CommandBar

Dim objApp As Outlook.Application
Dim colCB As Office.CommandBars
Dim objCB As Office.CommandBar
Dim objControls As Office.CommandBarControls
Dim objControl As Office.CommandBarControl

Set objApp = CreateObject("Outlook.Application")
Set colCB = objApp.ActiveInspector.CommandBars
Set objCB = colCB.Add("Circular230", msoBarTop)
Set objControls = objCB.Controls

Set objCustomButton = objControl
objControl.Caption = "Circular 230"

objControl.Visible = True
objCB.Visible = True

Set CreateSampleToolbar = objCB

Set sInspector = CreateObject("Redemption.SafeInspector")
sInspector.Item = Application.ActiveInspector
sInspector.SelText = "Testing circular 210"
Set RTFEditor = sInspector.RTFEditor
RTFEditor.SelAttributes.Style.Bold = True


Set objCB = Nothing
Set colCB = Nothing
Set objApp = Nothing

End Function

Sub objCustomButton_Click()

End Sub
 
G

Guest

Sorry, I wasn't very clear. I've got a better example. I'll start with the
code that will create a custom class called clsInspectorTrapper. Put this
anywhere you want (Module or Class; usually the ThisOutlookSession module),
as long as you have this in the General Declarations section:

Option Explicit
Dim myInspectorTrapper As clsInspectorTrapper

Now we have to create this class in a test procedure (or whatever procedure
you want):

Sub TestTheInspectorTrapper()
Set myInspectorTrapper = New clsInspectorTrapper
'Usually this should be called in the
ThisOutlookSession.Application_Startup() event.
End Sub

Sub FinishedWithTheInspectorWrapper()
Set myInspectorTrapper = Nothing
End Sub

Now create the class called clsInspectorTrapper and add the code below.
Note the comments. If you have any questions, let me know:


Option Explicit
Dim WithEvents objInspectors As Outlook.Inspectors
Dim WithEvents objMyInspector As Outlook.Inspector
Dim WithEvents objMyMailItem As Outlook.MailItem
Dim WithEvents objMyCustomButton As Office.CommandBarButton

Private Sub Class_Initialize()
'Return a handle to the Inspectors collection
Set objInspectors = Application.Inspectors
End Sub

Private Sub Class_Terminate()
Set objInspectors = Nothing
Set objMyMailItem = Nothing
Set objMyCustomButton = Nothing
End Sub

Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
'This event will fire every time an item or Window is opened in Outlook
(for e-mail, Contacts, Tasks, etc.)

If Inspector.CurrentItem.Class <> olMail Then
'Only handle MailItem objects
Exit Sub
End If

'Set a reference to the e-mail so that we can trap the Open event
Set objMyMailItem = Inspector.CurrentItem
Set objMyInspector = Inspector
End Sub

Private Sub objMyCustomButton_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
MsgBox "I've been clicked!"
End Sub

Private Sub objMyMailItem_Close(Cancel As Boolean)
Set objMyMailItem = Nothing
End Sub

Private Sub objMyMailItem_Open(Cancel As Boolean)
Dim objCommandBar As Office.CommandBar

Set objCommandBar = objMyInspector.CommandBars("Standard")
Set objMyCustomButton = objCommandBar.Controls.Add(msoControlButton, , ,
, True) 'Set True for temporary buton
objMyCustomButton.Caption = "My custom button"
End Sub


--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/
 
G

Guest

Im getting a variable not declared error in the module that I put the
creation of the clsInspectorTrapper class. I wish I can send a screenshot. Is
that possible?

Ok I made a module that has the following code. I just right clicked on
ThisOutlookSession and Insert and Module. It made it module 1. Note(for some
reason it makes a Modules directory that is on the same level as Microsoft
Outlook Objects which has ThisOutlookSession inside it. So I dont know if it
is really making this module inside thisoutlooksession, but I guess thats not
my biggest problem at this state.

The smartcomplete seems to see the variable because it changes my casing on
as im typing, but I get a variable. Is it possible for me to send you a
screenshot somewhere????
--------------------------------------------------------
Option Explicit
Dim myInspectorTrapper As clsInspectorTrapper

Private Sub TestTheInspectorTrapper()
Set myInspctorTrapper = New clsInspectorTrapper
End Sub

Private Sub FinishedWithTheInspectorWrapper()
Set myInspectorTrapper = Nothing
End Sub
-------------------------------------------------------------

*****THEN
I make a class by doing the same. It made another folder called Class
Modules and put in Class1 (which i renames to clsInspectorTrapper).

With exactly what you had
-----------------------------------------------------
Option Explicit
Dim WithEvents objInspector As Outlook.Inspectors
Dim WithEvents objMyInspector As Outlook.Inspector
Dim WithEvents objMyMailItem As Outlook.MailItem
Dim WithEvents objMyCustomButton As Office.CommandBarButton

Private Sub Class_Initialize()
Set objInspector = Application.Inspectors
End Sub

Private Sub Class_Terminate()
Set objInspectors = Nothing
Set objMyMailItem = Nothing
Set objMyCustomButton = Nothing
End Sub

Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
'This event fires every time an item or Window is opened in Outlook (for
email, contacts,
'Tasks, etc.)
If Inspector.CurrentItem.Class <> olMail Then
'Only handle MailItem Objects
Exit Sub


'Set reference to the e-mail so that we can trap the Open event
Set objMyMailItem = Inspector.CurrentItem
Set objMyInspector = Inspector

End Sub

Private Sub objMyCustomButton_Click(ByVal ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
MsgBox "I've been Clicked!"
End Sub

Private Sub objMyMailItem_Close(Cancel As Boolean)
Set objMyMailItem = Nothing
End Sub

Private Sub objmymailitem_open(Cancel As Boolean)
Dim objCommandBar As Office.CommandBar
Set objCommandBar = objMyInspector.CommandBars("Standard")
Set objCustomButtom = objCommandBar.Controls.Add(msoControlButton, , , , True)
objMyCustomButton.Caption = "Circular 230"
objMyCustomButton.Visible = True
objCommandBar.Visible = True
End Sub
-------------------------------------------------------------------------
 
G

Guest

Sorry - I deliberated changed the order of the code in my reply to illustrate
the easy stuff first. The caveat, of course, is that declaring the
myInspectorTrapper variable isn't going to work until the clsInspectorTrapper
object is available - thus you need to create the clsInspectorTrapper Class
Module first before you can actually compile the code. If you have that
class created and named as above, the referring variable should know it is
declared against a valid class object. Try compiling now that you have the
Class created.

The display of Modules and Class Modules (and User Forms) in the Project
Explorer is irrelevant, whether they are grouped by type or if it is listed
in a flat display. The ThisOutlookSession is essentially the default Module,
which cannot be removed. The only difference is that it has an explicit bind
to the Application object, which you'll see in addition to (General) in the
combo box on the left side at the top of the editor. This box also lists any
other module level variables that are declared WithEvents (you'll see the
variables that you've declared in clsInspectorTrapper in there as well when
you view the code from that class).
 
G

Guest

This is working out great. Now after the user clicks on this button. How can
I access the email textarea of the current inspector. I need to put in a
disclaimer. And is there a way to make it a certain font and/or bold as I am
putting it in?

Also, I noticed that since the button is temporary every time I start
outlook I have to run the module. I need to have it load on my clients
machine every time. I know you can make a COM object that will load this
module at loadtime right? Or is it better to make it not a temporary button
that way it stays?

------------------------------------------------------------
Private Sub objMyCustomButton_Click(ByVal ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
MsgBox "I've been Clicked!"
End Sub

Thanks.
 
G

Guest

If you want to install this on multiple machines, the best approach (but more
work and a little more involved) is to implement this as a COM Add-In.

However, after thinking about your intent, I believe it will be easier just
to manually create your custom button for the Inspector windows, and
associate it with a simple Sub procedure macro. This way, you don't have to
worry about trapping Inspectors and automatically creating the button. Just
call the ActiveInspector object in this procedure. It can be fired by
mapping your custom button to the procedure name in the menu customization
dialog.

If you want to customize Rich Text formatting in the message body, the only
way to do this is with Redemption's SafeInspector object
(http://www.dimastr.com). Otherwise, for HTML formatting you can use good
ol' HTML tags. Remember that the .Body or .HTMLBody properties are just one
big fat string. There's no "sequential" editing or anything fancy you can do
(like ReadLine, ReadAll, etc.).

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/
 
G

Guest

I see. I am working on 2 version, one for outlook 2003 and one for 2000.
The 2003 verion works fine. Because I am able to use the BodyFormat method
of the mail. So my basic idea was to store the current email in a string and
then put the disclaimer in and then concat the current and then change the
format of the body to html and this works perfectly on 2003 (it keeps the
original formatting, spacing, carriage returns, fonts and all), but in
outlook 2000 the .BodyFormat doesnt exit, therefore like you said I get plain
text back from the string that I store, and cant convert it to html.
So you saying, other then redemption, there is no way around it (keeping the
original formatting of the email, that is).

Private Sub objMyCustomButton_Click(ByVal ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
Dim current As String
current = objMyMailItem.HTMLBody
objMyMailItem.HTMLBody = "<table><tr><td><font face=Verdana
size=3><strong>Disclaimer</strong></font><br></td></tr></table>" & current
objMyMailItem.BodyFormat = olFormatHTML
End Sub
 
G

Guest

You can handle the plain text e-mails by inserting your disclaimer at the end
of the string:

MailItem.Body = MailItem.Body & vbCrlf & vbCrlf & "Insert my really long
disclaimer at the end"

vbCrlf is a constant that represents a new line. You could also use vbTab,
but that's about it for plain text formatting options.
 
G

Guest

Taking your advise about using Redemptions SafeInspector. But I'm having
trouble finding any examples that show completely what to do. Modifying your
methods this is what I have. I'm assuming after installing the com object
that you simply use Dim sInspector AS Redemption.SafeInspector to declare the
variable? Now getting past that point I'm a bit hazy as to does the
safeinspector object equvalent to the Outlook.MailItem. Thefore would I need
to create a mailitem or just a SafeInspector. If both then how would I set
the mailitem to have the safeinspector as its email body. And if
safeinspector is all I need then do you know how to modify my buttonclicked
event.

Option Explicit
Dim WithEvents objInspector As Outlook.Inspectors
Dim WithEvents objMyInspector As Outlook.Inspector
Dim WithEvents objMyMailItem As Outlook.MailItem
Dim WithEvents objMyCustomButton As Office.CommandBarButton
Dim sInspector As Redemption.SafeInspector

Private Sub objInspector_NewInspector(ByVal Inspector As Inspector)
'MsgBox "Comes to create Inspector"
'This event fires every time an item or Window is opened in Outlook (for
email, contacts,
'Tasks, etc.)
If Inspector.CurrentItem.Class <> olMail Then
'Only handle MailItem Objects
Exit Sub
End If


'Set reference to the e-mail so that we can trap the Open event
Set objMyMailItem = Inspector.CurrentItem
Set objMyInspector = Inspector
Set sInspector = CreateObject("Redemption.SafeInspector")
sInspector.Item = objMyMailItem


Private Sub objMyCustomButton_Click(ByVal ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
Dim current As String

current = sInspector.RTFEditor.RTFText
'MsgBox current

'sInspector.RTFEditor.RTFText = "<table><tr><td><font face=Verdana
size=3><strong>Four score and seven years ago our fathers brought forth on
this continent a new nation, conceived in liberty, and dedicated to the
proposition that all men are created equal.<br>Now we are engaged in a great
civil war, testing whether that nation, or any nation so conceived and so
dedicated, can long endure. We are met on a great battlefield of that war. We
have come to dedicate a portion of that field as a final resting-place for
those who here gave their lives that nation might live. It is altogether
fitting and proper that we should do
this.</strong></font><br></td></tr></table>" & current

End Sub
 
G

Guest

The SafeInspector object is equivalent to Outlook's Inspector object, and the
SafeMailItem is equivalent to Outlook's MailItem object. However, you may
not need a separate variable for a SafeMailItem. Remember what I said two
posts back - it would be easier if you just created a macro procedure that
you map to a manually created custom toolbar button. Then you just have to
worry about getting the open Inspector via Application.ActiveInspector
instead of monitoring the Inspectors_Add event, etc.

Once you have a SafeInspector object, use its properties and methods as
outlined in the reference at http://www.dimastr.com to work with the message
body content.

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top