2013년 1월 23일 수요일

[vsto] Unable to copy/add contact in programatically created pst file through vsto



Unable to copy/add contact in programatically created pst file through vsto



http://www.add-in-express.com/creating-addins-blog/2012/03/19/outlook-personal-folders-store/

http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/10cda756-8b05-4e7b-98d1-ce288bebe656/




Sometimes developers need to add an additional store in Outlook for archiving e-mails, making backup copies of email conversations etc. TheAddStore method of the Namespace class of the Outlook Object Model provides the required functionality. It adds a personal folders file (.pst) to the current profile. So, let’s look at this method in depth! I’ll provide code samples both for VSTO developers and those who develop withAdd-in Express for Office and .net.

A personal folders store. What is it?

New Personal folders stores in the Outlook Navigation pane.Typically a personal folders store represents a root folder in the Outlook Navigation pane. After you run a sample code which adds stores you can see a similar picture in your Outlook profile. The screenshots were captured on my PC with Windows 7 x64 and Outlook 2010 x64.
Also you may find the results of our tests in the Mail section of the Control Panel applet.
      1. Go to User Accounts and click Mail.
      The Mail section in the Control Panel applet.
      2. Choose an appropriate account.
      Mail profiles
      3. Press the Data Files… button to see all available data files.
      Profile settings
      4. Here you are! Now you can see two additional stores that have been added using the sample code.
      Data files

Coding… coding… coding…

The AddStore method accepts a string which represents a file path to the Outlook .pst file. If there is no such .pst file on the hard drive it will be created automatically. I would like to point out the fact that theAddStore method doesn’t return a store object. It doesn’t return anything at all; the method just does its job. That is because the Stores collection and the Store class were introduced in Outlook 2007. Outlook 2010 brought some new methods and properties to the Store class like CategoriesIsConversationEnabled andGetDefaultFolder, etc.
Outlook 2003 introduced the AddStoreEx method which requires a second parameter. It can be one of the following constants of the OlStoreType enumeration: olStoreDefaultolStoreUnicode and olStoreANSI.
  • olStoreDefault – allows you to create a .pst file in the default format which is compatible with the mailbox mode (the cached or non-cached exchange mode).
  • olStoreUnicode – allows you to add a new .pst file to the user’s profile that has a greater capacity (than the ANSI format provides) and supports Unicode data. The Unicode format of the .pst file was introduced with Outlook 2003.
  • olStoreANSI – allows you to add a new .pst file which is compatible with all versions of Outlook including Outlook 2000 and 2002, but it doesn’t fully support Unicode data.
You just need to pass an instance of the Outlook Application class to the AddNewPersonalStore method to get it running.
C# & Add-in Express:
private void AddNewPersonalStore(Outlook._Application OutlookApp) 
     Outlook.NameSpace ns = null
     try 
     { 
         ns = OutlookApp.GetNamespace("MAPI"); 
         ns.AddStore(@"D:\Mail stores\MySecondStore.pst"); 
         // ns.AddStoreEx(@"D:\Mail stores\MySecondUnicodeStore.pst", 
         //                     Outlook.OlStoreType.olStoreUnicode); 
     } 
     catch (Exception ex) 
     { 
         MessageBox.Show("Error: " + ex.Message); 
     } 
     finally 
     { 
         if (ns != null) Marshal.ReleaseComObject(ns); 
     } 
C# & VSTO:
using System.Runtime.InteropServices; 
using System.Windows.Forms; 
// 
private void AddNewPersonalStore(Outlook.Application Application) 
    Outlook.NameSpace ns = null
    try 
    { 
        ns = Application.GetNamespace("MAPI"); 
        ns.AddStore(@"D:\Mail stores\MySecondStore.pst"); 
        // ns.AddStoreEx(@"D:\Mail stores\MySecondUnicodeStore.pst", 
        //                      Outlook.OlStoreType.olStoreUnicode); 
    } 
    catch (Exception ex) 
    { 
        MessageBox.Show("Error: " + ex.Message); 
    } 
    finally 
    { 
        if (ns != null) Marshal.ReleaseComObject(ns); 
    } 
