Solved

Get Email Fields in VBA

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
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: …
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

920 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

15 Experts available now in Live!

Get 1:1 Help Now