PDA

View Full Version : Solved: button on the opened email



michelle
05-24-2005, 05:26 AM
Dear readers,

We are using outlook 2003 and we are trying to get a button on a opened email and place VBA code behind the button. A button on outlook we can place.

Can somebody give help pls?

Michelle.:doh:


PS. record a macro is not not possible!

MOS MASTER
05-24-2005, 01:10 PM
Hi Michelle, :yes

Well you're right there's no recorder in Outlook! :rofl:

What you want is possible but not the most easy thing to do.
You would need an Inspector Wrapper to catch each window being opend and check if that window is a new mail message.

Then capture it and create a new button on a toolbar. I'll choose "Standard"
We will use a class module to code for the events needed.

Don't know how familiar you are with coding so I'll start from the beginning.

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 this code to initialize the class module we are going to 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


So no let's insert a class module
Choose Insert/Class module
Press F4 the properties pane will appear
Change the name of the new class to "clsInspector"
Double click on class "clsInspector" (Or F7)
Paste this code:

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")
Set oMailButton = oMailBar.Controls.Add(Type:=msoControlButton)
oMailBar.Visible = True
With oMailButton
.Caption = "Say Hi"
.FaceId = 1000
.Style = msoButtonIconAndCaption
End With
End Sub

Private Sub oOpenMail_Close(Cancel As Boolean)
On Error Resume Next
oMailInspector.CommandBars("Standard").Controls("Say Hi").Delete

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 Class_Terminate()
Set oAppInspectors = Nothing
Set oOpenMail = Nothing
Set oMailButton = Nothing
End Sub


Now go to the Error menu and choose "Project1 Compile"
Close the Editor
Close Outlook Choose yes when you're asked to save the OTM-File
Restart Outlook
Open a new mail .... TADA..a new button appears!
Press the button and some text will be inserted in the body of the mailitem.
So I think this should do it for you!

Enjoy..:whistle:

Steiner
05-25-2005, 12:50 AM
Wow, that's interesting, I never used those inspectory before. :bow: I think you should make this a KB entry!

Daniel

michelle
05-25-2005, 12:57 AM
Hello MOS MASTER,

Thanks a lot for the energy you put in the written code.

I did everything you said but with the start of our code there apears an error:

Option Explicit
Dim TrapInspector As clsInspector

The type clsInspector is not reconized by Outlook.

Googling for clsInspector gave no results!

Maybe there must be a reference extra be choosen, the next references I chosed:

-- VB for applications
-- MS Outlook 11.0 object library
-- OLE automation
-- MS forms 2.0 object library
-- MS office 11.0 object library

I can't find a reference for using the clsInspector!
Can you pls tell me wich reference I do need?
_________________________________________________________

I don't understand the code when starting:
Set TrapInspector = New clsInspector

The TrapInspector never come back in your code

Greetings,

Michelle.

