1

I found this code that is able to extract Emails from a specified folder in Outlook into Excel. The issue is that sometimes it gives me the run time error -2147352567 (80020009)' when setting the subfolder item. For example, today it gave me the error but after 7 tries in one hour (just for testing), it worked. This behavior occurs randomly. Some days it works the first time then again it throws an error the next day and I have to keep running it till it works such as Today.

Sub EmailStatsV3()
    Dim Item As Object
    Dim varOutput() As Variant
    Dim lngcount As Long
    Dim xlApp As Excel.Application
    Dim xlSht As Excel.Worksheet
    Dim ShareInbox As Outlook.MAPIFolder
    Dim olNs As Outlook.NameSpace
    Dim olRecip As Outlook.Recipient
    Dim SubFolder As Object

    Set olNs = Application.GetNamespace("MAPI")
    Set olRecip = olNs.CreateRecipient("[email protected]") '// Owner's Name or email address
    Set ShareInbox = olNs.GetSharedDefaultFolder(olRecip, olFolderInbox)
    Set SubFolder = ShareInbox.Folders("name").Folders("outages") 'Change this line to specify folder

    ReDim varOutput(1 To SubFolder.Items.Count, 1 To 11)

    For Each Item In SubFolder.Items
        If TypeName(Item) = "MailItem" Then
            lngcount = lngcount + 1
            varOutput(lngcount, 1) = Item.SenderEmailAddress 'Sender or SenderName
            varOutput(lngcount, 2) = Item.ReceivedTime 'stats on when received
            varOutput(lngcount, 3) = Item.ConversationTopic 'Conversation subject
            varOutput(lngcount, 4) = Item.Subject 'to split out prefix
            varOutput(lngcount, 5) = Item.Categories 'to split out category
            varOutput(lngcount, 6) = Item.Sender
            varOutput(lngcount, 7) = Item.SenderName
            varOutput(lngcount, 8) = Item.To
            varOutput(lngcount, 9) = Item.CC
            varOutput(lngcount, 10) = SubFolder.Name
            varOutput(lngcount, 11) = Item.Body



        End If
    Next

    'Creates a blank workbook in excel
    Set xlApp = New Excel.Application
    Set xlSht = xlApp.Workbooks.Add.Sheets(1)

    xlSht.Range("A1").Resize(UBound(varOutput, 1), _
                             UBound(varOutput, 2)).Value = varOutput
    xlApp.Visible = True

    Set olNs = Nothing
    Set olRecip = Nothing '// Owner's Name or email address
    Set ShareInbox = Nothing
    Set SubFolder = Nothing   'Change this line to specify folder


End Sub

Thank you,

Edit: This runtime error only occurs for me when accessing subfolders in shared default folders . A workaround was that I set the folder to current folder and it did the trick. For others facing similar issues, this is what I changed. You must remember to select the folder first.

 Set SubFolder = Application.ActiveExplorer.CurrentFolder

1 Answer 1

3

Looks like you are running the code on the server-side, at least the error code says about that... if not, make sure the network connection is stable and you don't have any connection-related problems.

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

Read more about that in the Considerations for server-side Automation of Office article.

Sign up to request clarification or add additional context in comments.

5 Comments

I see, I believe that is the case and due to security reasons (IT), I can't use an alternative approach. Thank you
A low-level API (Extended MAPI) is a possible alternative.
Or, if you deal with Exchange only, you may consider using EWS.
I figured a work around by just setting the folder to the current selected folder. This works and doesn't give me a run time error. In the future, I might try those alternatives, thanks. For now , I just have to remember to select the folder first before running the macro. Thanks
Glad to know that you've found a solution! Good luck with your project!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.