none
Delete olInbox type custom folder RRS feed

  • Question

  • I have created a custom folder of type Inbox :

    folder = rootFolder.Folders.Add(CUSTOM_NAME, Outlook.OlDefaultFolders.olFolderInbox) asOutlook.Folder

    Now I want to delete it :

    rootFolder.Folders[CUSTOM_NAME].Delete();

    It is not throwing any exception nor it is able to delete it.

    Need help to forcefully delete it programmatically.

    Friday, March 7, 2014 3:16 PM

Answers

  • Hello Haris,

    .Net and VBA macros are different.

    First of all, please make sure that you release all underlying COM objects properly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about this in the Systematically Releasing Objects article in MSDN.

    Then I'd recommend breaking the chain of property and method call into separate lines. Thus, you will be able to find a problematic a property or method call.

    Finally, did you try to debug the code?

    • Marked as answer by Haris Hassan Monday, March 10, 2014 6:11 AM
    Friday, March 7, 2014 4:50 PM

All replies

  • I had no problem with the following script (I ran it from OutlookSpy - click Script, paste the script, click Run)

    set rootFolder = Application.Session.GetDefaultFolder(olFolderInbox).Parent
    set folder = rootFolder.Folders.Add("CUSTOM_NAME", olFolderInbox)
    MsgBox "created"
    rootFolder.Folders("CUSTOM_NAME").Delete()
    MsgBox "deleted"


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!


    Friday, March 7, 2014 4:15 PM
  • Hello Haris,

    .Net and VBA macros are different.

    First of all, please make sure that you release all underlying COM objects properly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about this in the Systematically Releasing Objects article in MSDN.

    Then I'd recommend breaking the chain of property and method call into separate lines. Thus, you will be able to find a problematic a property or method call.

    Finally, did you try to debug the code?

    • Marked as answer by Haris Hassan Monday, March 10, 2014 6:11 AM
    Friday, March 7, 2014 4:50 PM
  • I can reproduce the problem in the cached Exchange 2013 mode if the folder is created and deleted fast enough - I have seen a similar problem with contact items: you create a contact, save it, some time later the item is uploaded to the server, which (for whatever reason) decides that it needs to "fix" a few properties, then the change is pushed to the cached store. To a user/developer it looks like there was an additional modification of the contact.

    I am guessing in this case the folder is uploaded to the server, then deleted locally. When the server pushes the changes back, the folder is recreated. If you leave the first message box in the script above up for a minute or two (to allow for the changes from the server to come back), then dismiss the dialog box and delete the folder, it will be properly deleted.

    Do you see this behavior?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, March 7, 2014 4:58 PM