[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 610
  • Last Modified:

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).
0
hke72
Asked:
hke72
  • 3
  • 2
1 Solution
 
David LeeCommented:
Hi, hke72.

The statement

    objMsg = Outlook.Application.ActiveInspector.CurrentItem

will get you the currently open message.
0
 
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?
0
 
David LeeCommented:
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

0
 
hke72Author Commented:
Thank you!
0
 
David LeeCommented:
You're welcome.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now