C# : How to Wrap Winword COM InterOp Methods

If you have ever used the Microsoft Office COM Interop classes to automate Word (or any other Office Application), you know those methods often take a lot of parameters, which can be tedious.

Note: This was written a while ago before .Net 4.0 was available. .Net 4.0 introduces the dynamic keyword which makes COM Interop much easier.

For example, the Word.Documents.Open() method takes 16 parameters in the Interop v 12 classes. You don’t want to have to specify all those parameters at each method call.

So what can you do? Well, starting with .Net 3.0, you can use a nice feature called extension methods. You will leverage those to give you nice methods to call with few parameters.

So you define a static class that will contain the extension methods of the various COM Interop classes you are interested in. For example, for the Open method discussed above, see how I wrap it with my extension method:

using Microsoft.Office.Interop.Word;

namespace MyWordNamespace {

    public static class ExtensionMethods {

        public static Document Open(
                this Documents documents, 
                string fileName) {

            Object filename                 = fileName;
            Object confirmConversions       = Type.Missing;
            Object readOnly                 = Type.Missing;
            Object addToRecentFiles         = Type.Missing;
            Object passwordDocument         = Type.Missing;
            Object passwordTemplate         = Type.Missing;
            Object revert                   = Type.Missing;
            Object writePasswordDocument    = Type.Missing;
            Object writePasswordTemplate    = Type.Missing;
            Object format                   = Type.Missing;
            Object encoding                 = Type.Missing;
            Object visible                  = Type.Missing;
            Object openConflictDocument     = Type.Missing;
            Object openAndRepair            = Type.Missing;
            Object documentDirection        = Type.Missing;
            Object noEncodingDialog         = Type.Missing;
            Object xmlTransform             = Type.Missing;

            return documents.Open(
                ref filename,
                ref confirmConversions,
                ref readOnly,
                ref addToRecentFiles,
                ref passwordDocument,
                ref passwordTemplate,
                ref revert,
                ref writePasswordDocument,
                ref writePasswordTemplate,
                ref format,
                ref encoding,
                ref visible,
                ref openAndRepair,
                ref documentDirection,
                ref noEncodingDialog,
                ref xmlTransform
            );
        }
    }
}

Which you can then call like this (I’ve also added wrappers for Documents.Close and Application.Quit):

using Microsoft.Office.Interop.Word;
using MyWordNamespace;

namespace MyApplication {

    class Program {

        static void Main(string[] args) {

            Application application = new Application();
            try {
                application.Visible = true;

                Document document = application.Documents.Open(
                    @"c:\temp\Document.docx"
                );
                Thread.Sleep(5000);

                application.Documents.Close();
            }
            finally {                
                application.Quit(WdSaveOptions.wdDoNotSaveChanges);
            }
        }
    }
}

Nice, isn’t it?

Happy programming!