none
How to check if an MS Word is closed in C# RRS feed

  • Question

  • I am doing an application in C# which open exe files like notepad, calculator.
    So, my aim is when I open an exe file like Word, a button is created automatically
    and then when I close the Word app, the button will be removed.
    Each app will have its own button when it is launched.
    I tried to do it but I have some issues with the closing of the app.
    Here is what I did:

    var applicationWord = new Microsoft.Office.Interop.Word.Application();
    
    
                applicationWord.Visible = true;
                   applicationWord.DocumentBeforeClose += DocumentBeforeClose;
    
    
    
                   if (applicationWord.Visible == true)
                   {
    
    
                           button = new Button();
                           button.Image = Properties.Resources.word_80;
                           PIC_Barre.Controls.Add(button);
                           button.AutoSize = true;
                           //     button.Tag = proc.Id;
                           PIC_Barre.Controls.Add(button);
    
    
                       foreach (Process proc in Process.GetProcessesByName("WINWORD"))
                       {
                           if (proc.ProcessName.Contains("WINWORD"))
                           {
                               proc.WaitForInputIdle();
                               {
                                   Thread.Sleep(500);
                                   SetWindowPos(proc.MainWindowHandle.ToInt32(),
                                       (int)SetWinPos_ZOrderOpt.HWND_TOPMOST,
                                       0, 0, 0, 0,
                                       (int)(SetWinPosFlags.SWP_NOSIZE |
                                             SetWinPosFlags.SWP_NOMOVE));
                               }
                               button.Click += (s, e) => { ShowWindowAsync(proc.MainWindowHandle, (int)ShowWindowCommands.Normal); };
                               proc.Exited += (s, e) =>
                               {
                                   var method = (Action)(() => PIC_Barre.Controls.Remove(button));
                                   // button.Visible = false;
                                   if (button.InvokeRequired)
                                   {
                                       button.Invoke(method);
                                   }
    
    
                               };
    
                           }
    
                       }
                   }

    Thursday, September 15, 2016 8:05 AM

Answers

  • Hi TatsuSheva,

    you can use the below mentioned code to get the name of all currently opened document.

     Microsoft.Office.Interop.Word.Application objWord;
                objWord = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
                for (int i = 0; i < objWord.Windows.Count; i++)
                {
                    object a = i + 1;
                    Window objWin = objWord.Windows.get_Item(ref a);
                    MessageBox.Show(objWin.Caption);
                }

    Then instead of showing the document names in messagebox you need to store the name in an array.

    so now you have the list of documents to compare.

    I can see that you are using the thread to continuously check whether the document is still open or close.

    so here you need to again fetch the currently open documents through above mentioned code.

    and try to compare the results with the list stored in array.

    if document is not available or it closed then you can execute the code to destroy the button.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 20, 2016 2:45 AM
    Moderator

All replies

  • Hi TatsuSheva,

    I find that you had created the same that for this issue in ASP.NET Forum.

    and maybe you deleted that thread.

    How to check if an MS Word is closed in C#

    you had mentioned that ,"I am doing an application in C#."

    in which type of project you are developing?

    Web Application, Windows Application or Word Related Project?

    you had mentioned that,"when I open an exe file like Word, a button is created automatically
    and then when I close the Word app, the button will be removed.
    Each app will have its own button when it is launched.
    I tried to do it but I have some issues with the closing of the app. "

    you did not mentioned on which line of code you face the issue. you just mentioned you have issue with the closing the app. did you get any error?

    if possible please try to clear your overall objective.

    are you trying to handle the particular document or it can be any random document?

    I can see that you are trying to use Word.interop.

    if you are handling multiple type of files then you need to add interop for every file extensions. so that you can use the functionality of that application in your code.

    also I see that you are creating and deleting the buttons in your code.

    is it possible to just hide and show the buttons in your app.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 15, 2016 10:47 AM
    Moderator
  • It depends on many factors. If you call an instance of MS Word application, you have full control over that application. If not, you have to check (in some period of time, probably by using Background Worker) if instance of MS Word application is open and keep a button visible till the application is opened.
    Thursday, September 15, 2016 10:56 AM
  • It is not me who deleted, I think a moderator deleteD it.

    -in which type of project you are developing?

    Windows Application

    -you did not mentioned on which line of code you face the issue. you just mentioned you have issue with the closing the app. did you get any error?

    Is it not an error that I get but when I close a MS Word window , normally the button must be removed...

    -are you trying to handle the particular document or it can be any random document?

    Any random document.

    -also I see that you are creating and deleting the buttons in your code.

    -is it possible to just hide and show the buttons in your app.

    If I open many documentS at the same time how for example if the document is minimized how it will be restored ? Because of that I create a button for each app.


    • Edited by TatsuSheva Thursday, September 15, 2016 11:15 AM
    Thursday, September 15, 2016 11:00 AM
  • If it is the case of an instance of MS Word application how I can associate the button to the window so that when I close the window this specific button will be removed.
    • Edited by TatsuSheva Thursday, September 15, 2016 11:12 AM
    Thursday, September 15, 2016 11:11 AM
  • What kind of solution I can have for this issue please ?
    Thursday, September 15, 2016 5:17 PM
  • Hi TatsuSheva,

    you can use the below mentioned code to get the name of all currently opened document.

     Microsoft.Office.Interop.Word.Application objWord;
                objWord = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
                for (int i = 0; i < objWord.Windows.Count; i++)
                {
                    object a = i + 1;
                    Window objWin = objWord.Windows.get_Item(ref a);
                    MessageBox.Show(objWin.Caption);
                }

    Then instead of showing the document names in messagebox you need to store the name in an array.

    so now you have the list of documents to compare.

    I can see that you are using the thread to continuously check whether the document is still open or close.

    so here you need to again fetch the currently open documents through above mentioned code.

    and try to compare the results with the list stored in array.

    if document is not available or it closed then you can execute the code to destroy the button.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 20, 2016 2:45 AM
    Moderator