Share via


How to: Copy and Paste Shapes in a Visio Document

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.

Copying and Pasting 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.

    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
    
    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);
    }
    

Copying and Pasting 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.

    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
    
    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);
    }
    

See Also

Tasks

How to: Add Shapes to a Visio Document

Concepts

Visio Object Model Overview

Working with Visio Shapes

Other Resources

Visio Solutions