Share via


How to: Programmatically add shapes to a Visio document

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can add shapes to a Microsoft Office Visio document by retrieving the masters from a stencil and dropping the shapes on the active page.

For more information, see the VBA reference documentation for the Microsoft.Office.Interop.Visio.Documents.Add method, Microsoft.Office.Interop.Visio.Application.ActivePage property, and Microsoft.Office.Interop.Visio.Page.Drop method.

Add shapes to a Visio Document

To add shapes to a Visio document

  • With a document active, retrieve the masters from the Documents.Masters collection and drop the shapes on the active document. You can retrieve a master by using the index or master name.

    The following code example creates a blank Visio document, and then opens it with the Basic Shapes stencil docked. The code then retrieves several shapes and drops them on the active page.

    this.Application.Documents.Add("");
    
    Visio.Documents visioDocs = this.Application.Documents;
    Visio.Document visioStencil = visioDocs.OpenEx("Basic Shapes.vss",
        (short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenDocked);
    
    Visio.Page visioPage = this.Application.ActivePage;
    
    Visio.Master visioRectMaster = visioStencil.Masters.get_ItemU(@"Rectangle");
    Visio.Shape visioRectShape = visioPage.Drop(visioRectMaster, 4.25, 5.5);
    visioRectShape.Text = @"Rectangle text.";
    
    Visio.Master visioStarMaster = visioStencil.Masters.get_ItemU(@"5-Point Star 7");
    Visio.Shape visioStarShape = visioPage.Drop(visioStarMaster, 2.0, 5.5);
    visioStarShape.Text = @"Star text.";
    
    Visio.Master visioHexagonMaster = visioStencil.Masters.get_ItemU(@"Hexagon");
    Visio.Shape visioHexagonShape = visioPage.Drop(visioHexagonMaster, 7.0, 5.5);
    visioHexagonShape.Text = @"Hexagon text.";
    
    Me.Application.Documents.Add("")
    
    Dim visioDocs As Visio.Documents = Me.Application.Documents
    Dim visioStencil As Visio.Document = visioDocs.OpenEx("Basic Shapes.vss", CShort(Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenDocked))
    
    Dim visioPage As Visio.Page = Me.Application.ActivePage
    
    Dim visioRectMaster As Visio.Master = visioStencil.Masters("Rectangle")
    Dim visioRectShape As Visio.Shape = visioPage.Drop(visioRectMaster, 4.25, 5.5)
    visioRectShape.Text = "Rectangle text."
    
    Dim visioStarMaster As Visio.Master = visioStencil.Masters("5-Point Star 7")
    Dim visioStarShape As Visio.Shape = visioPage.Drop(visioStarMaster, 2.0, 5.5)
    visioStarShape.Text = "Star text."
    
    Dim visioHexagonMaster As Visio.Master = visioStencil.Masters("Hexagon")
    Dim visioHexagonShape As Visio.Shape = visioPage.Drop(visioHexagonMaster, 7.0, 5.5)
    visioHexagonShape.Text = "Hexagon text."
    

See also