Showing posts with label VSTO. Show all posts
Showing posts with label VSTO. Show all posts

Sunday, April 27, 2014

Displaying the column names for MS Project in C#


Whilst, MSDN provides a very updated and informative developer reference for MS Office  suite in general; it seems to have missed out the same for MS Project .

I was recently developing a MS Project addin in C# using VSTO, and came across several challenges ; but I am happy to say I completed the addin in due time.

The following code displays the name of the columns in the currently open project .

   MSProject.Project activeProject  = Globals.ThisAddIn.Application.ActiveProject;
    MSProject.Table prjTable = prjTable.TaskTables[activeProject  .CurrentTable];

    for (int i = 1; i <= prjTable .TableFields.Count; i++)
    {
         MSProject.PjField field = objTable.TableFields[i].Field;
         string colName  = Globals.ThisAddIn.Application.FieldConstantToFieldName(field).ToString();
        MessageBox.Show(colName);

    }
         
    if (activeProject  != null)
    {
        Marshal.ReleaseComObject(activeProject  );
        objProject = null;
    }

    if (prjTable != null)
    {
       Marshal.ReleaseComObject(prjTable );
       prjTable = null;
    }


There are two other points that should be noticed in this post :-

a) Usage of for loop instead of foreach loop. According to OOM.NET: Part 2 - Outlook Item Leaks , foreach  loops causes a memory leak while for does not. I am following this principle for all the Office addins.

b) Usage of Marshal.ReleaseComObject - Its usage is mandatory as all the objects being dealt with are COM objects

.

Sunday, February 14, 2010

Adding folders to a custom Outlook store

In continuation to my earlier post on creating an Outlook store, today I shall show you how to create custom folders for the store .
I started off by trying the MSDN way, and am providing the code below. The myStore refers to the Outlook.MAPIFolder which represents the custom store.

myStore.Folders.Add("First Folder", olFolderInbox);
myStore.Folders.Add("Second Folder", olFolderInbox);


While the first line created the folder for me , the second line greeted me with an execption "Invalid argument type" . I tried replacing the olFolderInbox parameter with the other permissible parameters of  OlDefaultFolders, but without success .It took me some time to solve this problem , but what I learnt was interesting.

If you look at the Folders.Add(Name,Type ) method you shall see that the Type is an optional parameter, and if you have not worked with Office Automation earlier in C#, you might be wondering how to populate the default parameter; and there was the problem .

Sunday, January 24, 2010

Creating a Outlook store programatically

Office programming has become much easier with Visual Studio for Office. Though the object model still needs to be know, but there is less of COM plumbing, and helper classes to make life easier .
Among the entire Office suite family, Outlook is probably the most used , and hence users prefer addins which can connect to other external applications, be it CRM or fetching data from web service and displaying in the Outlook dashboard
If you are really serious about Office Development,  you should get a copy of Add In Express, which makes office development very easy .  The Addin Express team has been kind enough to provide a 10% discount to the readers of this blogs and so you can grab a copy of Add In Express ( Standard , Professional, Premium ) at http://www.add-in-express.com/purchase/index.php using the discount coupon ADX-GSUJAY-ADX10.

The following code creates a MAPIStore programmmatically. I have used C# as the language. Though I have worked with MAPI , ( Simple and Extended MAPI ) with C++/COM, if you dont need Extended MAPI , C# is just fine . But again remember , that the entire Office Suie is unmanaged code, and C# is managed code , so you may have to do marshalling sometimes .


Outlook.NameSpace olNamespace = OutlookApp.GetNamespace("MAPI");
// Get the Default Inbox folder
Outlook.MAPIFolder olMapiFolder = olNamespace.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

Outlook.MAPIFolder olCustomFolder;               
string path = "C:\\mydata.pst";
// Create a store

Wednesday, December 23, 2009

Develop Office Addins in a breeze – Part 1

Five years back I was working on a product, which included Word automation. The task was simple ; let the user create a new Word document from within the product . The product was being developed in Visual C++ 6.0 and there was no VSTO . The key idea behind this feature was Office integration .  I had done this using Word automation and COM plumbing with the Word Object Model.
If you are new to Offiice Development ;  and interested in getting a quick overview, I would recommend VSTO 3.0 for Office 2007 Programming. On the other hand if you want to get more deep ,  I would recommend Visual Studio Tools for Office 2007: VSTO for Excel, Word, and Outlook .
Why Office Add - Ins
Things have changed in the last 5 years. Visual C++ 6 has been replaced by the Visual Studio suite, Windows Programming has become much easier, .NET has evolved,  global economy has gone north and new innovative and useful products have made development easier.
Maybe if I was developing the same product today, the concept might have been different . My boss would have probably told me , “Hey , why not develop a product specific menu  in the Word application itself, so the user can directly talk to our app while using Word”. This approach makes much sense, as it not only addresses our need, but also makes the user feel more comfortable ( he doesn't have to start our application to access  the  specific features )
So I would have landed developing a Word Add-in , maybe using Visual Studio Professional Edition ( Extensibility –> Shared Add In Project )  or Visual Studio for Office ( haven’t used it ever ) .