FreeFormBuilder in Excel VBA, C#, VB.Net example

FreeFormBuilder in Excel VBA, C#, VB.Net example

Freeform

Used to form Geometrical shapes in Excel. FreeFormbuilder is very useful object for the mathematicians to represent complex diagrams like timeline of a project. In this article I will be showcasing the code which will create a free form.

Approach

  • Use the BuildFreeform method of the Shapes object to return a FreeformBuilder object.
  • Use the AddNodes method to add nodes to the freeform.
  • Use the ConvertToShape method to create the shape defined in the FreeformBuilder object and add it to the Shapes collection.

C# code example

private void btnFreeFormBuilder_Click(object sender, RibbonControlEventArgs e)
{
	//Obtain current worksheet reference
	excel.Worksheet oWorksheet = Globals.ThisAddIn.Application.ActiveSheet;

	//Create freeformbuilder
	excel.FreeformBuilder oformBuilder = oWorksheet.Shapes.BuildFreeform(MsoEditingType.msoEditingCorner, 360, 200);

	//Add nodes
	oformBuilder.AddNodes(MsoSegmentType.msoSegmentCurve, MsoEditingType.msoEditingCorner, 380, 230, 400, 250, 550,400);
	oformBuilder.AddNodes(MsoSegmentType.msoSegmentCurve, MsoEditingType.msoEditingAuto, 480,200);
	oformBuilder.AddNodes(MsoSegmentType.msoSegmentLine, MsoEditingType.msoEditingAuto, 480, 400);
	oformBuilder.AddNodes(MsoSegmentType.msoSegmentLine, MsoEditingType.msoEditingAuto, 360, 200);

	//Convert to shape
	excel.Shape oShape= oformBuilder.ConvertToShape();

	//change properties
	oShape.Name = "MyShape";
	oShape.Title = "Testing";
}
}

VB.Net code example

Private Sub FreeFormBuilder_Click(sender As Object, e As RibbonControlEventArgs) Handles FreeFormBuilder.Click
	'Obtain current worksheet reference
	Dim oWorksheet As excel.Worksheet
	oWorksheet = Globals.ThisAddIn.Application.ActiveSheet

	'Create freeformbuilder
	dim oformBuilder As excel.FreeformBuilder
	oformBuilder = oWorksheet.Shapes.BuildFreeform(MsoEditingType.msoEditingCorner, 360, 200)

	'Add nodes
	oformBuilder.AddNodes(MsoSegmentType.msoSegmentCurve, MsoEditingType.msoEditingCorner, 380, 230, 400, 250, 550,400)
	oformBuilder.AddNodes(MsoSegmentType.msoSegmentCurve, MsoEditingType.msoEditingAuto, 480,200)
	oformBuilder.AddNodes(MsoSegmentType.msoSegmentLine, MsoEditingType.msoEditingAuto, 480, 400)
	oformBuilder.AddNodes(MsoSegmentType.msoSegmentLine, MsoEditingType.msoEditingAuto, 360, 200)

	'Convert to shape
	Dim oShape As excel.Shape
	oShape = oformBuilder.ConvertToShape()

	'change properties
	oShape.Name = "MyShape"
	oShape.Title = "Testing"
End Sub

VBA code example

Public Sub FreeFormBuilderExample()
    Dim oWorksheet As Worksheet
    Set oWorksheet = ActiveSheet
    
    With oWorksheet.Shapes.BuildFreeform(msoEditingCorner, 360, 200)
     .AddNodes msoSegmentCurve, msoEditingCorner, _
     380, 230, 400, 250, 450, 300
     .AddNodes msoSegmentCurve, msoEditingAuto, 480, 200
     .AddNodes msoSegmentLine, msoEditingAuto, 480, 400
     .AddNodes msoSegmentLine, msoEditingAuto, 360, 200
     .ConvertToShape
    End With
End Sub

Output

Next >> Excel Replace vs VBA Replace Function complete example

Leave a Reply

Your email address will not be published.