Catch the current mailitem in Outlook

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).
Who is Participating?
David LeeConnect With a Mentor Commented:
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

David LeeCommented:
Hi, hke72.

The statement

    objMsg = Outlook.Application.ActiveInspector.CurrentItem

will get you the currently open message.
hke72Author Commented:
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?
hke72Author Commented:
Thank you!
David LeeCommented:
You're welcome.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.