none
VB.NET Outlook Mail Delete RRS feed

  • Question

  • Okay, this is what I'm trying to do, from an archived PST file, copy the folder structure and not the contents through VB.NET.  So far I have the user select the PST they want to make a copy of the file structure and I recursively copy the folder from the selected PST to a temporary PST, however this command copies all the contents, so right now I want to delete the mail contents but I don't know... it doesn't seem to work.  I have the copying portion working but the deletion isn't.  Much help appreciated. Thanks.

    Here's the code:

    Imports Microsoft.Office.Interop.Outlook
    
    Public Class Form1
      Const PST_PATH As String = "D:\temp.pst"
      Dim toAPP As New Microsoft.Office.Interop.Outlook.ApplicationClass()
      Dim toNS As Microsoft.Office.Interop.Outlook.NameSpace = toAPP.GetNamespace("MAPI")
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim objFolder As Microsoft.Office.Interop.Outlook.MAPIFolder = Nothing
        Dim fromAPP As New Microsoft.Office.Interop.Outlook.ApplicationClass()
        Dim fromNS As Microsoft.Office.Interop.Outlook.NameSpace = fromAPP.GetNamespace("MAPI")
    
        objFolder = fromNS.PickFolder
    
        If Not objFolder Is Nothing Then
          Call ProcessFolder(objFolder)
        Else
          MsgBox("You did not pick a folder.")
        End If
    
      End Sub
    
      Sub ProcessFolder(ByVal StartFolder)
        Dim objFolder As Microsoft.Office.Interop.Outlook.MAPIFolder = Nothing
        Dim inbox
        Dim path As String
    
        On Error Resume Next
    
        toNS.AddStore(PST_PATH)
    
        For Each objFolder In StartFolder.Folders
          inbox = toNS.Application.Session.Folders.GetLast()
    
          objFolder.CopyTo(inbox)
    
          Dim oDeletedItems As Microsoft.Office.Interop.Outlook.Folder
          Dim oItems As Microsoft.Office.Interop.Outlook.Items
          Dim i As Long
    
          oDeletedItems = toNS.Application.Session.Folders.GetLast()
          oItems = oDeletedItems.Items
          For i = oItems.Count To 1 Step -1
            oItems.Item(i).Delete()
          Next
    
          Call ProcessFolder(objFolder)
        Next
    
        objFolder = Nothing
      End Sub
    End Class
    
    
    

    Tuesday, August 24, 2010 2:05 PM

Answers

  • If "inbox" is the folder where you're doing the copying, why not just set oItems to inbox.Items and then delete?
     
    I don't see why though you just don't use the Folders.Add() method to add each folder and subfolder where you want it, of the correct type and not copy folders. That would not only eliminate having to delete all those items in each folder but also would be a heck of a lot faster without all the copying and subsequent deletions.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "HTRAN" <=?utf-8?B?SFRSQU4=?=> wrote in message news:8146f584-0031-4699-b38e-11e58adf0611...

    Okay, this is what I'm trying to do, from an archived PST file, copy the folder structure and not the contents through VB.NET.  So far I have the user select the PST they want to make a copy of the file structure and I recursively copy the folder from the selected PST to a temporary PST, however this command copies all the contents, so right now I want to delete the mail contents but I don't know... it doesn't seem to work.  I have the copying portion working but the deletion isn't.  Much help appreciated. Thanks.

    Here's the code:

    Imports Microsoft.Office.Interop.Outlook
    
    Public Class Form1
     Const PST_PATH As String = "D:\temp.pst"
     Dim toAPP As New Microsoft.Office.Interop.Outlook.ApplicationClass()
     Dim toNS As Microsoft.Office.Interop.Outlook.NameSpace = toAPP.GetNamespace("MAPI")
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
      Dim objFolder As Microsoft.Office.Interop.Outlook.MAPIFolder = Nothing
      Dim fromAPP As New Microsoft.Office.Interop.Outlook.ApplicationClass()
      Dim fromNS As Microsoft.Office.Interop.Outlook.NameSpace = fromAPP.GetNamespace("MAPI")
    
      objFolder = fromNS.PickFolder
    
      If Not objFolder Is Nothing Then
       Call ProcessFolder(objFolder)
      Else
       MsgBox("You did not pick a folder.")
      End If
    
     End Sub
    
     Sub ProcessFolder(ByVal StartFolder)
      Dim objFolder As Microsoft.Office.Interop.Outlook.MAPIFolder = Nothing
      Dim inbox
      Dim path As String
    
      On Error Resume Next
    
      toNS.AddStore(PST_PATH)
    
      For Each objFolder In StartFolder.Folders
       inbox = toNS.Application.Session.Folders.GetLast()
    
       objFolder.CopyTo(inbox)
    
       Dim oDeletedItems As Microsoft.Office.Interop.Outlook.Folder
       Dim oItems As Microsoft.Office.Interop.Outlook.Items
       Dim i As Long
    
       oDeletedItems = toNS.Application.Session.Folders.GetLast()
       oItems = oDeletedItems.Items
       For i = oItems.Count To 1 Step -1
        oItems.Item(i).Delete()
       Next
    
       Call ProcessFolder(objFolder)
      Next
    
      objFolder = Nothing
     End Sub
    End Class
    
    
    


    Ken Slovak MVP - Outlook
    • Proposed as answer by Sue MosherModerator Sunday, August 29, 2010 12:23 AM
    • Marked as answer by HTRAN Monday, August 30, 2010 1:01 PM
    Tuesday, August 24, 2010 3:49 PM

