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.
PS. record a macro is not not possible!
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.
PS. record a macro is not not possible!
Hi Michelle,
Well you're right there's no recorder in Outlook!
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.
[VBA]
- 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
[/VBA]
[VBA]
- 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
[/VBA]
So I think this should do it for you!
- 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.
Enjoy..
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
Wow, that's interesting, I never used those inspectory before. I think you should make this a KB entry!
Daniel
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.
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
Steiner und MOS MASTER
You are both great!
It is working!
Michelle!!
Hi Daniel,Originally Posted by Steiner
Thank you!
Ok I'll put it on my to do list .....
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
Hi Michelle,Originally Posted by michelle
You're welcome!
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
Hi Joost,
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?
Regards,
Rob.
[UVBA].....[/UVBA]
Hoi Rob,
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/de...nanzaPartI.asp
He uses a bimap to the button but perhaps a emoticon should work too.
HTH,
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
Thanks Joost
In Private Sub oOpenMail_Open I've added [VBA]oMailButton.BeginGroup = True[/VBA]. To give the button it's own group.
and in Private Sub oMailButton_Click I've added [VBA]oOpenMail.Subject = "name of subject"
oOpenMail.To = "E-mail Addresses"
oOpenMail.Send[/VBA] 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 [VBA]oOpenMail.Body = "some text"[/VBA] the problem is that this will delete all the text that's already there. Is it possible to just add text to the body?
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?
[UVBA].....[/UVBA]
Hey Rob,
Not 100% sure, as I don't code in Outlook, but what about this:
[vba]oOpenMail.Body = oOpenMail.Body & vbnewline & "This is a new line of text"[/vba]
Ken Puls, CMA - Microsoft MVP (Excel)
I hate it when my computer does what I tell it to, and not what I want it to.
Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar
This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!
Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!
Excellent Ken!Originally Posted by kpuls
Well I'm 100% sure this works cause I use this to insert a signature to my emails!
So Rob take our Outlook Guru's advice (MD Ken) cause he's right on the money! (Again scary Ken.....)
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
Hi Rob,Originally Posted by MOS MASTER
I see thanx Joost but am Not sure it worked for you?
You're welcome anyhow!...
Tell me did you use it with succes? Did you alter the code and how?
Later buddy.
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
Hi Ken, Joost
I'm glad that you are showing up in the Outlook forum . 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 reply .Not 100% sure, as I don't code in Outlook, but what about this
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?I see thanx Joost but am Not sure it worked for you?
Thanks guys,
Rob.
[UVBA].....[/UVBA]
Hoi Rob,Originally Posted by Airborne
Well untested but one of these two should do the job getting the current body behind the inserted text:[vba]
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
[/vba]
This is pretty tough to do en requires difficult coding most of the times.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.
Can you tell which Editor you use for your outlook? Word or Outlook editor?
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
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,
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
Yep Joost [VBA]oOpenMail.body = "This is a new line of text" & vbNewLine & oOpenMail.body[/VBA] does the trick. Thanks.
I'll check out the link and will let you know. Thanks again,
Rob.
[UVBA].....[/UVBA]
Hi Rob,
Great..you're welcome!
Can't wait to here if the picture sub worked for you as well!
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
Great link Joost, thanks. It works! I will have more questions coming but I'll start another thread for those.
Cheers,
Rob.
[UVBA].....[/UVBA]