Sunday, February 22, 2009

Access Each Message in Outlook Inbox - visual basic

hi i have found this function that i liked to share with you,this function will help you to access each message in outlook inbox.

This routine gets access the outlook inbox folder on the local machine, loops through each message, reads basic info about the  message, and saves the message and all of it's attachments. It is designed to give you a basic idea of what you can do with the outlook object model, especially the contents of folders.

Note: A reference to Microsoft Outlook Type Library is required.

you can download this pdf file that you can copy the code and use it..



Public Sub ProcessInbox()
    
' define the needed variable
    Dim oOutlook As Outlook.Application
    Dim oNs As Outlook.NameSpace
    Dim oFldr As Outlook.MAPIFolder
    Dim oAttachments As Outlook.Attachments
    Dim oAttachment As Outlook.Attachment
    Dim iMsgCount As Integer

    Dim oMessage As Outlook.MailItem

    Dim iCtr As Long, iAttachCnt As Long

    Dim sFileNames As String
    Dim aFileNames() As String


    ' get reference to inbox
    Set oOutlook = New Outlook.Application
    Set oNs = oOutlook.GetNamespace("MAPI")
    Set oFldr = oNs.GetDefaultFolder(olFolderInbox)
    Debug.Print "Total Items: "; oFldr.Items.Count
    Debug.Print "Total Unread items = " & oFldr.UnReadItemCount

   
' this loop will access each message and get there basic info
    For Each oMessage In oFldr.Items
        With oMessage
        ' basic info about message
            Debug.Print .To
            Debug.Print .CC
            Debug.Print .Subject
            Debug.Print .Body

            If .UnRead Then
                Debug.Print "Message has not been read"
            Else
                Debug.Print "Message has been read"
            End If
            iMsgCount = iMsgCount + 1

            ' Save message as text file
            .SaveAs "C:\message" & iMsgCount & ".txt", olTXT

            ' Reference and save all attachments
            With oMessage.Attachments
                iAttachCnt = .Count
                If iAttachCnt > 0 Then
                    For iCtr = 1 To iAttachCnt
                        .Item(iCtr).SaveAsFile "C:\" & .Item(iCtr).FileName
                    Next ' iCtr
                End If
            End With
        End With

        DoEvents
    Next ' oMessage

        Set oAttachment = Nothing
        Set oAttachments = Nothing
        Set oMessage = Nothing
        Set oFldr = Nothing
        Set oNs = Nothing
        Set oOutlook = Nothing
End Sub


you can download this pdf file that you can copy the code and use it..




2 comments:

Alex said...

Usually my contacts store on my computer in genelar folder for MS Outlook,but one day I copied it on flash,and data was damaged.But accidentally on next day in inet I saw-viewing .pst files.Tool is free as far as I remember,moreover utility can convert your file to a file with *.pst extension.

Alexis said...

My nephew had an unpleasant situation.All his mails were corrupted and he used-can read contacts in outlook.Tool decided this problem.Besides that this tool helped me after that.It recovered all mails for free and program showed me how read contacts from pst, decrypt this file and extract all contacts to *.vcf files.