1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| 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
{
} |
Partager