Partager via


How to: Programmatically copy and paste shapes in 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 programmatically copy shapes on one page of a document and paste them into a new page in the same document. You can choose to paste them into the default location (the center of the active window) or into the same coordinate locations as they had on the original page.

Copy and paste shapes

For details about the object model, see the VBA reference documentation for the Microsoft.Office.Interop.Visio.Shape.DrawRectangle, Microsoft.Office.Interop.Visio.Shape.DrawOval, Microsoft.Office.Interop.Visio.Shape.Copy, and Microsoft.Office.Interop.Visio.Shape.Paste methods and the Microsoft.Office.Interop.Visio.VisCutCopyPasteCodes.visCopyPasteNormal flag.

To copy shapes to the center of another page

  • The following example demonstrates how to copy the shapes from the first page and paste them into the center of the second page.

    this.Application.Documents.Add("");
    Visio.Page copyPage;
    Visio.Page pastePage;
    Visio.Shape rectangle = null;
    Visio.Shape oval = null;
    
    Visio.Pages visioPages = this.Application.ActiveDocument.Pages;
    
    visioPages.Add();
    
    try
    {
        copyPage = visioPages[1];
        rectangle = copyPage.DrawRectangle(1.1, 2.2, 4.5, 6.7);
        oval = copyPage.DrawOval(1, 8.75, 3.5, 6.25);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    
    try
    {
        pastePage = visioPages[2];
        rectangle.Copy(Visio.VisCutCopyPasteCodes.visCopyPasteNormal);
        pastePage.Paste(Visio.VisCutCopyPasteCodes.visCopyPasteNormal);
        oval.Copy(Visio.VisCutCopyPasteCodes.visCopyPasteNormal);
        pastePage.Paste(Visio.VisCutCopyPasteCodes.visCopyPasteNormal);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    
    Me.Application.Documents.Add("")
    Dim copyPage As Visio.Page
    Dim pastePage As Visio.Page
    Dim rectangle As Visio.Shape = Nothing
    Dim oval As Visio.Shape = Nothing
    
    Dim visioPages As Visio.Pages = Me.Application.ActiveDocument.Pages
    
    visioPages.Add()
    
    Try
        copyPage = visioPages(1)
        rectangle = copyPage.DrawRectangle(1.1, 2.2, 4.5, 6.7)
        oval = copyPage.DrawOval(1, 8.75, 3.5, 6.25)
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    End Try
    
    Try
        pastePage = visioPages(2)
        rectangle.Copy(Visio.VisCutCopyPasteCodes.visCopyPasteNormal)
        pastePage.Paste(Visio.VisCutCopyPasteCodes.visCopyPasteNormal)
        oval.Copy(Visio.VisCutCopyPasteCodes.visCopyPasteNormal)
        pastePage.Paste(Visio.VisCutCopyPasteCodes.visCopyPasteNormal)
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    End Try
    

Copy and paste shapes with the same positions

For details about the object model, see the VBA reference documentation for the Microsoft.Office.Interop.Visio.Shape.DrawRectangle, Microsoft.Office.Interop.Visio.Shape.DrawOval, Microsoft.Office.Interop.Visio.Shape.Copy, and Microsoft.Office.Interop.Visio.Shape.Paste methods and the Microsoft.Office.Interop.Visio.VisCutCopyPasteCodes.visCopyPasteNoTranslate flag.

If you need to control the format of the pasted information and (optionally) establish a link to a source file (for example, a Microsoft Office Word document), use the PasteSpecial method.

To copy shapes and shape locations to another page

  • The following example demonstrates how to copy the shapes from the first page and paste them into the second page with their original coordinate locations.

    this.Application.Documents.Add("");
    Visio.Page copyPage;
    Visio.Page pastePage;
    Visio.Shape rectangle = null;
    Visio.Shape oval = null;
    
    Visio.Pages visioPages = this.Application.ActiveDocument.Pages;
    
    visioPages.Add();
    
    try
    {
        copyPage = visioPages[1];
        rectangle = copyPage.DrawRectangle(1.1, 2.2, 4.5, 6.7);
        oval = copyPage.DrawOval(1, 8.75, 3.5, 6.25);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    
    try
    {
        pastePage = visioPages[2];
        rectangle.Copy(Visio.VisCutCopyPasteCodes.visCopyPasteNoTranslate);
        pastePage.Paste(Visio.VisCutCopyPasteCodes.visCopyPasteNoTranslate);
        oval.Copy(Visio.VisCutCopyPasteCodes.visCopyPasteNoTranslate);
        pastePage.Paste(Visio.VisCutCopyPasteCodes.visCopyPasteNoTranslate);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    
    Me.Application.Documents.Add("")
    Dim copyPage As Visio.Page
    Dim pastePage As Visio.Page
    Dim rectangle As Visio.Shape = Nothing
    Dim oval As Visio.Shape = Nothing
    
    Dim visioPages As Visio.Pages = Me.Application.ActiveDocument.Pages
    
    visioPages.Add()
    
    Try
        copyPage = visioPages(1)
        rectangle = copyPage.DrawRectangle(1.1, 2.2, 4.5, 6.7)
        oval = copyPage.DrawOval(1, 8.75, 3.5, 6.25)
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    End Try
    
    Try
        pastePage = visioPages(2)
        rectangle.Copy(Visio.VisCutCopyPasteCodes.visCopyPasteNoTranslate)
        pastePage.Paste(Visio.VisCutCopyPasteCodes.visCopyPasteNoTranslate)
        oval.Copy(Visio.VisCutCopyPasteCodes.visCopyPasteNoTranslate)
        pastePage.Paste(Visio.VisCutCopyPasteCodes.visCopyPasteNoTranslate)
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    End Try
    

See also