Solved

Get Email Fields in VBA

Posted on 2006-06-22
3
422 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 500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Changing a few Outlook Options can help keep you organized!
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

734 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