Microsoft Word Interop performance issue

-2

I have a winform program which loads Microsoft Word and performs some basic editing (find & replace), as well as some autosaving.

It loads an existing word template, which again is basic text.

The code for the interop is;

 try
        {
            // Is Word running?
            WordApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Microsoft.Office.Interop.Word.Application;
            WordApp.Visible = true;
            return WordApp;
        }
catch (COMException ce)
        {
            WordApp = null;
            if (ce.ErrorCode == unchecked((int)0x800401E3))

                WordApp = new Microsoft.Office.Interop.Word.Application();

            WordApp.Visible = true;
            return WordApp;
        }

Once the document is open, the user types what they need to but it has been noted that after a page or so of text, the performance really slows. There is an increasing lag on the users typing.

I thought initially it was due to some issues with the find/replace code so I have commented out everything apart from the code for loading the template;

            WordApp = WordEdit.GetWord(); //Class & Method calling interop code
        WordApp.Documents.Add(AppDomain.CurrentDomain.BaseDirectory + "\\Templates\\" + DocType + ".dot");

        //set Active Document
        WordDoc = WordApp.ActiveDocument;

The performance is still as poor.

I then thought to release the COM and set things to null, but again this didn't have any effect.

     System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);
        WordApp = null;
        WordDoc = null;

I then thought maybe my app is causing the system in general to slow down. If I quit the application but continue to use the Word application it load, the performance is still slow. If I start a fresh Word application (manually) this works perfectly. So it has something to do with the way my application is loading word. My application does not have any impact on system resources, and is currently set to do nothing other than load the template.

Is there either a different way get hold of Word (a different way to use interop) or a way to improve performance?

c#
ms-word
interop
asked on Stack Overflow Nov 13, 2017 by Adrian Brown • edited Nov 13, 2017 by YowE3K

1 Answer

-1

Although I haven't been able to pin point the cause of the slow performance, I have found a workaround.

Instead of having the app create a new Word session via the marshal, I instead use this code;

            Process.Start(AppDomain.CurrentDomain.BaseDirectory + "\\Templates\\" + DocType + ".dot");
        System.Threading.Thread.Sleep(2000);

This opens the required template (DocType+ extension) so the system creates a Word session.

I then use

 WordApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Microsoft.Office.Interop.Word.Application;
        WordApp.Visible = true;
        return WordApp;

To take control of the now active session.

The ` System.Threading.Thread.Sleep(2000); is there because if it wasn't, things run too quickly and then my app can't get hold of the Active Document in the WordApp object. Giving it a few seconds wait gives it chance to catch up.

It's not ideal - but it has removed all performance issues, `

answered on Stack Overflow Nov 27, 2017 by Adrian Brown

User contributions licensed under CC BY-SA 3.0