hi all

i've been strugling with this error. could someone help me. below is the code

        stremail = "some@email"
        For Each store In outlookapp.Session.Stores
            strchecking = store.DisplayName
            outlookfolder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
            For Each objfolder In outlookfolder.Folders
                For Each obj As Object In objfolder
                    If TypeOf (obj) Is Outlook.MailItem Then
                        MsgBox(obj.Subject)
                    End If
                Next
            Next
        Next

the error says : "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
and it appears in this line outlookfolder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)

I'm actually using visual studio 2008 and the outlook version I am working with is both 2007 and 2010, but both of them prompt same error and I don't know how to fix it. The code I posted is all that needed to extract outlook stores and email item.

Oh! I forgot. below are the needed things to understand my code above

Imports Microsoft.Office.Interop.Outlook

Dim outlookapp As Outlook.Application = New Outlook.Application
Dim outlookmapi As Outlook.NameSpace = outlookapp.GetNamespace("MAPI")
dim outlookfolder as Outlook.folders
Dim objfolder As Outlook.folder
Dim store As Outlook.Store

The following seems to work to view e-mail subjects (from Inbox):

Add Reference to Microsoft Outlook xx.x Object Library (where xx.x is a number: 12.0, 14.0, etc)

  • Click "Project" (in menu)
  • Select "Add Reference"
  • Click "COM"
  • Select "Microsoft Outlook 14.0 Object Library"

Add Imports Outlook = Microsoft.Office.Interop.Outlook

    Private Sub getOutlookEmailInfo()

        Dim outlookapp As Outlook.Application = New Outlook.Application()
        Dim olNameSpace As Outlook.NameSpace = outlookapp.GetNamespace("MAPI")

        ' TODO: Replace the "YourValidProfile" and "myPassword" with 
        'Missing.Value if you want to log on with the default profile.
        'olNameSpace.Logon("YourValidProfile", "myPassword", True, True)
        'olNameSpace.Logon()

        Dim oInbox As Outlook.MAPIFolder = olNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        Dim oItems As Outlook.Items = oInbox.Items

        Dim output As String = String.Empty
        For Each email As Outlook.MailItem In oItems
            output += "subject: " & email.Subject & System.Environment.NewLine
        Next

        MessageBox.Show(output)

    End Sub

Resources:
Reading e-mail without Outlook app open

Looping over Outlook mail items in "Sent Items" folder

How to use the Microsoft Outlook Object Library to create an Outlook contact in Visual Basic .NET

How to automate Outlook by using Visual Basic

Edited 2 Years Ago by cgeier

Yes that will work. But my problem here is that the codes only works with the default pst loaded in the outlook. What I really want to do is to load all emails, not only emails from default pst, but also the pst(s) that is loaded in the outlook.

I don't understand what you are looking for. What do you mean by "but also the pst(s) that is loaded in the outlook"? Maybe you can post a screen shot and then a description based on the screenshot. Also are you trying to do this for all users (on a computer) or only the currently logged in user?

96868d5a4b854779bc4ab5ee5f16c03b

those are the pst(s) that is loaded on a outlook.

Regularly, emails that receives by the user moves automatically in the inbox of the default pst file. But there are cases that users use rules to automatically move email item into another folder on a pst file, which is sometimes not the default pst file.

right now, I can only handle reading emails from different folders on the default pst and I really really appreciate any help or advise that you can give to me.

The following was tested on Outlook 2010:

If you've already added the .pst files you want, do the following:

Version 1 (.pst manually added):