VB.NET & Add-in Express:
Private Sub AddNewPersonalStore(ByRef OutlookApp As Outlook._Application) 
    Dim ns As Outlook.NameSpace = Nothing 
    Try 
        ns = OutlookApp.GetNamespace("MAPI"
        ns.AddStore("D:\Mail stores\MySecondStore.pst"
        ' ns.AddStoreEx("D:\Mail stores\MySecondUnicodeStore.pst", 
        '                      Outlook.OlStoreType.olStoreUnicode) 
    Catch ex As Exception 
        MsgBox("Error: " + ex.Message) 
    Finally 
        If Not IsNothing(ns) Then Marshal.ReleaseComObject(ns) 
    End Try 
End Sub 
VB.NET & VSTO:
Imports System.Runtime.InteropServices 
' 
Private Sub AddNewPersonalStore(ByRef Application As Outlook.Application) 
    Dim ns As Outlook.NameSpace = Nothing 
    Try 
        ns = Application.GetNamespace("MAPI"
        ns.AddStore("D:\Mail stores\MySecondStore.pst"
        ' ns.AddStoreEx("D:\Mail stores\MySecondUnicodeStore.pst", 
        '                      Outlook.OlStoreType.olStoreUnicode) 
    Catch ex As Exception 
        MsgBox("Error: " + ex.Message) 
    Finally 
        If Not IsNothing(ns) Then Marshal.ReleaseComObject(ns) 
    End Try 
End Sub 
If you run the code in Outlook 2010, you may notice that the following calls are identical:
AddStore(@"D:\Mail stores\MySecondStore.pst"); 
... 
AddStoreEx(@"D:\Mail stores\MySecondUnicodeStore.pst"
                   Outlook.OlStoreType.olStoreUnicode); 
Also, I developed a simple windows forms application which adds a new store to the Outlook profile. Here is the code. Please don’t forget to add a COM reference to Outlook if you want to get the code running.
C# & Windows Forms Application:
using Outlook = Microsoft.Office.Interop.Outlook; 
using System.Runtime.InteropServices; 
// 
private void AddNewStoreToOutlook(string pathToPst) 
    Outlook.Application app = null
    Outlook.NameSpace ns = null
    try 
    { 
        app = new Outlook.Application(); 
        ns = app.GetNamespace("MAPI"); 
        ns.Logon(); 
        ns.AddStore(pathToPst); 
        ns.Logoff(); 
        app.Quit(); 
    } 
    catch (Exception ex) 
    { 
        MessageBox.Show("Error: " + ex.Message); 
    } 
    finally 
    { 
        if (ns != null) Marshal.ReleaseComObject(ns); 
        if (app != null) Marshal.ReleaseComObject(app); 
    } 
VB.NET & Windows Forms Application:
Imports System.Runtime.InteropServices 
Imports Outlook = Microsoft.Office.Interop.Outlook 
' 
Private Sub AddNewStoreToOutlook(pathToPst As String
    Dim app As Outlook.Application = Nothing 
    Dim ns As Outlook.NameSpace = Nothing 
    Try 
        app = New Outlook.Application() 
        ns = app.GetNamespace("MAPI"
        ns.Logon() 
        ns.AddStore(pathToPst) 
        ns.Logoff() 
        app.Quit() 
    Catch ex As Exception 
        MessageBox.Show("Error: " + ex.Message) 
    Finally 
        If Not IsNothing(ns) Then Marshal.ReleaseComObject(ns) 
        If Not IsNothing(app) Then Marshal.ReleaseComObject(app) 
    End Try 
End Sub 
As you may see, the Logon and Logoff methods were used in case of a standalone application. That is because add-ins do their job after a user has logged into an Outlook profile.
See you on our forums and in the e-mail support!

댓글 없음: