ZOrder in Excel

ZOrder in Excel

ZOrder:

It is a property which determines the order of shape or shapes. Moves the specified shape in front of or behind other shapes in the collection (that is, changes the shape’s position in the z-order). In Excel following constants determine the behavior of shapes order:

  • msoBringForwad : this constant brings selected shape on top of its immediate parent shape
  • msoBringToFront : this constant brings selected shape at the top by passing all
  • msoSendBackward : this constant sends selected shape to backward of immediate child shape
  • msoSendToBack : this constant sends selected shape at the bottom of all the shapes

Note: Use the ZOrderPosition property to determine a shape’s current position in the z-order.

In C# ZOrder is represent to method which takes MsoZOrderCmd enum as shown in below:

C# code example:

private void btnRotateShape_Click(object sender, RibbonControlEventArgs e)
{
	//get active worksheet reference
	excel.Worksheet oWorksheet = Globals.ThisAddIn.Application.ActiveSheet;
	
	//Retain selection
	object oSelection = (object)Globals.ThisAddIn.Application.Selection;
	
	//get selected shape name
	string shapeName=oSelection.GetType().InvokeMember("Name", BindingFlags.GetProperty, null, oSelection, null).ToString();
	
	//Retaing all available shepes in worksheet
	excel.Shapes oShapes = oWorksheet.Shapes;
	
	//Get selected shape object
	excel.Shape oShape = oShapes.Item(shapeName);
	
	//Bring shape to forward
	oShape.ZOrder(MsoZOrderCmd.msoBringForward);
}

VB.Net code example:

Private Sub ShapeForward_Click(sender As Object, e As RibbonControlEventArgs) Handles ShapeForward.Click
	'Bind worksheet object reference
	Dim oWorkSheet as excel.Worksheet
	oWorkSheet = Globals.ThisAddIn.Application.ActiveSheet

	'Bind selection reference
	dim oSelection as Object
	oSelection= Globals.ThisAddIn.Application.Selection

	'Get shape 
	Dim shapeName as String
	shapeName= oSelection.GetType().InvokeMember("Name",BindingFlags.GetProperty, Type.DefaultBinder,oSelection,Nothing).ToString()

	'Retaing all available shepes in worksheet
	Dim oShapes as excel.Shapes

	'Get selected shape object
	Dim oShape as excel.Shape
	oShape=oShapes.Item(shapeName)

	'Bring shape to forward
	oShape.ZOrder(MsoZOrderCmd.msoBringForward)
End Sub

VBA code example:

Selection.ShapeRange.ZOrder msoSendToBack

Let’s see result:

Before:

After:

Leave a Reply

Your email address will not be published.