I am working on creating a VSTO addin for MS Word. I am trying to add multiple buttons to a word document with a loop. The code works fine if I just add one button, when I try to add multiple buttons I get the following error:
System.Runtime.InteropServices.COMException
HResult=0x80004005
Message=Error HRESULT E_FAIL has been returned from a call to a COM component.
Source=<Cannot evaluate the exception source>
StackTrace:Cannot evaluate the exception stack trace
Here is the code:
private void cb_create_click(object sender, RibbonControlEventArgs e)
{
Document vstoDocument = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
int pos = 0;
for (int i = 0; i<3; i++)
{
Microsoft.Office.Tools.Word.Controls.Button button = new Microsoft.Office.Tools.Word.Controls.Button();
button = vstoDocument.Controls.AddButton(Globals.ThisAddIn.Application.ActiveDocument.Range(pos, pos), 20, 20, "button_" + pos);
button.Text = "+";
button.BackColor = System.Drawing.Color.Azure;
button.Click += Button_Click;
pos++;
}
}
private void Button_Click(object sender, EventArgs e)
{
//other code goes here.
}
User contributions licensed under CC BY-SA 3.0