none
Using Interop.word in an application RRS feed

  • Question

  • My apologies if this question was already addressed in another thread. I've searched for a solution and did not find it and hence I've asked this question..

    Solution Environment:

    MS Visual Studio 2010 Ultimate, MS Office 2007 Professional, Microsoft Interop classes

    Scenario:

    I've created a .Net application which uses the Interop classes to interact with MS Word. Consider this: An application which mimics a key-value pair table. When the user clicks on the "value" field, the .net application opens up a winword window where the user can enter some notes. Upon clicking close (winword), .net app will do a winword.wordopenxml and save the data in the field. For this to happen, i've added event handles to winword. The application runs perfectly fine. The problem arises when i have multiple instances of winword opened (separately by the user but not thru the .net application) . The event handlers from the .net application is applied to all instances of winword and hence, upon closing just one instance word, all the other instances gets closed too. I tried different ways to initialize the word object in the application. I also tried the work around "Initializing two instances of winword and quit the first instance" -> This approach also did not work well. 

    Gurus: Could you please help me find a suitable solution for this problem ?

    -Rupesh


    • Moved by Cindy Meister MVPModerator Thursday, June 16, 2011 8:49 AM not using VSTO technology (From:Visual Studio Tools for Office)
    Saturday, June 11, 2011 6:41 PM

