?
Solved

Get Email Fields in VBA

Posted on 2006-06-22
3
Medium Priority
?
439 Views
Last Modified: 2010-04-08
I want to write a VBA macro to get the field values (subject, body, To, etc) from an email. I will run it from a toolbar.
I know how to create an email and edit them there but how do I get them from an already created email that is open or currently selected?
0
Comment
Question by:stopher2475
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Author Comment

by:stopher2475
ID: 16962559
I found some code to get the currently selected email.
The one problem with it is that if I have another email open that is not the
one selected in the explorer window the user might be getting data from the wrong email.
Is there any way to use the current email if thats where you're running it from?

Public Sub GetMailFields()
Dim objExplorer As Outlook.Explorer
Dim objSelection As Outlook.Selection
Dim objItem As Outlook.MailItem 'Object
Dim strheader As String

' MAPI property tag used
objSession.Logon , , False, False, 0 ' Use the existing Outlook session

Set objExplorer = ThisOutlookSession.ActiveExplorer
Set objSelection = objExplorer.Selection

' Get selected Message ID
Set objItem = objSelection.Item(1)

MsgBox objItem.Subject & Chr(10) & _
        objItem.To & Chr(10) & _
        objItem.SentOn

' Tidy up
Set objExplorer = Nothing
Set objSelection = Nothing
Set objItem = Nothing

End Sub
0
 
LVL 2

Author Comment

by:stopher2475
ID: 16962633
Found out how to get the currently open email as well.
Added a condition to use ThisOutlookSession.ActiveInspector.CurrentItem
if the active window wasn't the activeexplorer.

Public Sub GetMailFields()
   
    Dim objExplorer As Outlook.Explorer
    Dim objSelection As Outlook.Selection
    Dim objItem As Outlook.MailItem 'Object
    Dim strheader As String
   
    ' MAPI property tag used
    'objSession.Logon , , False, False, 0 ' Use the existing Outlook session
   
    Set objExplorer = ThisOutlookSession.ActiveExplorer
    Set objSelection = objExplorer.Selection
   
    ' Get selected Message ID
    If ThisOutlookSession.ActiveWindow = objExplorer Then
        Set objItem = objSelection.Item(1)
    Else
        Set objItem = ThisOutlookSession.ActiveInspector.CurrentItem
    End If
   
    MsgBox objItem.Subject & Chr(10) & _
            objItem.To & Chr(10) & _
            objItem.SentOn
   
   
    ' Tidy up
    Set objExplorer = Nothing
    Set objSelection = Nothing
    Set objItem = Nothing

End Sub
0
 
LVL 76

Accepted Solution

by:
David Lee earned 1500 total points
ID: 16963952
Hi stopher2475,

Do you want all the property values or only certain ones?  If certain ones, which ones?

Cheers!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
In this article I discuss my selections of the Top Four free Outlook OST File Viewers available. Open, view and read even damaged OST files by using these tools. They all provide a clear preview of all data such as emails, notes, tasks, calendars, e…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

718 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