All replies

  • If "inbox" is the folder where you're doing the copying, why not just set oItems to inbox.Items and then delete?
     
    I don't see why though you just don't use the Folders.Add() method to add each folder and subfolder where you want it, of the correct type and not copy folders. That would not only eliminate having to delete all those items in each folder but also would be a heck of a lot faster without all the copying and subsequent deletions.

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "HTRAN" <=?utf-8?B?SFRSQU4=?=> wrote in message news:8146f584-0031-4699-b38e-11e58adf0611...

    Okay, this is what I'm trying to do, from an archived PST file, copy the folder structure and not the contents through VB.NET.  So far I have the user select the PST they want to make a copy of the file structure and I recursively copy the folder from the selected PST to a temporary PST, however this command copies all the contents, so right now I want to delete the mail contents but I don't know... it doesn't seem to work.  I have the copying portion working but the deletion isn't.  Much help appreciated. Thanks.

    Here's the code:

    Imports Microsoft.Office.Interop.Outlook
    
    Public Class Form1
     Const PST_PATH As String = "D:\temp.pst"
     Dim toAPP As New Microsoft.Office.Interop.Outlook.ApplicationClass()
     Dim toNS As Microsoft.Office.Interop.Outlook.NameSpace = toAPP.GetNamespace("MAPI")
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
      Dim objFolder As Microsoft.Office.Interop.Outlook.MAPIFolder = Nothing
      Dim fromAPP As New Microsoft.Office.Interop.Outlook.ApplicationClass()
      Dim fromNS As Microsoft.Office.Interop.Outlook.NameSpace = fromAPP.GetNamespace("MAPI")
    
      objFolder = fromNS.PickFolder
    
      If Not objFolder Is Nothing Then
       Call ProcessFolder(objFolder)
      Else
       MsgBox("You did not pick a folder.")
      End If
    
     End Sub
    
     Sub ProcessFolder(ByVal StartFolder)
      Dim objFolder As Microsoft.Office.Interop.Outlook.MAPIFolder = Nothing
      Dim inbox
      Dim path As String
    
      On Error Resume Next
    
      toNS.AddStore(PST_PATH)
    
      For Each objFolder In StartFolder.Folders
       inbox = toNS.Application.Session.Folders.GetLast()
    
       objFolder.CopyTo(inbox)
    
       Dim oDeletedItems As Microsoft.Office.Interop.Outlook.Folder
       Dim oItems As Microsoft.Office.Interop.Outlook.Items
       Dim i As Long
    
       oDeletedItems = toNS.Application.Session.Folders.GetLast()
       oItems = oDeletedItems.Items
       For i = oItems.Count To 1 Step -1
        oItems.Item(i).Delete()
       Next
    
       Call ProcessFolder(objFolder)
      Next
    
      objFolder = Nothing
     End Sub
    End Class
    
    
    


    Ken Slovak MVP - Outlook
    • Proposed as answer by Sue MosherModerator Sunday, August 29, 2010 12:23 AM
    • Marked as answer by HTRAN Monday, August 30, 2010 1:01 PM
    Tuesday, August 24, 2010 3:49 PM
  • I initially tried the Folder.Add using traversing through the FolderPath but the folders get created in the following format:

    "\\PSTName\Folder1\SubFolder1"

    "\\PSTName\Folder1\SubFolder1\SubSubFolder1"

    "\\PSTName\Folder1\SubFolder2"

    etc. as individual folders instead of actual folders with respect to their placement.  Also, I tried referencing "inbox" for the delete but the email messages still exist.

    Tuesday, August 24, 2010 6:07 PM
  • You would need to parse the folder path strings, by splitting on the folder separator character to get the required hierarchy.

    If you delete an item doesn't it get deleted and go to Deleted Items? It should not stay in the parent folder obviously after being deleted. Do you get any exceptions? What if you run in debug mode and step the code, do you see any exceptions, are all the objects instantiated and not null?


    Ken Slovak MVP - Outlook
    Tuesday, August 24, 2010 7:02 PM