Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to get email info from outlook?

Status
Not open for further replies.

Oliver2003

Technical User
Apr 19, 2003
144
GB
Is it possible to retrieve e-mail information from outlook, such as to, from, e-mail body, time date etc into a visual basic form?

How would this be done?

Any comment or ideas appreciated
Thank you
 
Yes, you can use the Outlook object model or CDO 1.21. The strategy is:

- Get a reference to the Outlook folder containing the mail items.
- Get a reference to that folder's Items collections
- Return an Item from the collection
- Read the item's properties such as Subject, Body, ReceivedTime etc.

The only property that's a bit tricky is sender e-mail address. Can get this through CDO but if coding against Outlook, you have to create a dummy reply to the mail item and get the e-mail address from its Recipients collection.

Here's a quick Outlook example. It uses late binding so will work with all versions of Outlook. If you're only targeting one version of Outlook then you can set a project reference and declare the object variables as Outlook object types:
Code:
Dim objOLApp As Object
Dim objNS As Object
Dim objFolder As Object
Dim objItem As Object

Set objOLApp = CreateObject("Outlook.Application")
Set objNS = objOLApp.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(6) 'olFolderInbox
For Each objItem In objFolder.Items
 With objItem
  Debug.Print "To: " & .To
  Debug.Print "From: " & .SenderName
  Debug.Print "Subject: " & .Subject
  Debug.Print "Received: " & .ReceivedTime
 End With
Next

Set objItem = Nothing
Set objFolder = Nothing
Set objNS = Nothing
Set objOLApp = Nothing


Paul Bent
Northwind IT Systems
 
You can also create a VB project within Outlook itself (.otm file). Tools/Macro/VB editor.
 
Thanks for the replies, Paul, your example does just what I wanted.
I used the .body to get the body on the email.

When getting to and from is it possible to get the actual e-mail address and not the name of the person that is displayed?

Thank you very much for your help!
 
Each mail item has a Recipients collection. Loop through the collection; for each recipient, return the Address property. The Type property indicates if it's a To, Cc or Bcc recipient.

As I mentioned you can create a dummy reply to get the sender e-mail address:

Dim objReply As Object

With objItem
Set objReply = .Reply
Debug.Print "Sender: " & objReply.Recipients(1).Address
End With

There's a slightly more direct way using CDO 1.21 but it requires more code up front to detect the Windows version and read the default profile from the appropriate registry key to logon to a CDO session.

Paul Bent
Northwind IT Systems
 
Ok, Cheers Paul I'll give that a try.

Do you know if it is possible to fire the "send \ Receive" event in outlook from vb?

Again, thanks for your help

Oliver
 
office2003 has a SenderEmailAddress property for a mailitem.
 
Yes, you can access the menu commands through the CommandBars hierarchy. These code snippets are from a class module; the Outlook application and namespace objects have already been instantiated during Initialize and are stored as private class members. It's also read the registry, determined the version of Outlook and stored it in a private member

The class supports multilingual versions of Outlook so the menu strings are loaded from a res file. I've commented the English strings in the code.
Code:
'--- DELIVER method
'--- Requests immediate delivery of all undelivered
'    messages submitted in the current session
    
Public Sub Deliver()
        
    Dim objExplorer As Object 'Temp Explorer object
    Dim objFolder As Object   'Temp folder object
    Dim objOLCBCol As Object  'Outlook Command Bars Collection object
    Dim objOLCB As Object     'Outlook Command Bar object
    Dim objOLPop As Object    'Outlook Menu top level item object
    Dim objOLCtl As Object    'Outlook Menu command object
        
    'Outlook must be running, ie there is an active Explorer
    'else the command bars collection won't be returned
    Set objExplorer = objOLApp.ActiveExplorer
    If objExplorer Is Nothing Then
        'Open an Outlook Explorer
        Set objFolder = objOLNS.GetDefaultFolder(olFolderInbox)
        Set objExplorer = objFolder.GetExplorer
        objExplorer.Activate
    End If
    'Deliver the message - this will send ~ALL~ items in the Outbox
    Set objOLCBCol = objOLApp.ActiveExplorer.CommandBars
    'Check we got the command bars - method might fail if
    'the operator closed the Explorer
    If objOLCBCol Is Nothing Then
        Err.Raise oerrNoCommandBars, , _
        "Deliver method error - command bars collection not found."
    End If
    'Get a reference to the Tools menu
    Set objOLCB = objOLCBCol("Menu Bar")
    'Tools menu is language specific
    '101 = Tools
    Set objOLPop = objOLCB.Controls(LoadResString(101))
    If gintOLVer >= 10 Then
        'Using Outlook 2002 or later, Send command no
        ' longer in Tools menu
        'Select Send All in Send/Receive submenu
        '105 = Send/Receive
        Set objOLPop = objOLPop.Controls(LoadResString(105))
        '106 = Send and Receive All
        Set objOLCtl = objOLPop.Controls(LoadResString(106))
    Else
        'Using Outlook 2000 or earlier; select Send in the Tools menu
        '102 = Send
        Set objOLCtl = objOLPop.Controls(LoadResString(102))
    End If
    objOLCtl.Execute
        
    'Tidy up
    Set objExplorer = Nothing
    Set objFolder = Nothing
    Set objOLCtl = Nothing
    Set objOLPop = Nothing
    Set objOLCB = Nothing
    Set objOLCBCol = Nothing
    
End Sub
You might find OLServices (ActiveX DLL) useful: The above is from the Deliver method of the OExplorer object. The OMailItem object has a SendNow method that does the same thing.

Paul Bent
Northwind IT Systems
 
Hi! What about getting info from your contatc list and / or your address book.

Any hint about that?

ElQuixiote ..

 
elquixiote,

It works just the same for contacts. First get the contacts folder then the Items collection.

Address books like the GAL or a PAB can be returned by name from the AddressLists collection of the namespace object. Each AddressList contains an AddressEntries collection.

Paul Bent
Northwind IT Systems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top