Click here to Skip to main content
13,665,371 members
Click here to Skip to main content
Add your own
alternative version


39 bookmarked
Posted 28 Nov 2002

Helper class to load WinWord

, 28 Nov 2002
Rate this:
Please Sign up or sign in to vote.
Simple class to load/unload WinWord (or any other OLE application)


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

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.
                wordApp = (Word.Application)Marshal.
                wordApp = null;
            // Load Word if it's not.
            if(wordApp == null)
                    wordApp = new Word.ApplicationClass();
                    isNewApp = true;
                    wordApp = null;

        public void Dispose()

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

                            // Wait until Word shuts down.
                                    // When word shuts down this call 
                                    // throws an exception.
                                    string dummy = wordApp.Version;
                    catch {}

                    wordApp = null;
            disposed = true;


        public Word.Application Application
                return wordApp;


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!


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


About the Author

Alexander Kojevnikov
Software Developer (Senior)
Australia Australia
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionMarshall.getactiveobject(word.application) does not works for windows services ? Pin
muneebalikiyani26-Feb-13 1:05
membermuneebalikiyani26-Feb-13 1:05 
GeneralSlowly. Pin
123456uio5-Feb-07 22:41
member123456uio5-Feb-07 22:41 
GeneralA problem. Pin
shir118-Sep-06 22:29
membershir118-Sep-06 22:29 
GeneralSuggestion Pin
xlg8-Aug-06 5:59
memberxlg8-Aug-06 5:59 
GeneralSweet Pin
<marquee>ElNoNo12-Apr-06 11:13
memberElNoNo12-Apr-06 11:13 
GeneralWinWord in AxWebBrowser Pin
pramod.21c22-Feb-06 19:17
memberpramod.21c22-Feb-06 19:17 
GeneralDoubt.. Pin
DevangUdeshi3018217-Apr-05 20:27
memberDevangUdeshi3018217-Apr-05 20:27 
GeneralRe: Doubt.. Pin
Alexander Kojevnikov19-Apr-05 9:31
memberAlexander Kojevnikov19-Apr-05 9:31 
GeneralWordAutomation Examples Pin
Nickle31-Dec-02 10:14
memberNickle31-Dec-02 10:14 
GeneralRe: WordAutomation Examples Pin
Alexandre Kojevnikov3-Feb-03 22:27
memberAlexandre Kojevnikov3-Feb-03 22:27 
Try this code:
object argFileName = "file.doc";
object argReadOnly = false;
object argAddToRecentFiles = false;
object argVisible = false;
object misssing = Missing.Value;
    ref argFileName, ref missing, ref argReadOnly,
    ref argAddToRecentFiles, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing,
    ref missing, ref argVisible);

Alexandre Kojevnikov
Leuven, Belgium

GeneralSorry to tell you this... Pin
David Stone29-Nov-02 11:04
memberDavid Stone29-Nov-02 11:04 
GeneralRe: Sorry to tell you this... Pin
Marc Clifton29-Nov-02 12:02
memberMarc Clifton29-Nov-02 12:02 
GeneralRe: Sorry to tell you this... Pin
Jörgen Sigvardsson29-Nov-02 14:04
memberJörgen Sigvardsson29-Nov-02 14:04 
GeneralRe: Sorry to tell you this... Pin
Nishant S29-Nov-02 15:13
editorNishant S29-Nov-02 15:13 
GeneralRe: Sorry to tell you this... Pin
Marc Clifton29-Nov-02 12:05
memberMarc Clifton29-Nov-02 12:05 
GeneralRe: Sorry to tell you this... Pin
Nishant S29-Nov-02 15:12
editorNishant S29-Nov-02 15:12 
GeneralRe: Sorry to tell you this... Pin
Alex Kay30-Nov-02 6:47
memberAlex Kay30-Nov-02 6:47 
GeneralRe: Sorry to tell you this... Pin
David Stone30-Nov-02 13:18
memberDavid Stone30-Nov-02 13:18 
GeneralRe: Sorry to tell you this... Pin
shofb1-May-08 23:25
membershofb1-May-08 23:25 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180810.1 | Last Updated 29 Nov 2002
Article Copyright 2002 by Alexander Kojevnikov
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid