0

I have this code from How can I create an Outlook PST file using .Net? but this simply exports no data inside. Anyone can assist me?

Const BACKUP_PST_PATH As String = "C:\backup.pst"    

Dim oFolder As Outlook.MAPIFolder = Nothing
Dim oMailbox As Outlook.MAPIFolder = Nothing

Dim app As New Outlook.Application()
Dim ns As Outlook.NameSpace = app.GetNamespace("MAPI")
Try
    //if the file doesn not exist, outlook will create it
    ns.AddStore(BACKUP_PST_PATH)
    oFolder = ns.Session.Folders.GetLast()
    oMailbox = ns.PickFolder()

    For Each f As Outlook.Folder In oMailbox.Folders
        If f.DefaultItemType <> Microsoft.Office.Interop.Outlook.OlItemType.olMailItem And f.FolderPath <> oFolder.FolderPath Then
            f.CopyTo(oFolder )
        End If
    Next

    ns.RemoveStore(oFolder)

Catch ex As Exception
    ns.RemoveStore(oFolder)
    IO.File.Delete(BACKUP_PST_PATH)
    Throw ex
End Try
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Welcome to SO. Have you stepped through the code to make sure it's behaving as you expect? One possible pain point is the filter that excludes all Mail Items. You really should step through the code. – InteXX Aug 01 '20 at 16:00
  • Why have you written (copied) VB code? The InterOp is slow because it is so flexible. It is only worth using if you are going to do lots of work in VB. I would code this in VBA. – Tony Dallimore Aug 01 '20 at 16:27
  • Are you planning to use this routine more than once? It creates "C:\backup.pst" or links to an existing "C:\backup.pst". It copies the subfolders (except MailItem folders) of a selected folder across to "C:\backup.pst" and then unlinks "C:\backup.pst". What happens when you use it a second time? I can find no documentation that tells me what happens if I copy folder "X" when folder "X" already exists in the destination store. I would hope that it creates folder "X (2)" or similar. I would experiment careful with all these routine before trying to a create an permanent routine using them. – Tony Dallimore Aug 01 '20 at 16:36
  • My comment about VB versus VBA needs expanding. VBA compiles to an interpreted language. VB compiles to machine code at runtime and uses whatever hardware is currently available. I once helped a client with a task they were attempting in VBA. Extrapolating from a small part of the task suggested the complete task would take 42 days. I recoded in VB and the complete task took 50 minutes. The InterOp will allow you to perform tasks within any version of any Office product. Very convenient if your clients have different versions of Office but not fast. – Tony Dallimore Aug 01 '20 at 16:46
  • Thanks for your comments. I have a lot of outlook tasks. Which includes exporting conversion etc. if one I succeeed I will continue wih the second project. – abbasiftt2 Aug 01 '20 at 18:57

0 Answers0