Reputation: 49
I am trying to add a button in Excel using C#. I have the button added with the code below:
Shape rpsImportButton = worksheet.Shapes.AddFormControl(XlFormControl.xlButtonControl, 700, 35, 150, 22);
rpsImportButton.Name = "Genrate RPS Import Template";
The problem I am having is that the label on the button just appears as "Button 2". the .Name
attribute does not set the label. My question is, how do I set the button's label? I would think this is an easy problem, and I am just missing something small.
Thanks
Upvotes: 2
Views: 1010
Reputation: 666
This is something that took me for ever to get right, and many, many of the answers I tried and found through out the internet didn't help AT ALL. Solution:
Excel.Shape ButtonXX = ws.Shapes.AddFormControl(Excel.XlFormControl.xlButtonControl, 700, 35, 150, 22);
ButtonXX.OLEFormat.Object.Text = "Text I Want";
Enjoy :)
Upvotes: 3
Reputation: 6374
Please try:
// make sure that 'worksheet' is dynamic
dynamic rpsImportButton = worksheet.Butons.Add(700, 35, 150, 22);
rpsImportButton.Text = "Genrate RPS Import Template";
EDIT:
Replaced worksheet.Shapes.AddFormControl
by worksheet.Buttons.Add
Upvotes: 0