Steiner
05-25-2005, 01:06 AM
clsInspector is the class you should just have created yourself (it's the second code snippet). Have a look at number 3 and 4 betweend the 2 code snippets above:
- press F4 the properties pane will appear
- Change the name of the new class to "clsInspector"

Daniel

michelle
05-25-2005, 01:18 AM
Steiner und MOS MASTER

You are both great!

It is working!

:friends:
Michelle!!

MOS MASTER
05-25-2005, 10:15 AM
Wow, that's interesting, I never used those inspectory before. :bow: I think you should make this a KB entry!

Daniel
Hi Daniel,

Thank you! :friends:

Ok I'll put it on my to do list .....

MOS MASTER
05-25-2005, 10:16 AM
Steiner und MOS MASTER

You are both great!

It is working!

:friends:
Michelle!!
Hi Michelle, :yes
You're welcome! :beerchug:

Airborne
09-09-2005, 05:17 PM
Hi Joost,


:thumb works great. I wonder if it's also possible to add a standard emotiocon stored in e.g. c:\temp? When I click the button "Icon" it will insert the emoticon?:eek:

Regards,

Rob.

MOS MASTER
09-10-2005, 06:13 AM
Hoi Rob, :yes

Well haven't tried that yet but it should be possible.

Try out this excellent article by our fellow dutchman Romke Soldaat:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoffsol02/html/odc_ButtonBonanzaPartI.asp

He uses a bimap to the button but perhaps a emoticon should work too.

HTH, :whistle:

Airborne
09-10-2005, 06:08 PM
Thanks Joost:thumb

In Private Sub oOpenMail_Open I've added oMailButton.BeginGroup = True. To give the button it's own group.

and in Private Sub oMailButton_Click I've added oOpenMail.Subject = "name of subject"
oOpenMail.To = "E-mail Addresses"
oOpenMail.Send I did that because I often forward e-mail to the same persons with the same subject and want to send it right away.

One question....with oOpenMail.Body = "some text" the problem is that this will delete all the text that's already there. Is it possible to just add text to the body?:think:


Thanks,
Rob.


p.s. I know that this post was already marked solved, I'm not sure if I should start a new post for my questions?

Ken Puls
09-10-2005, 09:44 PM
Hey Rob,

Not 100% sure, as I don't code in Outlook, but what about this:

oOpenMail.Body = oOpenMail.Body & vbnewline & "This is a new line of text"

MOS MASTER
09-11-2005, 04:59 AM
Hey Rob,

Not 100% sure, as I don't code in Outlook, but what about this:

oOpenMail.Body = oOpenMail.Body & vbnewline & "This is a new line of text"

Excellent Ken!

Well I'm 100% sure this works cause I use this to insert a signature to my emails! :yes

So Rob take our Outlook Guru's advice (MD Ken) cause he's right on the money! :devil: (Again scary Ken.....) :rofl:

MOS MASTER
09-11-2005, 05:01 AM
Hoi Rob, :yes

Well haven't tried that yet but it should be possible.

Try out this excellent article by our fellow dutchman Romke Soldaat:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoffsol02/html/odc_ButtonBonanzaPartI.asp

He uses a bimap to the button but perhaps a emoticon should work too.

HTH, :whistle:

Hi Rob, :yes

I see thanx Joost but am Not sure it worked for you?

You're welcome anyhow!...:rofl:

Tell me did you use it with succes? Did you alter the code and how?

Later buddy.

Airborne
09-11-2005, 02:59 PM
Hi Ken, Joost


Not 100% sure, as I don't code in Outlook, but what about this I'm glad that you are showing up in the Outlook forumhttp://vbaexpress.com/forum/images/smilies/045.gif . Almost there. I use the code when I reply to e-mails. The new line of text now shows up on the bottom of the reply but it should be on top of the replyhttp://vbaexpress.com/forum/images/smilies/eek.gif .


I see thanx Joost but am Not sure it worked for you? Thanks for the link. The link seems to explain how to change/add icons to buttons however but what I meant was, how to copy/paste an emoticon from a directory on my harddisk into the e-mail I want to sent by using code in Outlook?

Thanks guys,

Rob.

MOS MASTER
09-11-2005, 03:47 PM
Hi Ken, Joost

I'm glad that you are showing up in the Outlook forumhttp://vbaexpress.com/forum/images/smilies/045.gif . Almost there. I use the code when I reply to e-mails. The new line of text now shows up on the bottom of the reply but it should be on top of the replyhttp://vbaexpress.com/forum/images/smilies/eek.gif .

Hoi Rob,

Well untested but one of these two should do the job getting the current body behind the inserted text:
oOpenMail.body = "This is a new line of text" & vbNewLine & oOpenMail.body
'Or
sTmpBody = oOpenMail.body
oOpenMail.body = "This is a new line of text" & vbNewLine & sTmpBody




Thanks for the link. The link seems to explain how to change/add icons to buttons however but what I meant was, how to copy/paste an emoticon from a directory on my harddisk into the e-mail I want to sent by using code in Outlook?

Thanks guys,

Rob.

This is pretty tough to do en requires difficult coding most of the times.

Can you tell which Editor you use for your outlook? Word or Outlook editor?

MOS MASTER
09-11-2005, 03:48 PM
O I see I have one in my links.

This one by Sue Mosher is a very nice way to get a picture in the HTML emailbody:
http://www.outlookcode.com/d/code/htmlimg.htm

HTH, :whistle:

Airborne
09-11-2005, 08:06 PM
Yep Joost:yes oOpenMail.body = "This is a new line of text" & vbNewLine & oOpenMail.body does the trick. Thanks.

I'll check out the link and will let you know. Thanks again,

Rob.

MOS MASTER
09-12-2005, 08:43 AM
Hi Rob, :yes

Great..you're welcome! :thumb

Can't wait to here if the picture sub worked for you as well!

Airborne
09-15-2005, 10:06 AM
:thumb Great link Joost, thanks. It works! I will have more questions coming but I'll start another thread for those.


Cheers,

Rob.

MOS MASTER
09-15-2005, 03:15 PM
:thumb Great link Joost, thanks. It works! I will have more questions coming but I'll start another thread for those.


Cheers,

Rob.

Glad to here that Bob...Graag gedaan! :thumb