Comment : copier et coller des formes dans un document Visio par programmeHow to: Programmatically copy and paste shapes in a Visio document

Vous pouvez copier des formes par programmation sur une page d’un document, et les coller dans une nouvelle page du même document.You can programmatically copy shapes on one page of a document and paste them into a new page in the same document. Vous pouvez choisir de les coller à l’emplacement par défaut (au centre de la fenêtre active) ou aux mêmes coordonnées que dans la page d’origine.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.

Copier et coller des formesCopy and paste shapes

Pour plus d’informations sur le modèle objet, consultez la documentation de référence VBA sur les méthodes Microsoft.Office.Interop.Visio.Shape.DrawRectangle, Microsoft.Office.Interop.Visio.Shape.DrawOval, Microsoft.Office.Interop.Visio.Shape.Copyet Microsoft.Office.Interop.Visio.Shape.Paste , ainsi que sur l’indicateur Microsoft.Office.Interop.Visio.VisCutCopyPasteCodes.visCopyPasteNormal .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.

Pour copier des formes au centre d’une autre pageTo copy shapes to the center of another page

  • L’exemple suivant montre comment copier les formes de la première page, et les coller au centre de la seconde 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
    

Copier et coller des formes avec les mêmes positionsCopy and paste shapes with the same positions

Pour plus d’informations sur le modèle objet, consultez la documentation de référence VBA sur les méthodes Microsoft.Office.Interop.Visio.Shape.DrawRectangle, Microsoft.Office.Interop.Visio.Shape.DrawOval, Microsoft.Office.Interop.Visio.Shape.Copyet Microsoft.Office.Interop.Visio.Shape.Paste , ainsi que sur l’indicateur Microsoft.Office.Interop.Visio.VisCutCopyPasteCodes.visCopyPasteNoTranslate .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.

Si vous avez besoin contrôler le format des informations collées et (éventuellement) établir un lien vers un fichier source (par exemple, un document Microsoft Office Word), utilisez la méthode PasteSpecial.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.

Pour copier des formes et des emplacements de forme vers une autre pageTo copy shapes and shape locations to another page

  • L’exemple suivant montre comment copier les formes de la première page, et les coller au centre de la seconde page avec leurs coordonnées d’origine.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
    

Voir aussiSee also

Solutions Visio Visio solutions
Présentation du modèle objet de Visio Visio object model overview
Utilisez des formes Visio Work with Visio shapes
Comment : ajouter par programmation des formes à un document VisioHow to: Programmatically add shapes to a Visio document