Using ActiveX Controls in Excel with C#

This article guides on using ActiveX controls in Excel with C#. It has the details to set the IDE for development, a list of steps defining the process, and a sample code for adding Excel Active X control using C#. You will learn to add ActiveX control and then access it to update or read the control value.

Steps to Add ActiveX Controls in Excel using C#

  1. Set the IDE to use Aspose.Cells for .NET for working with ActiveX controls
  2. Create an Excel file using the Workbook class for adding ActiveX control
  3. Access the shapes collection from a sheet and call the AddActiveXControl() method to add controls
  4. Access the ActiveX control and send its linked cell
  5. For setting a specific property of a control, typecast the ActiveX control and set the value
  6. For updating or accessing a control, check its control type and update the desired value

These steps summarize how to use ActiveX controls in Excel using C#. Commence the process by accessing the shapes collection from a sheet in a Workbook and calling the AddActiveXControl() method in the ShapeCollection class. Provide the control type, target cell, and size of the ActiveX control, and link the control with a cell for manipulating its value.

Code to Add ActiveX Command Button using C#

using Aspose.Cells;
using Aspose.Cells.Drawing;
using Aspose.Cells.Drawing.ActiveXControls;
class Program
{
static void Main(string[] args)
{
new License().SetLicense("License.lic");
// Create workbook object
Workbook wb = new Workbook();
// Access first worksheet
Worksheet sheet = wb.Worksheets[0];
// Add Command Button ActiveX Control inside the Shape Collection
Shape s = sheet.Shapes.AddActiveXControl(ControlType.CommandButton, 4, 0, 4, 0, 100, 30);
// Access the ActiveX control object and set its linked cell property
ActiveXControl c = s.ActiveXControl;
c.LinkedCell = "A1";
// Add Toggle Button ActiveX Control inside the Shape Collection
Shape s1 = sheet.Shapes.AddActiveXControl(ControlType.ComboBox, 16, 0, 4, 0, 100, 30);
// Access the ActiveX control object and set its linked cell property
ActiveXControl c1 = s1.ActiveXControl;
c1.LinkedCell = "A4";
ComboBoxActiveXControl comboControl = (ComboBoxActiveXControl)c1;
comboControl.Value = "A sample value for the ComboBox";
// Save the workbook
wb.Save("Combo box with original value.xlsx");
foreach (var shape in sheet.Shapes)
{
// Access specific ActiveX Control and set its value
if (shape.ActiveXControl != null)
{
// Access Shape ActiveX Control
ActiveXControl control = shape.ActiveXControl;
// Check for the target type
if (control.Type == ControlType.ComboBox)
{
// Type cast ActiveXControl into ComboBoxActiveXControl and change its value
ComboBoxActiveXControl comboBoxActiveX = (ComboBoxActiveXControl)control;
comboBoxActiveX.Value = "A new value for the ComboBox";
}
}
}
// Save the workbook in xlsx format
wb.Save("AddActiveXControls_out.xlsx", SaveFormat.Xlsx);
}
}

This code demonstrates adding a Combo Box ActiveX control using C# in Excel sheet. The AddActiveXControl() method returns a reference to the newly added shape that you can use to set the linked cell and other properties such as font, color, shadow, and text alignment, to list a few. Similarly, if you typecast the shape to a specific control, you will get a vast range of properties for customizing the control.

This article has taught us how to add and access ActiveX controls in an Excel file. For adding an image in an Excel file, refer to the article on how to add image in Excel cell using C#.

 English