Search code examples
vbavisio

How do I draw a rounded rectangle in Visio VBA?


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.


Solution

  • 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