Microsoft KB Archive/313791

From BetaArchive Wiki

Article ID: 313791

Article Last Modified on 6/29/2007



APPLIES TO

  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Outlook 2002 Standard Edition
  • Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2



This article was previously published under Q313791

SUMMARY

This article describes how to use Microsoft Outlook 10.0 Object Library to delete Outlook folders in Visual Basic .NET.

back to the top

Create Sample to Delete an Outlook Folder

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual Basic Projects under Project Types, and then click Console Application under Templates. By default, Module1.vb is created.
  4. Add a reference to the Microsoft Outlook 10.0 Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab.
    3. Click Microsoft Outlook 10.0 Object Library, and then click Select
    4. Click OK. If you are prompted to generate wrappers for the library that you selected, click Yes.
  5. In the Code window, replace the default code with the following code:

    Imports System.Reflection
    
    Module Module1
    
        Sub Main()
            ' TODO: Replace with the folder name that you want to delete.
            Dim sName as string = "MyFolder"
    
            ' Create Outlook application.
            Dim oApp As Outlook.Application = New Outlook.Application()
    
            ' Get Mapi NameSpace and Logon.
            Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
            oNS.Logon("YourValidProfile", Missing.Value, False, True) ' TODO:
    
            ' TODO: My Folder Structure
            ' Inbox
            '    MyFolder
            ' You will delete the MyFolder folder.
    
            ' Get the folder to be deleted.
            Dim oInbox As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
            Dim oFolders As Outlook.Folders = oInbox.Folders
            Dim oFolder As Outlook.MAPIFolder = oFolders.Item(sName)
    
            ' Delete the target folder.
            oFolder.Delete()
    
            ' Log off.
            oNS.Logoff()
    
            ' Clean up.
            oApp = Nothing
            oNS = Nothing
            oFolders = Nothing
            oFolder = Nothing
        End Sub
    
    End Module
                        
  6. Modify the code where you see the TODO comments.
  7. Press F5 to build and to run the application.
  8. Verify that the MyFolder folder is deleted from the Inbox.

back to the top

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web site:

Microsoft Office Development with Visual Studio
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx


back to the top


Additional query words: OOM

Keywords: kbhowtomaster KB313791