none
how to close a running instance of Word document? (C#) RRS feed

  • Question

  • Hello, I am doing an application in which when I launch a MS Word app, a button is created and when the MS Word app is closed the button is removed.

    So here is  what I did to associate the MS Word and the button...

    Word.Application wdApp = new Word.Application();
    string oldCaption = wdApp.Application.Caption ;
    string guid = Guid.NewGuid().ToString();
    //set caption to random value
    wdApp.Application.Caption = guid;
    //make sure app is visible:
    wdApp.Visible = true;
    //find random value to get process id
    int processId = GetProcessIdByWindowTitle(guid);
    
    //reset caption
    wdApp.Application.Caption = oldCaption;
    
    //create a dictionary
    Dictionary<int, Button> mapping = new Dictionary<int, button>();
    //add mapping
    mapping.Add(new KeyValuePair<int, Button>(processId, deleteButton));
    
    
    
    
    public static int GetProcessIdByWindowTitle(string AppId)
    {
       Process[] P_CESSES = Process.GetProcesses();
       for (int p_count = 0; p_count < P_CESSES.Length; p_count++)
       {
            if (P_CESSES[p_count].MainWindowTitle.Equals(AppId))
            {
                        return P_CESSES[p_count].Id;
            }
       }
    
        return Int32.MaxValue;
    }

    But I got an error here mapping.Add(new KeyValuePair<int, Button>(processId, deleteButton));

    I am getting this error:  “No overload method for Add takes 1 argument”... 

    What should I do ? 

    Is there any oher solution for this ?

    Thank you.

    • Moved by David_JunFeng Monday, September 19, 2016 3:02 AM
    • Moved by CoolDadTx Monday, September 19, 2016 2:03 PM Office related
    Friday, September 16, 2016 6:04 PM

All replies

  • Hello,

    The Add method of the Dictionary class adds the specified key and value to the dictionary. And it takes two parameters:

    key - The key of the element to add.
    value - The value of the element to add. The value can be null for reference types.

    So, the code should look like this:

    mapping.Add(processId, deleteButton);
    
    

    Anyway, I've noticed the following line of code:

    mapping.Add(new KeyValuePair<int, Button>(processId, deleteButton));

    Where does the deleteButton instance come from?


    [custom.development]

    Friday, September 16, 2016 7:15 PM
  • Hi TatsuSheva,

    This is the forum to discuss questions and feedback for Word for Developers, since this issue is related to C#, I'll move your question to the MSDN forum for Visual C#

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=csharpgeneral

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, 
    and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us.
     
    Thanks for your understanding.
    Monday, September 19, 2016 2:05 AM
  • I already created a question in this forum but they told me that it will be more appropriate to put it here.
    • Edited by TatsuSheva Monday, September 19, 2016 9:38 AM
    Monday, September 19, 2016 9:37 AM
  • Hello,

    The Add method of the Dictionary class adds the specified key and value to the dictionary. And it takes two parameters:

    key - The key of the element to add.
    value - The value of the element to add. The value can be null for reference types.

    So, the code should look like this:

    mapping.Add(processId, deleteButton);
    

    Anyway, I've noticed the following line of code:

    mapping.Add(new KeyValuePair<int, Button>(processId, deleteButton));

    Where does the deleteButton instance come from?


    [custom.development]

    Here is my code to check if an instance is closed:
     try
                {
    
                    Word.Application wdApp = new Word.Application();
                    string oldCaption = wdApp.Application.Caption;
                    string guid = Guid.NewGuid().ToString();
                    //set caption to random value
                    wdApp.Application.Caption = guid;
                    //make sure app is visible:
                    wdApp.Visible = true;
                    //find random value to get process id
                    int processId = GetProcessIdByWindowTitle(guid);
               
                    
                    //reset caption
                    wdApp.Application.Caption = oldCaption;
               
                  
                    if( wdApp.Visible == true)
                    {
                        
                        deleteButton = new Microsoft.Office.Tools.Word.Controls.Button();
                        //create a dictionary
                        mapping = new Dictionary<int, Button>();
                        //add mapping
                        mapping.Add(processId, deleteButton);
                        PIC_Barre.Controls.Add(deleteButton);
    
                       
    
    
                        //PIC_Barre.Controls.Add(_button);
                    }
                    ((Word.ApplicationEvents4_Event)wdApp).Quit += () =>
                    {
                     
                    // remove the button corresponding to the processid
                         var method = (Action)(() =>
                    PIC_Barre.Controls.Remove(mapping[processId]));
                         if (mapping[processId].InvokeRequired)
                         {
                             mapping[processId].Invoke(method);
                           
                         }
                     // remove the key from the dictionary
                      
                    };
                    Debugger.Break();
                 
    
                
                }
                catch
                {
    

    I can remove only the last button I create, when I want to remove the others  I can't... I put breakpoint in order to know the value of the different variables .
    But when I create 2 buttons, the count in the mapping.add is always equal to 1... Is it normal ?
    • Proposed as answer by Crazy Cats Friday, September 23, 2016 7:40 AM
    Monday, September 19, 2016 3:17 PM
  • Hi TatsuSheva,

    According to your description, you could try to modify your code like below:
    Microsoft.Office.Tools.Word.Controls.Button deleteButton = new Microsoft.Office.Tools.Word.Controls.Button();

    • Proposed as answer by David_JunFeng Monday, September 26, 2016 2:13 PM
    Tuesday, September 20, 2016 9:04 AM