Private Sub getOutlookEmailInfo()
    Dim output As String = String.Empty
    Dim olApp As Outlook.Application = Nothing
    Dim olNameSpace As Outlook.NameSpace = Nothing

    'create new instance of Outlook.Application
    olApp = New Outlook.Application()

    'set olNameSpace to MAPI namespace
    olNameSpace = olApp.GetNamespace("MAPI")

    ' TODO: Replace the "YourValidProfile" 
    ' and "myPassword" with Missing.Value 
    ' if you want to log on with the default profile.
    ' olNameSpace.Logon("YourValidProfile", "myPassword", True, True)
    ' olNameSpace.Logon()


    'loop through stores
    For Each oStore As Outlook.Store In olNameSpace.Stores

        'root folder for store
        Dim rootFolder As Outlook.MAPIFolder = oStore.GetRootFolder()

        'folders for store
        Dim subFolders As Outlook.Folders = rootFolder.Folders

        'loop through all folders
        For Each oFolder As Outlook.Folder In subFolders

            'get folder items
            Dim oItems As Outlook.Items = oFolder.Items

            'search through each email
            For Each email As Object In oItems

                'make sure item is a mail item,
                'not a meeting request
                If email.MessageClass = "IPM.Note" Then
                    If TypeOf email Is Microsoft.Office.Interop.Outlook.MailItem Then
                        output += "oStore: " + oStore.DisplayName + " oFolder: " + oFolder.Name + " " + "subject: " & email.Subject & System.Environment.NewLine
                    End If
                End If
            Next
        Next
    Next

    'olNameSpace.Logoff()

    olNameSpace = Nothing
    olApp = Nothing

    MessageBox.Show(output)

End Sub

Version 1 Usage:

getOutlookEmailInfo()

If you want to programmatically add the .pst files.

Version 2 (.pst filename specified, display name not specified, .pst added programmatically):

Private Sub getOutlookEmailInfo(ByVal pstList As List(Of String))
    Dim output As String = String.Empty
    Dim olApp As Outlook.Application = Nothing
    Dim olNameSpace As Outlook.NameSpace = Nothing

    'create new instance of Outlook.Application
    olApp = New Outlook.Application()

    'set olNameSpace to MAPI namespace
    olNameSpace = olApp.GetNamespace("MAPI")

    ' TODO: Replace the "YourValidProfile" 
    ' and "myPassword" with Missing.Value 
    ' if you want to log on with the default profile.
    ' olNameSpace.Logon("YourValidProfile", "myPassword", True, True)
    ' olNameSpace.Logon()

    'used for removing stores that were 
    'added using 'AddStore'
    Dim pstDictionary As New Dictionary(Of String, String)

    For Each pst In pstList
        'add pst file to default profile
        olNameSpace.AddStore(pst)

        'get pst name
        'add to pstDictionary
        Dim pstName As String = olNameSpace.Folders.GetLast().Name
        If Not pstDictionary.ContainsKey(pstName) Then
            pstDictionary.Add(pstName, Nothing)
        End If
    Next

    'loop through stores 

    'It is necessary to loop from count 
    'to 1 using a decrement
    'because Stores.Count is decremented 
    'whenever(RemoveStore) is called. 
    'For Each' can't be used for this reason.

    For i As Integer = olNameSpace.Stores.Count To 1 Step -1

        'Dim oStore As Outlook.Store = olNameSpace.Session.Stores(i)
        Dim oStore As Outlook.Store = olNameSpace.Stores(i)

        'root folder for store
        Dim rootFolder As Outlook.MAPIFolder = oStore.GetRootFolder()

        'folders for store
        Dim subFolders As Outlook.Folders = rootFolder.Folders

        'loop through all folders
        For Each oFolder As Outlook.Folder In subFolders

            'get folder items
            Dim oItems As Outlook.Items = oFolder.Items

            'search through each email
            For Each email As Object In oItems

                'make sure item is a mail item, 
                'not a meeting request
                If email.MessageClass = "IPM.Note" Then
                    If TypeOf email Is Microsoft.Office.Interop.Outlook.MailItem Then
                        output += "oStore: " + oStore.DisplayName + " oFolder: " + oFolder.Name + " " + "subject: " & email.Subject & System.Environment.NewLine
                    End If
                End If
            Next
        Next

        'remove store that was added above
        If pstDictionary.ContainsValue(rootFolder.Name) Then
            olNameSpace.RemoveStore(rootFolder)
        End If

    Next

    'logoff 
    'olNameSpace.Logoff()

    olNameSpace = Nothing
    olApp = Nothing

    MessageBox.Show(output)

