Shapes in Excel Addin C#

Shapes in Excel Addin C#

Shapes:

Refers to drawing objects embedded/floating on the sheet. Programmer sometimes need to determine the shapes for manipulating them by code. You can add individual shapes to a chart, or add shapes on top of a SmartArt graphic to customize the chart or the graphic. For more information about using charts or SmartArt graphics in your documents.

List of shapes that we can have on a sheet:

  • Mixed Shapes
  • Auto Shapes
  • Callout
  • Chart
  • Comment
  • FreeForm
  • Group
  • OLE Objects
  • Form Controls
  • Line
  • Pictures
  • Placeholders
  • TextEffects
  • Media
  • TextBox
  • More… refer below method having entire list of available objects

Let’s write a method which will help you determining the available shapes in a Sheet and delete them:

public static void RemoveShapesFromSheet(Microsoft.Office.Interop.Excel.Workbook xlWorkbook)
{
	try
	{
		foreach (Microsoft.Office.Interop.Excel.Worksheet xlSheet in xlWorkbook.Sheets)
		{
			foreach (Microsoft.Office.Interop.Excel.Shape item in xlSheet.Shapes)
			{
				switch (item.Type)
				{
					case Microsoft.Office.Core.MsoShapeType.msoShapeTypeMixed:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoAutoShape:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoCallout:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoChart:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoComment:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoFreeform:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoGroup:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoEmbeddedOLEObject:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoFormControl:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoLine:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoLinkedOLEObject:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoLinkedPicture:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoOLEControlObject:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoPicture:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoPlaceholder:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoTextEffect:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoMedia:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoTextBox:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoScriptAnchor:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoTable:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoCanvas:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoDiagram:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoInk:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoInkComment:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoSmartArt:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoSlicer:
						item.Delete();
						break;
					case Microsoft.Office.Core.MsoShapeType.msoWebVideo:
						item.Delete();
						break;
					default:
						break;
				}
			}
		}
	}
	catch (Exception)
	{

		throw;
	}

}

Next>>Removing Macro from Excel File using C#

Leave a Reply

Your email address will not be published.