none
Microsoft.Office.Inerop.Outlook._Folders.Add throws Cannot Create Folder Exception RRS feed

  • Question

  • I am writing a Solutions Module add-in for Outlook 2010 using Visual Studio 2010.  I am able to establish the root for the Solution, but when I attempt to add a folder to the root, Microsoft.Outlook._Folders.Add throws an exception with the message: "Cannot create the folder", HResult: -2147352567.

    Here is a code sample:

    try {
    Outlook.Folder lfRoot;
    Outlook.Folder lfRootSF = Application.Session.DefaultStore.GetRootFolder() as Outlook.Folder
    lfRoot = lfRootSF.Folders.Add(DWSGlobal.KsDWSRootName, Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
    Outlook.Explorer leExplorer = Application.ActiveExplorer();
    solutionsModule = leExplorer.NavigationPan.Modules.getNavigationModule(Outlook.OLNavigationModuleType.olModuleSolutions) as Outlook.SolutionsModule;
    solutionsModule.AddSolution(lsRoot, Outlook.OLSoutionScope.olHideInDefaultModules);
    if (!solutionsModule.Visible) {
        solutionsModule.Visible = true;
    }
    Outlook.Folder lfCalendar = lsRoot.Folders.Add(DWSGlobal.KsDWSCalendar, Outlook.OlDefaultFolders.olFolderCalendar) as Outlook.Folder;
    } catch (Exception ex) {
    Debug.Write(ex.Message);
    }

    Exception Details:

    System.Runtime.InteropServices.COMException: {"Cannot create the folder."}

    InnerException: null

    Source: Microsoft Outlook

    StackTrace: at Microsoft.Office.Interop.Outlook._Folders.Add(String Name, Object Type)

    Any help anyone can offer would be greatly appreciated!

    Friday, March 30, 2012 6:42 AM

Answers

  • I don't know. After correcting the various typos and converting the code to VBA I ran it in the Outlook VBA project in an Outlook 2010 setup and it ran correctly, without errors, and created both a solutions module root folder as well as a calendar subfolder under the root.
     
    Are you sure that when you're getting the DWSGlobal.KsDWSRootName and DWSGlobal.KsDWSCalendar strings that you're getting valid strings?
     
    See if this code works for you in the Outlook VBA project. If it doesn't then we're looking at either store corruption, or some sort of permissions issues, or conflicts with some other solution module perhaps.
     

    Sub solutionmodule()
        On Error GoTo ErrHandler
       
        Dim lfRoot As Outlook.Folder
        Dim lfRootSF As Outlook.Folder
        Set lfRootSF = Application.Session.DefaultStore.GetRootFolder
        Set lfRoot = lfRootSF.folders.Add("SolutionModuleRoot", olFolderInbox)
       
        Dim leExplorer As Outlook.Explorer
        Set leExplorer = Application.ActiveExplorer
        Dim solModule As Outlook.SolutionsModule
        Set solModule = leExplorer.NavigationPane.Modules.GetNavigationModule(olModuleSolutions)
        solModule.AddSolution lfRoot, olHideInDefaultModules
       
        If solModule.Visible = False Then
            solModule.Visible = True
        End If
       
        Dim lfCalendar As Outlook.Folder
        Set lfCalendar = lfRoot.folders.Add("SolutionModuleCalendar", olFolderCalendar)
     
        Exit Sub
       
    ErrHandler:
        Debug.Print Err.Description, Err.Source
       
    End Sub
     

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "MG257" <=?utf-8?B?TUcyNTc=?=> wrote in message news:8ba4c9b4-30af-44c4-863f-2db5afe4ac33...

    No, Sorry it is a typo, it should be:

    solutionsModule.AddSoltution(lfRoot, Outlook.OlSolutionScop.olHideInDefaultModules);

    Thank you for any help you can offer.


    Mark J. Gergen


    Ken Slovak MVP - Outlook
    Friday, March 30, 2012 3:13 PM

All replies

  • Is that a typo, where you add IsRoot as the solution root? Is that folder declared and instantiated elsewhere?

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "MG257" <=?utf-8?B?TUcyNTc=?=> wrote in message news:b7933eb4-6436-44f0-b1aa-2805aaab2560...

    I am writing a Solutions Module add-in for Outlook 2010 using Visual Studio 2010.  I am able to establish the root for the Solution, but when I attempt to add a folder to the root, Microsoft.Outlook._Folders.Add throws an exception with the message: "Cannot create the folder", HResult: -2147352567.

    Here is a code sample:

    try {
    Outlook.Folder lfRoot;
    Outlook.Folder lfRootSF = Application.Session.DefaultStore.GetRootFolder() as Outlook.Folder
    lfRoot = lfRootSF.Folders.Add(DWSGlobal.KsDWSRootName, Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
    Outlook.Explorer leExplorer = Application.ActiveExplorer();
    solutionsModule = leExplorer.NavigationPan.Modules.getNavigationModule(Outlook.OLNavigationModuleType.olModuleSolutions) as Outlook.SolutionsModule;
    solutionsModule.AddSolution(lsRoot, Outlook.OLSoutionScope.olHideInDefaultModules);
    if (!solutionsModule.Visible) {
        solutionsModule.Visible = true;
    }
    Outlook.Folder lfCalendar = lsRoot.Folders.Add(DWSGlobal.KsDWSCalendar, Outlook.OlDefaultFolders.olFolderCalendar) as Outlook.Folder;
    } catch (Exception ex) {
    Debug.Write(ex.Message);
    }

    Exception Details:

    System.Runtime.InteropServices.COMException: {"Cannot create the folder."}

    InnerException: null

    Source: Microsoft Outlook

    StackTrace: at Microsoft.Office.Interop.Outlook._Folders.Add(String Name, Object Type)

    Any help anyone can offer would be greatly appreciated!


    Ken Slovak MVP - Outlook
    Friday, March 30, 2012 1:07 PM
  • No, Sorry it is a typo, it should be:

    solutionsModule.AddSoltution(lfRoot, Outlook.OlSolutionScop.olHideInDefaultModules);

    Thank you for any help you can offer.


    Mark J. Gergen

    Friday, March 30, 2012 2:48 PM
  • I don't know. After correcting the various typos and converting the code to VBA I ran it in the Outlook VBA project in an Outlook 2010 setup and it ran correctly, without errors, and created both a solutions module root folder as well as a calendar subfolder under the root.
     
    Are you sure that when you're getting the DWSGlobal.KsDWSRootName and DWSGlobal.KsDWSCalendar strings that you're getting valid strings?
     
    See if this code works for you in the Outlook VBA project. If it doesn't then we're looking at either store corruption, or some sort of permissions issues, or conflicts with some other solution module perhaps.
     

    Sub solutionmodule()
        On Error GoTo ErrHandler
       
        Dim lfRoot As Outlook.Folder
        Dim lfRootSF As Outlook.Folder
        Set lfRootSF = Application.Session.DefaultStore.GetRootFolder
        Set lfRoot = lfRootSF.folders.Add("SolutionModuleRoot", olFolderInbox)
       
        Dim leExplorer As Outlook.Explorer
        Set leExplorer = Application.ActiveExplorer
        Dim solModule As Outlook.SolutionsModule
        Set solModule = leExplorer.NavigationPane.Modules.GetNavigationModule(olModuleSolutions)
        solModule.AddSolution lfRoot, olHideInDefaultModules
       
        If solModule.Visible = False Then
            solModule.Visible = True
        End If
       
        Dim lfCalendar As Outlook.Folder
        Set lfCalendar = lfRoot.folders.Add("SolutionModuleCalendar", olFolderCalendar)
     
        Exit Sub
       
    ErrHandler:
        Debug.Print Err.Description, Err.Source
       
    End Sub
     

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "MG257" <=?utf-8?B?TUcyNTc=?=> wrote in message news:8ba4c9b4-30af-44c4-863f-2db5afe4ac33...

    No, Sorry it is a typo, it should be:

    solutionsModule.AddSoltution(lfRoot, Outlook.OlSolutionScop.olHideInDefaultModules);

    Thank you for any help you can offer.


    Mark J. Gergen


    Ken Slovak MVP - Outlook
    Friday, March 30, 2012 3:13 PM
  • Not that it helps you, but I have been having a similar issue, it is intermittant sometimes it will load 10 times with no problem, others every time I load it errors. I coded round it using a loop to attempt 10 times breaking out if it created it successfully as I never got to the root cause.

    Wednesday, April 4, 2012 9:31 AM
  • Is it possible that you have Exchange and have more objects referenced in code than the server settings are allowing?
     
    By default you have a maximum of 255 RPC channels available for connection to Exchange from each Outlook client. If you exceed that number (set in the server registry), you get out of memory errors and can't open or create Outlook items or objects.
     
    Especially in loops you need to release each object when done with it (call Marshal.ReleaseComObject() and set it to null). That applies to any object property also, such as NameSpace.GetDefaultFolder(olFolderInbox).Items. That expression creates not only an Items collection object but an invisible Folder object. The lesson there is to explicitly declare and release each object, don't use compound dot operators, and declare objects outside of loops so you only create one instance of an object.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Vis Xero" <=?utf-8?B?VmlzIFhlcm8=?=> wrote in message news:e52b39f5-aa80-4972-a917-aa0bc2a4a6ba...

    Not that it helps you, but I have been having a similar issue, it is intermittant sometimes it will load 10 times with no problem, others every time I load it errors. I coded round it using a loop to attempt 10 times breaking out if it created it successfully as I never got to the root cause.


    Ken Slovak MVP - Outlook
    Tuesday, April 10, 2012 6:47 PM
  • Is it possible that you have Exchange and have more objects referenced in code than the server settings are allowing?

    By default you have a maximum of 255 RPC channels available for connection to Exchange from each Outlook client. If you exceed that number (set in the server registry), you get out of memory errors and can't open or create Outlook items or objects.
    Especially in loops you need to release each object when done with it (call Marshal.ReleaseComObject() and set it to null). That applies to any object property also, such as NameSpace.GetDefaultFolder(olFolderInbox).Items. That expression creates not only an Items collection object but an invisible Folder object. The lesson there is to explicitly declare and release each object, don't use compound dot operators, and declare objects outside of loops so you only create one instance of an object.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Vis Xero" <=?utf-8?B?VmlzIFhlcm8=?=> wrote in message news:e52b39f5-aa80-4972-a917-aa0bc2a4a6ba...

    Not that it helps you, but I have been having a similar issue, it is intermittant sometimes it will load 10 times with no problem, others every time I load it errors. I coded round it using a loop to attempt 10 times breaking out if it created it successfully as I never got to the root cause.


    Ken Slovak MVP - Outlook

    It is definately possible I am leaving objects open, the problme I am having is pinpointing where I have done this.  I chose to use FinalReleaseComObject instead of ReleaseComObject and have put this in the Finally of my try catch, is this a mistake, or is it just a case that my open object is else where?  I haven't then been nullifying my object once I have ComReleased it so I will try that also. is any item that is esentially Outlook. an object that could cause me this issue?
    Wednesday, April 11, 2012 9:59 AM