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?
Printable View
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?
If automating outlook is an option then have a look at this sample directly from msdn.
While it might not be exactly what you need it should still give you pletty good idea [I think].
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
You can loop through folders - it's a collection:
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).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
Hope this helps.
Thanks.
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
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.