Catch the current mailitem in Outlook

Posted on 2009-04-28
Last Modified: 2013-12-17
I am trying to find a way to do the equivalent to Currentproject/ActiveDocument in Outlook. I want to open a new e-mail, run my code which then changes my active e-mail in some ways (e.g. shows the from-field).
Question by:hke72
    LVL 76

    Expert Comment

    by:David Lee
    Hi, hke72.

    The statement

        objMsg = Outlook.Application.ActiveInspector.CurrentItem

    will get you the currently open message.
    LVL 1

    Author Comment

    But how can I run it? I am trying to use the code on an open e-mail. I want to put some predefined facts into the e-mail. If I push Alt+F11 in outlook I get  "This Outlook session". But this code could only be run from the Outlook window?
    LVL 76

    Accepted Solution

    Sorry, from the way you worded your question I assumed you were familiar with scripting in Microsoft Office and just wanted to know what would return the current item.  Here's a more complete version of what you'll need.  This is how you'd add this to Outlook.

    1.  Start Outlook
    2.  Click Tools > Macro > Visual Basic Editor
    3.  If not already expanded, expand Microsoft Office Outlook Objects
    4.  If not already expanded, expand Modules
    5.  Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
    6.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
    7.  Edit the code as needed.  I included comments wherever something needs to or can change
    8.  Click the diskette icon on the toolbar to save the changes
    9.  Close the VB Editor

    To use this

    1.  Create a new message, or reply to/forward an existing message
    2.  Run the macro
    Sub MyMacro()
        Dim olkMsg as Outlook.MailItem
        'Get the currently open message'
        Set olkMsg = Outlook.Application.ActiveInspector.CurrentItem
        'Add some text to the beginning of the message'
        olkMsg.Body = "My text" vbCrLf & vbCrLf& olkMsg.Body
        'Destroy the object to avoid memory leaks'
        Set olkMsg = Nothing
    End Sub

    Open in new window

    LVL 1

    Author Closing Comment

    Thank you!
    LVL 76

    Expert Comment

    by:David Lee
    You're welcome.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Create high volume marketing opportunities using email signatures with these top 10 DOs and DON'Ts of email signature marketing.
    Set OWA language and time zone in Exchange for individuals, all users or per database.
    Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
    In this video we show how to create a Distribution Group in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >>…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now