Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 21

Thread: Solved: button on the opened email

  1. #1

    Solved: button on the opened email

    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!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    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.
    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 this code to initialize the class module we are going to use
    [VBA]
    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]
    1. So no let's insert a class module
    2. Choose Insert/Class module
    3. Press F4 the properties pane will appear
    4. Change the name of the new class to "clsInspector"
    5. Double click on class "clsInspector" (Or F7)
    6. Paste this code:
    [VBA]
    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]
    1. Now go to the Error menu and choose "Project1 Compile"
    2. Close the Editor
    3. Close Outlook Choose yes when you're asked to save the OTM-File
    4. Restart Outlook
    5. Open a new mail .... TADA..a new button appears!
    6. 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..
    _________
    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)

  3. #3
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    Wow, that's interesting, I never used those inspectory before. I think you should make this a KB entry!

    Daniel

  4. #4
    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.

  5. #5
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    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

  6. #6
    Steiner und MOS MASTER

    You are both great!

    It is working!


    Michelle!!

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Steiner
    Wow, that's interesting, I never used those inspectory before. I think you should make this a KB entry!

    Daniel
    Hi Daniel,

    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)

  8. #8
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by michelle
    Steiner und MOS MASTER

    You are both great!

    It is working!


    Michelle!!
    Hi 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)

  9. #9
    VBAX Contributor Airborne's Avatar
    Joined
    Oct 2004
    Location
    Rotterdam
    Posts
    147
    Location
    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]

  10. #10
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    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)

  11. #11
    VBAX Contributor Airborne's Avatar
    Joined
    Oct 2004
    Location
    Rotterdam
    Posts
    147
    Location
    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]

  12. #12
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    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!





  13. #13
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by kpuls
    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]
    Excellent Ken!

    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)

  14. #14
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by MOS MASTER
    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,
    Hi Rob,

    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)

  15. #15
    VBAX Contributor Airborne's Avatar
    Joined
    Oct 2004
    Location
    Rotterdam
    Posts
    147
    Location
    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 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 .

    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.
    [UVBA].....[/UVBA]

  16. #16
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Airborne
    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 .
    Hoi Rob,

    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]

    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?
    _________
    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)

  17. #17
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    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)

  18. #18
    VBAX Contributor Airborne's Avatar
    Joined
    Oct 2004
    Location
    Rotterdam
    Posts
    147
    Location
    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]

  19. #19
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    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)

  20. #20
    VBAX Contributor Airborne's Avatar
    Joined
    Oct 2004
    Location
    Rotterdam
    Posts
    147
    Location
    Great link Joost, thanks. It works! I will have more questions coming but I'll start another thread for those.


    Cheers,

    Rob.
    [UVBA].....[/UVBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •