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...
Here is the code:
- 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
- {
- }
But when I create 2 buttons, the count in the mapping.add is always equal to 1... Is it normal ?
Bikesh SrivastavaPosted Sep 20, 2016, 3:15 AM