28 October 2015

Creating and managing PST files using Outlook Add-in

Outlook add-ins provides an easier way to add custom tabs and buttons to your Outlook toolbar which can perform a custom task or functionality that is originally not available in Outlook.

Example of such a requirement can be to have a button which can file an email message in a new or existing PST under appropriate new or existing folders based on certain special text in email subject or body. If the PST or folder inside PST does not exist, it should get created automatically.

This can be done using ‘VSTO 3.0 Outlook 2007’ add-in. This is available in Visual Studio 2008 as Outlook 2007 Add-in.

Implementation

Step 1 - Create a project of type – ‘Office --> Outlook 2007 Add-in’ in Visual Studio 2008. You can choose language as either C# or VB.NET. In our case, we have used VB.NET.




























Step 2 - Now add an item of type ‘Ribbon (Visual Designer)’ in the project
























Step 3 - This ribbon creates a new ‘Custom Tab’ in your outlook ribbon bar (toolbar). You can rename the ribbon from ‘Custom Tab’ to ‘Auto File’. You can then add a ‘Office Ribbon Control – Box’ on the ribbon and then add ‘Office Ribbon Control – Button’.

























Step 4 - Double click the button added on ribbon. This will auto-generate the click function for that button. The click function will be something like this – 

Private Sub FileToPST_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles FileToPST.Click
End Sub

Here FileToPST_Click is the name of the function and it handles the Click event of button FileToPST

Step 5 - Now in this function, we can write code to create/manage PSTs and move email in them.

1.    Create object of type Outlook.Application. This object will act as handle for outlook application

Dim OL As Outlook.Application
OL = CType(GetObject(, "Outlook.Application"), Outlook.Application)

2.    Create object of type Outlook.MailItem. This object will act as handle for current outlook mail.

Dim MI As Outlook.MailItem
MI = OL.ActiveInspector.CurrentItem

3.    Create object of type Outlook.Namespace. This object is required to refer to MAPI namespace and outlook PST will be created under this namespace. This namespace contains both current Inbox and all the PSTs.

Dim NS As Outlook.NameSpace
NS = OL.GetNamespace("MAPI")

4.    Create object of type Outlook.Store. This object will act as handle for the PST where we will store the email.

Dim store As Outlook.Store = Nothing

5.    You can refer to subject and body of email using following code –

         mailSubject = MI.Subject
         
   mailBody = MI.Body

6.    Now create or open an existing PST using following code

NS.AddStore(PSTFolderPath)

7.    Once PST is created or opened, use following code to loop through all the PSTs under namespace and then refer to it.

            For Each store In NS.Stores
                If store.FilePath = PSTFolderPath Then
                    Exit For
                End If
   Next

8.    At the end of this code, object store will be referring to required PST (new or existing).

9.    Now you need to refer to root folder of your PST first. To do this, use following code –

          Dim rootFolder As Outlook.MAPIFolder
          rootFolder = store.GetRootFolder()

10. Once, you have access to the root folder, you can either access sub-folders under it or create new sub-folders under it.

11. To access a sub-folder named ‘MyFolder’ under your root folder in PST, you can use following code –

Dim subFolder As Outlook.Folder
         For Each subFolder In rootFolder.Folders

            If subFolder.Name = "MyFolder" Then
                  Exit For
            End If
Next

12. If you would like to create a new folder under root folder with name ‘MyNewFolder’, you can use following code –

rootFolder.Folders.Add("MyNewFolder")

13. Finally, if you would like to move or copty your email message in this folder, you can use following code

                                  i.    For moving - MI.Move(subFolder)
                                 ii.    For copying - MI.Copy(subFolder)

Here MI object refers to current mail item/email message.

14. Once, all the required operations have been performed, you can close the PST by removing it from MAPI namespace. This can be done using following code


         NS.RemoveStore(rootFolder)


Here I have provided very basic example of moving/copying an email from Inbox to your existing/new PST in an existing/new folder. Further logic can be created as per requirement like moving/copying mail based on certain text in Subject/Body of email. There are no limitations of type of logic that can be written. This overcomes the limitations in ‘Email Rules’ provided by Outlook.

Deployment

Lets name the sample application has as ‘Oscar_OutlookAddIn’

For creating installer package, following steps can be followed:-

Build --> Publish Oscar_OutlookAddin


















It will ask for location where you will like to create the installer. Enter or Browse the location and then click ‘Next’ button.



On the next page, select the middle option – ‘From a UNC path or file share’ and click Next































Last screen will appear now showing you the location where installer package will be created. Click on ‘Finish’ button.































This will create the installer at the specified location.









Now you can distribute this complete folder to client machines for installation. User can click on ‘setup.exe’ to install the add-in. Once add-in is installed, whenever any email message is opened, a new button called ‘Auto-File’ will appear in a separate ribbon like this – 




Outlook add-ins provides us the flexibility to create custom buttons in our Outlook which cannot be fulfilled by existing functionality in Outlook. 























No comments: