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);
}
};
}
}
}
public bool IsProcessOpen(string name)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains(name))
{
return true;
}
}
return false;
}
if (IsProcessOpen("WinWord.exe"))
{
// do something
}
private void DeleteControl()
{
Microsoft.Office.Tools.Word.Controls.Button deleteButton =
this.Controls.AddButton(25, 75, 80, 30, "deleteButton");
deleteButton.Text = "Click to delete";
deleteButton.Click += new EventHandler(deleteButton_Click);
}
// Delete the clicked button.
void deleteButton_Click(object sender, EventArgs e)
{
Microsoft.Office.Tools.Word.Controls.Button clickedButton =
(Microsoft.Office.Tools.Word.Controls.Button)sender;
clickedButton.Delete();
}
try
{
Word.Application wdApp = new Word.Application();
wdApp.Visible = true;
if( wdApp.Visible == true)
{
deleteButton = new Microsoft.Office.Tools.Word.Controls.Button();
PIC_Barre.Controls.Add(deleteButton);
}
((Word.ApplicationEvents4_Event)wdApp).Quit += () =>
{
var method = (Action)(() => PIC_Barre.Controls.Remove(deleteButton));
if (deleteButton.InvokeRequired)
{
deleteButton.Invoke(method);
}
};
When I launch an instance of Word, a button is created directly and when I close the window of the instance , the button must be removed.
Word.Application wdApp = new Word.Application();
wdApp.Visible = true;
if( wdApp.Visible == true)
{
deleteButton = new Microsoft.Office.Tools.Word.Controls.Button();
PIC_Barre.Controls.Add(deleteButton);
}
((Word.ApplicationEvents4_Event)wdApp).Quit += () =>
{
PIC_Barre.Controls.Remove(deleteButton);
};
the mapping.Add(new KeyValuePair<int, Button>(processId, deleteButton));
I am getting this error: “No overload method for Add takes 1 argument”
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 .
Should your exit code not mirror your launch code: Remove button then exit Word?