views:

1236

answers:

4

I'm writing an app that will manipulate Outlook data. I want to make a backup of that data first and am hoping I could just loop through the contact/calendar items, etc and write them out to a PST file.

How can I write the contents of 1 or several Outlook folders to a PST using .Net? [vb or c# no matter]

A: 

http://stackoverflow.com/questions/577904/can-i-read-an-outlook-2003-2007-pst-file-in-c

Chris Ballance
That is reading a PST not creating/writing to one isn't it?
brendan
+4  A: 

I was able to piece this code together from a variety of samples around the internet and MSDN docs. This will allow you to choose an outlook high level folder and will backup all folders underneath. In my case I didn't actually want mail folders so I exclude them.

        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
brendan
um; where is the "pst" object declared and initialized?
akavel
ns.AddStore(BACKUP_PST_PATH) - this will create a PST if none exists at that location - ns.Session.Folders.GetLast() will then get you a handle to that PST/Folder
brendan
so the "pst" variable in your code (in 2 places) is a typo meant to be "oFolder"?
akavel
yep, works well with "oFolder". Thanks very much! Although in the end it seems I won't be able to use it for my needs anyway...
akavel
ah, good catch, fixed typos
brendan
A: 

Thans for your valuable solution and i am able to create a pst file using this code but when i restore this file , I can see there is no mail attach with it. Don't know wahts the reason so far?

Vinod Kalla
If you remove the line _f.DefaultItemType <> Microsoft.Office.Interop.Outlook.OlItemType.olMailItem_, then it will save mailbox items as well.
Michael Todd
A: 

This looks good, but, I can't seem to delete the resulting pst file on the hard drive as it says it is in use by another process. The only thing that works is completely shutting down the machine. Just closing the app and Outlook doesn't work. Any ideas?

Dan
Make sure you are calling ns.RemoveStore(oFolder) as that should release the pst - other then that might want to ask as a seperate quest on here
brendan
Yep, I'm already calling RemoveStore. So there must be something else going on. I will post another question. Thanks!
Dan