2

I know I can draw a rectangle in Visio VBA using the Shape.DrawRectangle method, which is basically equivalent to using the "rectangle" tool in the manual UI. However, the Visio shapes panel also includes a "rounded rectangle". How can I draw a rounded rectangle using VBA?

I found that Excel has a kind of equivalent shape via msoShapeRoundedRectangle. However I couldn't find any way to create an msoShapeRoundedRectangle in Visio, so maybe it's Excel-specific. Other than that, I've found no information about this online, or in the documentation.

Adam Burley
  • 5,551
  • 4
  • 51
  • 72

2 Answers2

4

First, you draw a rectangle, and second, you make it rounded :)

Set shp = ActivePage.DrawRectangle(0, 0, 1, 1)
shp.Cells("Rounding").Formula = 0.1

A common advice here is to use macro recorder, do the steps manually, and look at the code generated.

--- update ---

if you want the "rounded rectangle" shape (a styled rectangle with a yellow handle), you could use something like the code below (the "rounded rectangle" shape lives in the "basic shapes" stencil, which is "Basic.vss" or "Basic.vssx", depending on your Visio version). You open that stencil and then drop the shape from that stencil to the page at given coordinates:

Set stencil = Application.Documents.OpenEx("Basic.vss", visOpenDocked)
ActivePage.Drop stencil.Masters("Rounded Rectangle"), 1, 1
Nikolay
  • 10,752
  • 2
  • 23
  • 51
  • Thank you for the tip! I am quite new to Visio VBA. I was not aware of ShapeSheets, and I was only vaguely aware of the macro recorder. Your macro recorder produced much cleaner code than mine (probably you are on a later version of Visio than me) so it was still helpful. – Adam Burley Sep 24 '18 at 20:31
  • Thanks, unfortunately I realised this only creates something which looks like a rounded rectangle, not an actual rounded rectangle. A rounded rectangle has an extra yellow control called "Adjust Corner Rounding". Also, I found a rectangle created with the "rounded rectangle tool" has a "Rounding" value of 0 – Adam Burley Sep 24 '18 at 20:46
  • This is a built-in shape ("a rectangle with a handle", that is). I have updated my answer to include that case. – Nikolay Sep 24 '18 at 21:54
1

Something like this?

Dim vsoShape As Visio.Shape
Set vsoShape = Application.ActiveWindow.Page.DrawRectangle(1#, 7.375, 3#, 6.375)
vsoShape.CellsSRC(visSectionObject, visRowLine, visLineRounding).FormulaU = "12.5 pt"

Edit: As @Nikolay noted: I used the macro recorder to generate this example.

Neil B
  • 2,096
  • 1
  • 12
  • 23
  • Thanks, unfortunately I realised this only creates something which looks like a rounded rectangle, not an actual rounded rectangle. A rounded rectangle has an extra yellow control called "Adjust Corner Rounding". Also, I found a rectangle created with the "rounded rectangle tool" has a "Rounding" value of 0 – Adam Burley Sep 24 '18 at 20:47