We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Is there a way to create a command button on every sheet to run a macro?

I have a template file I use but that is on Sheet1 and I create multiple sheets after opening the template file I have. I need to have a button somewhere on the file to allow the user to click and run a macro. Is there a way I can do this, or create a button on every sheet?

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for considering Aspose.

Well, As per the macros are concerned, I am afraid, Aspose.Cells doesn’t allow to create or manipulate the macros. To create a button on a sheet, Aspose.Cells.Shapes class provides a method named AddButton, which is used to add a button control to the worksheet. Please see the following Sample code which may help you get your desired results.

Sample Code:

//Create a new Workbook.

Workbook workbook = new Workbook();

//Get the first worksheet in the workbook.

Worksheet sheet = workbook.Worksheets[0];

//Add a new button to the worksheet.

Aspose.Cells.Button button = sheet.Shapes.AddButton(2, 0, 2, 0, 28, 80);

//Set the caption of the button.

button.Text = "Aspose";

//Set the Placement Type, the way the

//button is attached to the cells.

button.Placement = PlacementType.FreeFloating;

//Set the font name.

button.Font.Name = "Tahoma";

//Set the caption string bold.

button.Font.IsBold = true;

//Set the color to blue.

button.Font.Color = Color.Blue;

//Set the hyperlink for the button.

button.AddHyperlink("http://www.aspose.com/");

//Saves the file.

workbook.Save(@"d:\test\tstbutton.xls");

Also, please see the following link, which will provide you the better understanding of the use of different controls.

http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/working-with-controls.html

Also, please see the details about the members of the button class,

http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/aspose.cells.buttonmembers.html

Thank You & Best Regards,