none
Sample Code Request: Create a Custom Shape with text and properties set by default RRS feed

  • Question

  • Sorry, this is probably just me not knowing where to look for the answer, but I would like to create a plugin that allows me to easily create a shape in a document and add some text to it by default as well as set some other parameters (transparency, etc).

    Is there some sample code to build a VSTO Word Plugin that does something similar.  Ideally I would have a custom "Rectangle Tool" creation tool similar to the image attached.

    I can add a rectangle using the Shapes.AddShape, but have only figured out how to add a shape where the cursor is now.  I would like the user to be able to click the tool, have the pointer change (similar to when you create a shape now in the default way), and drag and drop a rectangle on the word document.

    Can someone point me to the right API's to do this?


    Monday, December 3, 2018 3:12 AM

Answers

  • Hi Johnson,

    Please refer to the following code:

    Dim doc As New Document()
    
    	Dim sec As Section = doc.AddSection()
    
    	Dim para1 As Paragraph = sec.AddParagraph()
    
    	 
    
    	Dim shape1 As ShapeObject = para1.AppendShape(50, 50, ShapeType.Heart)
    
    	 
    
    	shape1.FillColor = Color.Red
    
    	shape1.StrokeColor = Color.Red
    
    	shape1.HorizontalPosition = 200
    
    	shape1.VerticalPosition = 100
    
    	 
    
    	Dim shape2 As ShapeObject = para1.AppendShape(100, 100, ShapeType.Arrow)
    
    	 
    
    	shape2.FillColor = Color.Purple
    
    	shape2.StrokeColor = Color.Black
    
    	shape2.LineStyle = ShapeLineStyle.[Double]
    
    	shape2.StrokeWeight = 3
    
    	shape2.HorizontalPosition = 200
    
    	shape2.VerticalPosition = 200
    
    	 
    
    	Dim para2 As Paragraph = sec.AddParagraph()
    
    	Dim shapegr As ShapeGroup = para2.AppendShapeGroup(200, 400)
    
    	 
    
    	 
    
    	shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.Rectangle) With { _
    
    	    Key .Width = 500, _
    
    	    Key .Height = 300, _
    
    	    Key .LineStyle = ShapeLineStyle.ThickThin, _
    
    	    Key .StrokeColor = System.Drawing.Color.Blue, _
    
    	    Key .StrokeWeight = 1.5 _
    
    	})
    
    	shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.RightTriangle) With { _
    
    	    Key .Width = 500, _
    
    	    Key .Height = 300, _
    
    	    Key .VerticalPosition = 301, _
    
    	    Key .LineStyle = ShapeLineStyle.ThickThin, _
    
    	    Key .StrokeColor = System.Drawing.Color.Green, _
    
    	    Key .StrokeWeight = 1.5 _
    
    	})
    
    	shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.QuadArrow) With { _
    
    	    Key .Width = 500, _
    
    	    Key .Height = 300, _
    
    	    Key .VerticalPosition = 601, _
    
    	    Key .LineStyle = ShapeLineStyle.ThickThin, _
    
    	    Key .StrokeColor = System.Drawing.Color.Blue, _
    
    	    Key .StrokeWeight = 1.5 _
    
    	})
    
    	 
    
    	doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010)

    For more information, please see the following links:

    How to Insert Shape and shape group in Word Document in C#, VB.NET

    Custom shape object in MS Office

    Hopefully it helps you.

    Best Regards,

    Lina


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Tuesday, December 4, 2018 3:05 AM

All replies

  • Hi Johnson,

    Please refer to the following code:

    Dim doc As New Document()
    
    	Dim sec As Section = doc.AddSection()
    
    	Dim para1 As Paragraph = sec.AddParagraph()
    
    	 
    
    	Dim shape1 As ShapeObject = para1.AppendShape(50, 50, ShapeType.Heart)
    
    	 
    
    	shape1.FillColor = Color.Red
    
    	shape1.StrokeColor = Color.Red
    
    	shape1.HorizontalPosition = 200
    
    	shape1.VerticalPosition = 100
    
    	 
    
    	Dim shape2 As ShapeObject = para1.AppendShape(100, 100, ShapeType.Arrow)
    
    	 
    
    	shape2.FillColor = Color.Purple
    
    	shape2.StrokeColor = Color.Black
    
    	shape2.LineStyle = ShapeLineStyle.[Double]
    
    	shape2.StrokeWeight = 3
    
    	shape2.HorizontalPosition = 200
    
    	shape2.VerticalPosition = 200
    
    	 
    
    	Dim para2 As Paragraph = sec.AddParagraph()
    
    	Dim shapegr As ShapeGroup = para2.AppendShapeGroup(200, 400)
    
    	 
    
    	 
    
    	shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.Rectangle) With { _
    
    	    Key .Width = 500, _
    
    	    Key .Height = 300, _
    
    	    Key .LineStyle = ShapeLineStyle.ThickThin, _
    
    	    Key .StrokeColor = System.Drawing.Color.Blue, _
    
    	    Key .StrokeWeight = 1.5 _
    
    	})
    
    	shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.RightTriangle) With { _
    
    	    Key .Width = 500, _
    
    	    Key .Height = 300, _
    
    	    Key .VerticalPosition = 301, _
    
    	    Key .LineStyle = ShapeLineStyle.ThickThin, _
    
    	    Key .StrokeColor = System.Drawing.Color.Green, _
    
    	    Key .StrokeWeight = 1.5 _
    
    	})
    
    	shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.QuadArrow) With { _
    
    	    Key .Width = 500, _
    
    	    Key .Height = 300, _
    
    	    Key .VerticalPosition = 601, _
    
    	    Key .LineStyle = ShapeLineStyle.ThickThin, _
    
    	    Key .StrokeColor = System.Drawing.Color.Blue, _
    
    	    Key .StrokeWeight = 1.5 _
    
    	})
    
    	 
    
    	doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010)

    For more information, please see the following links:

    How to Insert Shape and shape group in Word Document in C#, VB.NET

    Custom shape object in MS Office

    Hopefully it helps you.

    Best Regards,

    Lina


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Tuesday, December 4, 2018 3:05 AM
  • Lina,

    Thank you.  This should get me where I need to go!

    Aaron

    Tuesday, December 4, 2018 3:18 AM