Solved

Get Email Fields in VBA

Posted on 2006-06-22
3
395 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
  • 2
3 Comments
 
LVL 2

Author Comment

by:stopher2475
Comment Utility
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
Comment Utility
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
Comment Utility
Hi stopher2475,

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

Cheers!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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 …

763 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

7 Experts available now in Live!

Get 1:1 Help Now