none
Outlook 2007 vba code doesn't work in Outlook 2010 RRS feed

  • Question

  • This code is a simplified version of what I'm trying to do (count the emails in Inboxes in each of several main level shared folders). The code runs fine in Outlook2007 but shows the following error when run in Outlook2010:

      'Run-time error '-2147221163 (80040155)':
      'Method 'Folders' of object'_NameSpace' failed

    Private Sub ReadLevelOneFolders()
      Dim olApp As Outlook.application
      Dim olNS As Outlook.NameSpace
      Dim olFolder As Outlook.Folder
     
      Set olApp = CreateObject("Outlook.Application.14")
      Set olNS = olApp.GetNamespace("MAPI")
     
      For Each olFolder In olNS.folders  '<---------- Fails here
        MsgBox olFolder.Name
      Next
       
    End Sub

    My company is going to all Office 2010 soon and I need to figure this out. Any suggestions would be greatly appreciated. I'm an intermediate level programmer in Outlook VBA and new to Office 2010.

    Thanks

    Friday, September 16, 2011 9:11 PM

Answers

  • If you are running the code in Outlook self, no need to create the Outlook Application Object. You might try this code, and see if that helps:

    Private Sub ReadLevelOneFolders()
      
      Dim olNS As Outlook.NameSpace
      Dim olFolder As Outlook.Folder
     
      Set olNS = Application.GetNamespace("MAPI")
     
      For Each olFolder In olNS.folders  
        MsgBox olFolder.Name
      Next
       
    End Sub
    


    I have tested this in Excel 2007. I am sure it works in Excel 2010.


    Let me know your results.

     


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Monday, January 30, 2012 11:59 PM
    Moderator

All replies

  • I'm an Excel guy, but I think Outlook 2010 doesn't like MAPI (Messaging API) any more - your code might need to be transitioned to Outlook PIA (Primary Interop Assembly).

    Until some Outlook MVP happens upon your post, read more at

    http://msdn.microsoft.com/en-us/library/bb652780.aspx


    HTH, Bernie
    Monday, September 19, 2011 1:44 PM
  • Use the Object Browser (F2) to see if Folders are still in that class in the Outlook library
    Monday, October 17, 2011 5:20 PM
  • If you are running the code in Outlook self, no need to create the Outlook Application Object. You might try this code, and see if that helps:

    Private Sub ReadLevelOneFolders()
      
      Dim olNS As Outlook.NameSpace
      Dim olFolder As Outlook.Folder
     
      Set olNS = Application.GetNamespace("MAPI")
     
      For Each olFolder In olNS.folders  
        MsgBox olFolder.Name
      Next
       
    End Sub
    


    I have tested this in Excel 2007. I am sure it works in Excel 2010.


    Let me know your results.

     


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Monday, January 30, 2012 11:59 PM
    Moderator