End Sub

Version 2 Usage:

Dim pstList As New List(Of String)
pstList.Add("C:\Temp\Outlook Files\AccountInfoBackup.pst")
pstList.Add("C:\Temp\Outlook Files\TravelBackup.pst")

getOutlookEmailInfo(pstList)

Version 3 (.pst filename specified, display name specified, .pst added programmatically):

Private Sub getOutlookEmailInfo(ByVal pstDictionary As Dictionary(Of String, String))
    Dim output As String = String.Empty
    Dim olApp As Outlook.Application = Nothing
    Dim olNameSpace As Outlook.NameSpace = Nothing

    'create new instance of Outlook.Application
    olApp = New Outlook.Application()

    'set olNameSpace to MAPI namespace
    olNameSpace = olApp.GetNamespace("MAPI")

    ' TODO: Replace the "YourValidProfile" 
    ' and "myPassword" with Missing.Value 
    ' if you want to log on with the default profile.
    ' olNameSpace.Logon("YourValidProfile", "myPassword", True, True)
    ' olNameSpace.Logon()

    For Each kvp As KeyValuePair(Of String, String) In pstDictionary
        'add pst file to default profile
        olNameSpace.AddStore(kvp.Key)

        'change DisplayName for pst
        olNameSpace.Folders.GetLast().Name = kvp.Value
    Next

    'loop through stores 

    'It is necessary to loop from count 
    'to 1 using a decrement
    'because Stores.Count is decremented 
    'whenever(RemoveStore) is called. 
    'For Each' can't be used for this reason.

    For i As Integer = olNameSpace.Stores.Count To 1 Step -1

        'Dim oStore As Outlook.Store = olNameSpace.Session.Stores(i)
        Dim oStore As Outlook.Store = olNameSpace.Stores(i)

        'root folder for store
        Dim rootFolder As Outlook.MAPIFolder = oStore.GetRootFolder()

        'folders for store
        Dim subFolders As Outlook.Folders = rootFolder.Folders

        'loop through all folders
        For Each oFolder As Outlook.Folder In subFolders

            'get folder items
            Dim oItems As Outlook.Items = oFolder.Items

            'search through each email
            For Each email As Object In oItems

                'make sure item is a mail item, 
                'not a meeting request
                If email.MessageClass = "IPM.Note" Then
                    If TypeOf email Is Microsoft.Office.Interop.Outlook.MailItem Then
                        output += "oStore: " + oStore.DisplayName + " oFolder: " + oFolder.Name + " " + "subject: " & email.Subject & System.Environment.NewLine
                    End If
                End If
            Next
        Next

        'remove store that was added above
        If pstDictionary.ContainsValue(rootFolder.Name) Then
            olNameSpace.RemoveStore(rootFolder)
        End If
    Next

    'logoff 
    'olNameSpace.Logoff()

    olNameSpace = Nothing
    olApp = Nothing

    MessageBox.Show(output)

End Sub

Version 3 Usage:

Dim pstDictionary As New Dictionary(Of String, String)
pstDictionary.Add("C:\Temp\Outlook Files\AccountInfoBackup.pst", "Temp - Account Info")
pstDictionary.Add("C:\Temp\Outlook Files\TravelBackup.pst", "Temp - Travel")

getOutlookEmailInfo(pstDictionary)

Edited 2 Years Ago by cgeier

thanks to this...

I think the code below is what i'm missing for.
Dim rootFolder As Outlook.MAPIFolder = oStore.GetRootFolder()

I really Appreciate your help... I think I can solve it now this time.

This question has already been answered. Start a new discussion instead.