All replies

  • I've created a .Net application which uses the Interop classes to interact with MS Word. Consider this: An application which mimics a key-value pair table. When the user clicks on the "value" field, the .net application opens up a winword window where the user can enter some notes. Upon clicking close (winword), .net app will do a winword.wordopenxml and save the data in the field. For this to happen, i've added event handles to winword. The application runs perfectly fine. The problem arises when i have multiple instances of winword opened (separately by the user but not thru the .net application) . The event handlers from the .net application is applied to all instances of winword and hence, upon closing just one instance word, all the other instances gets closed too. I tried different ways to initialize the word object in the application. I also tried the work around "Initializing two instances of winword and quit the first instance" -> This approach also did not work well.

     

    Hi Rupesh,

     

    Thanks for your post.

    Maybe I have not understood you correctly. However, why don’t embed the application into a Word add-in if the application just implements some functionality like entering notes?

    In addition, what do you mean of separately by users? Office is user specific, we are only allowed to open an Office instance by current account. You need to switch user of Windows OS to change the user of Word.

     

    I look forward to hearing of you.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, June 14, 2011 3:39 AM
    Moderator
  • Hi Rupesh

    As you're not using VSTO I'm going to move this question to the Word for Developers forum where it will be more on-topic.

    <<The problem arises when i have multiple instances of winword opened (separately by the user but not thru the .net application) . The event handlers from the .net application is applied to all instances of winword and hence, upon closing just one instance word, all the other instances gets closed too. I tried different ways to initialize the word object in the application. I also tried the work around "Initializing two instances of winword and quit the first instance" -> This approach also did not work well. >>

    Please explain how the approach "did not work well".


    Cindy Meister, VSTO/Word MVP
    Thursday, June 16, 2011 8:48 AM
    Moderator
  • Hi Cindy,

    Thanks for moving to appropriate forum

    Hi Rupesh,

    Any update? I look forward to hearing of you.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 20, 2011 11:42 AM
    Moderator
  • Hi Calvin,

    Solution Environment: MS Visual Studio 2010 Ultimate, MS Office 2010

    Sorry for my late response. Here is the detailed description of what i've implemented.
    I've written a windows form application which uses winword.exe for user input. 

    User opens the application. Application will initialize a winword process and set winword.visible = false.
    App also adds event handlers (event4) for onClose and onSave. When ever user wants to enter some data, the app brings up the winword.exe using winword.visible = true.

    Once the user enters his input and on click on "X" mark or close of winword, the app event handler gets fired and picks up the document and does some business related tasks. Even after the user click on "X" mark, the app does not close the existing winword.exe process. Instead, it sets the winword.visible = false and closes only the document. The winword process still stays in the background.

    During this process, I noticed that when ever the application is running, the event handlers that were meant only for the hidden winword.exe process gets attached to ANY winword.exe process (opened by the user manually - like opening a word document alongside the app) . This is creating a lot of issues. I am not able to over come this scenario.

    -Rupesh

    Monday, July 11, 2011 3:01 AM
  • Hi Rupesh,

     

    Thanks for your update.

     

    According to your description, I try to repro your issue by following code:

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using Word = Microsoft.Office.Interop.Word;

     

    namespace WinFormHandeWord

    {

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

            Word.Application app = null;

            Word.Document doc = null;

            private void button1_Click(object sender, EventArgs e)

            {

                app = new Word.Application();

                app.Visible = false;

                doc = app.Documents.Add();

     

                app.DocumentBeforeClose += new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(app_DocumentBeforeClose);

            }

     

            void app_DocumentBeforeClose(Word.Document Doc, ref bool Cancel)

            {

                Cancel = true;

                Doc.Close();

                app.Visible = false;

            }

     

            private void button2_Click(object sender, EventArgs e)

            {

                app.Visible = true;

            }

     

            private void button3_Click(object sender, EventArgs e)

            {

                Word.Application oWord = new Word.Application();

                oWord.Visible = true;

                Word.Document oDoc = oWord.Documents.Add();

            }

        }

    }

     

    It is a WinForm application includes three buttons:

    Button1: Create the Word application and hide it, register DocumentBeforeClose event handler

    Button2: Unhide the application

    Button3: Create another Word application

     

    I tried something like follow:

    Click button1 to create the application and a document (Document1) -> click button2 to unhide the application and a document (Document2) -> click button3 to create another application -> Set a breakpoint at DocumentBeforeClose even handler -> close document1, you will see that the event is fired -> close the document2, the event doesn’t raise.

     

    It seems that the issue doesn’t occur on my side. However, if you open task manager after above action done, you will see two WINWORD.EXR processes. The issue is similar with yours somehow, but it isn’t the issue as your description. The scenario is caused that the COM object resource created by managed code hasn’t been release by GC. The resource will be released after you close the WinForm application.

     

    I look forward to hearing of you soon.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 11, 2011 4:07 AM
    Moderator
  • Hi Rupresh

    <<During this process, I noticed that when ever the application is running, the event handlers that were meant only for the hidden winword.exe process gets attached to ANY winword.exe process (opened by the user manually - like opening a word document alongside the app) . This is creating a lot of issues. I am not able to over come this scenario.>>

    This is a common problem and has to do with the design of the Office applications and how they're registered in the ROT. See this article (but note it's not actually a bug; the behavior at the time the article was written as perceived as a bug, so somehow that snuck into the article title):

    BUG: Starting Word Manually Uses Same Instance as Automation
    http://support.microsoft.com/kb/188546/


    Cindy Meister, VSTO/Word MVP
    Monday, July 11, 2011 10:05 AM
    Moderator
  • I would like to demonstrate the issue using a screen share. Please let me know if its possible for you to join me for a sreenshare!

     

    -Rupesh

    Tuesday, July 12, 2011 6:26 PM
  • You can post some screen captures or upload your project to MSN SkyDriver and post a link here for download.

     

    About how to insert a picture in your post in MSDN forum, please check this thread:

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/33bf2119-5f56-40b2-a689-d437ec09e550

     

    I look forward to hearing of you.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 13, 2011 7:49 AM
    Moderator
  • Hi Calvin,

    After a very big gap, i've tried the code. I was able to see the same issue as described above.

    Download the code from here: http://cid-3c46ca45ddd36aa7.skydrive.live.com/self.aspx/.Public/WordAutomation.zip

    Change the TargetFramework from 3.5 to 4.0 and compile the "WindowsFormsApplication1" application. (Surprisingly, I see this issue only if I do a ctrl+n on the opened document in 3.5)

    Run the application. Click on the button and select a word document(#ref1). Now, open a word document(#ref2) outside the application (from explorer.exe)

    try to close the document (#ref2). The events from (#ref1) also gets attached to the (#ref2). Let me know if you are still unable to replicate the issue.

    I will create a sceencast and uplaod it to a suitable location.

    -Rupesh

    Thursday, November 24, 2011 1:37 AM