Click here to Skip to main content
15,880,725 members
Articles / Programming Languages / C#
Article

Helper class to load WinWord

Rate me:
Please Sign up or sign in to vote.
4.67/5 (5 votes)
28 Nov 2002 100.3K   2   40   19
Simple class to load/unload WinWord (or any other OLE application)

Introduction

In applications I develop, I often use Microsoft Word to generate various documents (reports, agendas, invoices, etc). The code to load and shut down the WinWord's COM server was always the same, so I wrote a simple helper class which manages this.

The Code

C#
using System;
using System.Threading;
using System.Runtime.InteropServices;

namespace AlexKay.Office
{
    /// <summary>
    /// Helper class to manage the Word.Application coclass.
    /// </summary>
    public class WordLoader : IDisposable
    {
        private Word.Application wordApp = null;
        private bool isNewApp = false;

        private bool disposed = false;
        
        public WordLoader()
        {
            // Check if Word is registered in the ROT.
            try
            {
                wordApp = (Word.Application)Marshal.
                    GetActiveObject("Word.Application");
            }
            catch
            {
                wordApp = null;
            }
            // Load Word if it's not.
            if(wordApp == null)
            {
                try
                {
                    wordApp = new Word.ApplicationClass();
                    isNewApp = true;
                }
                catch
                {
                    wordApp = null;
                }
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if(!this.disposed)
            {
                if(disposing)
                {
                    // Dispose managed resources.
                }
                
                // Dispose unmanaged resources.
                if(wordApp != null)
                {
                    try
                    {
                        if(isNewApp && wordApp.Documents.Count == 0)
                        {
                            object arg1 = Word.WdSaveOptions.
                                            wdDoNotSaveChanges;
                            object arg2 = null;
                            object arg3 = null;
                            wordApp.Quit(ref arg1, ref arg2, ref arg3);

                            // Wait until Word shuts down.
                            for(;;)
                            {
                                Thread.Sleep(100);
                                try
                                {
                                    // When word shuts down this call 
                                    // throws an exception.
                                    string dummy = wordApp.Version;
                                }
                                catch
                                {
                                    break;
                                }
                            }
                        }
                    }
                    catch {}

                    wordApp = null;
                }
            }
            disposed = true;
        }

        ~WordLoader()
        {
            Dispose(false);
        }

        public Word.Application Application
        {
            get
            {
                return wordApp;
            }
        }
    }
}

Comments

When the WordLoader object is created, it checks if the Word co-class is already created and registered in the running object table. If it's not, it creates a new instance of the Word.Application co-class which loads the WinWord application. When the WordLoader object is disposed it shuts down Word, if it has no documents open.

As you can see, this is quite simple. Any comments on the code are welcome!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralDoubt.. Pin
DevangUdeshi3018217-Apr-05 20:27
DevangUdeshi3018217-Apr-05 20:27 
GeneralRe: Doubt.. Pin
Alexander Kojevnikov19-Apr-05 9:31
Alexander Kojevnikov19-Apr-05 9:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.