Results 1 to 7 of 7

Thread: [RESOLVED] Read Contents of Mail Items

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Resolved [RESOLVED] Read Contents of Mail Items

    I use Outlook 2002 and have a number of emails saved in one of Outlook's folders. How would I go about reading the contents of those emails using VB6 and not VBA?

  2. #2

  3. #3

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Read Contents of Mail Items

    I'm not exactly interested in automating Outlook but rather just to be able to examine emails that I've stored in a particular Outlook folder. For example I have the following which reads the Subject and Body of everything in the Inbox. It does what I want to do except that rather than the Inbox I want to drill down to one of my personal folders and I so far can't figure out how to do that.

    Code:
    Dim myOLApp As New Outlook.Application
    Dim olNameSpace As Outlook.NameSpace
    Dim myFolder As Outlook.MAPIFolder
    Dim i As Integer
    Dim strSubject As String
    Dim strBody As String
    
    Set myOLApp = CreateObject("Outlook.Application")
    Set olNameSpace = myOLApp.GetNamespace("MAPI")
    Set myFolder = olNameSpace.GetDefaultFolder(olFolderInbox) ' This is the line I need to change/replace
    
    For i = 1 To myFolder.Items.Count
        strSubject = myFolder.Items(i).Subject
        strBody = myFolder.Items(i).Body
    Next
    Set myOLApp = Nothing
    Set olNameSpace = Nothing

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Read Contents of Mail Items

    You can loop through folders - it's a collection:
    Code:
    Private Sub Command1_Click()
    '====================================
    Dim oOutlook As Outlook.Application
    Dim oNameSpace As Outlook.NameSpace
    Dim oFolder As Outlook.MAPIFolder
    Dim oSubFolder As Outlook.MAPIFolder
    Dim oItem As Outlook.MailItem
    
        Set oOutlook = New Outlook.Application
        Set oNameSpace = oOutlook.GetNamespace("MAPI")
        
        For Each oFolder In oNameSpace.Folders
            Debug.Print oFolder.Name
            For Each oSubFolder In oFolder.Folders
                Debug.Print "     " & oSubFolder.Name
            Next oSubFolder
        Next oFolder
        
        Set oNameSpace = Nothing
        Set oOutlook = Nothing
    
    End Sub
    You may now loop through items collection in whatever "subfolder" you wish (I named it that way so it's easier to picture entire structure).

    Hope this helps.

  5. #5

  6. #6

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: [RESOLVED] Read Contents of Mail Items

    Actually Rhino it's not resolved, at least not fully. The problem is that my personal folders structure looks like this (in part)

    Primary1
    ----Secondary
    --------Tertiary
    Primary2
    Primary3


    and your code only finds Primary1, 2 and 3, etc. I know I can do something like the following (after I add another nested For Each), but is there any way to directly point to Tertiary?

    Code:
    Dim oOutlook As Outlook.Application
    Dim oNameSpace As Outlook.NameSpace
    Dim oFolder As Outlook.MAPIFolder
    Dim oSubFolder As Outlook.MAPIFolder
    Dim oSubSubFolder As Outlook.MAPIFolder
    
    Dim oItem As Outlook.MailItem
    
        Set oOutlook = New Outlook.Application
        Set oNameSpace = oOutlook.GetNamespace("MAPI")
        
        For Each oFolder In oNameSpace.Folders
            Debug.Print oFolder.Name
            For Each oSubFolder In oFolder.Folders
                If oSubFolder.Name = "Primary1" Then
                    For Each oSubSubFolder In oSubFolder.Folders
                        Debug.Print "     " & oSubSubFolder.Name
                    Next
                End If
            Next oSubFolder
        Next oFolder
        
        Set oNameSpace = Nothing
        Set oOutlook = Nothing

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: [RESOLVED] Read Contents of Mail Items

    You will need to implement some sort of recursive loop.
    For that you can use Folders.Count (I think) and Items.Count properties.
    So, basically you will loop through each folder/subfolder if it has at least 1 mail item or 1 subfolder until counts are zero.
    Then you'll return back to main loop... Do While/Until will probably work better then For - Next loop.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width