Consulting

Results 1 to 2 of 2

Thread: Reading Undelivered mails from outlook.code problem

  1. #1

    Exclamation Reading Undelivered mails from outlook.code problem

    have to read undelivered e-mail messages from outlook inbox and store in a text file. i am total newbie in VBA. so plz help.
    Here is my code.
    Public Sub ProcessInbox()
    Dim oOutlook As Outlook.Application
    Dim oNs As Outlook.NameSpace
    Dim oFldr As Outlook.MAPIFolder
    Dim iMsgCount As Integer
    Dim oMessage As Outlook.ReportItem
    Dim iCtr As Long, iAttachCnt As Long
    
    Set oOutlook = New Outlook.Application
    Set oNs = oOutlook.GetNamespace("MAPI")
    Set oFldr = oNs.GetDefaultFolder(olFolderInbox)
    For Each oMessage In oFldr.Items
    
            With oMessage
            'basic info about message
            If (oMessage.Subject) = "Undeliverable" Then
                Debug.Print .To
                Debug.Print .CC
                Debug.Print .Subject
                Debug.Print .Body
                iMsgCount = iMsgCount + 1
                'save message as text file
                .SaveAs "C:\message" & iMsgCount & ".txt", olTXT
    end if
    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
    At this line
    For Each oMessage In oFldr.Items
    i am getting error run-time error 13 type mismatch

  2. #2
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    [VBA]Option Explicit

    Public Sub ProcessInbox()
    Const ForWriting As Long = 2
    Const TristateUseDefault As Long = -2
    Dim oOutlook As Outlook.Application
    Dim oNs As Outlook.Namespace
    Dim oFldr As Outlook.MAPIFolder
    Dim iMsgCount As Integer
    Dim oMessage As Outlook.ReportItem
    Dim iCtr As Long, iAttachCnt As Long
    Dim fso As Object
    Dim ts As Object
    Set oOutlook = New Outlook.Application
    Set oNs = oOutlook.GetNamespace("MAPI")
    Set oFldr = oNs.GetDefaultFolder(olFolderInbox)
    Set fso = CreateObject("Scripting.Runtime")
    Set ts = fso.OpenTextFile("C:\Output.txt", ForWriting, True, TristateUseDefault)
    For Each oMessage In oFldr.Items

    With oMessage
    'basic info about message
    If (oMessage.Subject) = "Undeliverable" Then

    ts.WriteLine .To
    ts.WriteLine .CC
    ts.WriteLine .Subject
    ts.WriteLine .Body & vbNewLine & vbNewLine
    iMsgCount = iMsgCount + 1
    'save message as text file
    .SaveAs "C:\message" & iMsgCount & ".txt", olTXT
    End If
    End With
    DoEvents

    Next oMessage

    ts.Close
    Set oAttachment = Nothing
    Set oAttachments = Nothing
    Set oMessage = Nothing
    Set oFldr = Nothing
    Set oNs = Nothing
    Set oOutlook = Nothing
    Set ts = Nothing
    Set fso = Nothing
    End Sub[/VBA]
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

Posting Permissions

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