如何在C#中关闭正在运行的MS Word实例

时间:2016-09-16 18:07:39

标签: c# windows process

我正在做一个应用程序,当我启动MS Word应用程序时,会创建一个按钮,当MS Word应用程序关闭时,该按钮将被删除。
所以这就是我将MS Word和按钮关联起来的作用......

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;
}

但我在这里收到错误mapping.Add(new KeyValuePair<int, Button>(processId, deleteButton));
我收到这个错误:“Add的加载方法没有1参数”......
我该怎么办?
这有什么解决方案吗?
谢谢。
编辑:
我修改了我的代码,但是我只删除了我创建的最后一个按钮,当我想删除其他的时候我不能...我为了知道不同变量的值而放置了断点。 但是当我创建2个按钮时,mapping.add中的计数总是等于1 ......这是正常的吗?
这是代码:

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
            {

            }

1 个答案:

答案 0 :(得分:0)

Word._Document document = this.Application.ActiveDocument;
document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);