Sample macro to duplicate a page in Visio

For a Microsoft Visio 2000 version of this article, see 289859 .


This article explains how to duplicate a page, along with the shapes on that particular page, by using Microsoft Visual Basic for Applications. The sample code also provides an example of how to use the Page.PageSheet property to get access to a page's ShapeSheet cells.

More Information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Sample code

The following sample code duplicates a page.

Public Sub DuplicateActivePage()
Dim OriginalPage As Visio.Shape
Dim DuplicatePage As Visio.Shape

Dim ShapeDropX As Double
Dim ShapeDropY As Double

Set OriginalPage = ActivePage.PageSheet
ActiveWindow.SelectAll 'Select everything on the first page...
ActiveWindow.Group '...then group it all...
ActiveWindow.Copy '... then copy it to the clipboard.

ShapeDropX = ActiveWindow.Selection(1).Cells("PinX")
'Get the new Group's x position.

ShapeDropY = ActiveWindow.Selection(1).Cells("PinY")
'Get the new Group's y position.

Set DuplicatePage = ActiveDocument.Pages.Add.PageSheet
'Create the new page.

'Set attributes of the new page to be the same as the original page.
With DuplicatePage
.Cells("PageWidth") = OriginalPage.Cells("PageWidth")
.Cells("PageScale") = OriginalPage.Cells("PageScale")
.Cells("ShdwOffsetX") = OriginalPage.Cells("ShdwOffsetX")
.Cells("PageHeight") = OriginalPage.Cells("PageHeight")
.Cells("DrawingScale") = OriginalPage.Cells("DrawingScale")
.Cells("ShdwOffsetY") = OriginalPage.Cells("ShdwOffsetY")
.Cells("DrawingSizeType") = OriginalPage.Cells("DrawingSizeType")
.Cells("DrawingScaleType") = OriginalPage.Cells("DrawingScaleType")
.Cells("InhibitSnap") = OriginalPage.Cells("InhibitSnap")
.Cells("PlaceStyle") = OriginalPage.Cells("PlaceStyle")
.Cells("PlaceDepth") = OriginalPage.Cells("PlaceDepth")
.Cells("PlowCode") = OriginalPage.Cells("PlowCode")
.Cells("ResizePage") = OriginalPage.Cells("ResizePage")
.Cells("DynamicsOff") = OriginalPage.Cells("DynamicsOff")
.Cells("EnableGrid") = OriginalPage.Cells("EnableGrid")
.Cells("CtrlAsInput") = OriginalPage.Cells("CtrlAsInput")
.Cells("LineAdjustFrom") = OriginalPage.Cells("LineAdjustFrom")
.Cells("BlockSizeX") = OriginalPage.Cells("BlockSizeX")
.Cells("BlockSizeY") = OriginalPage.Cells("BlockSizeY")
.Cells("AvenueSizeX") = OriginalPage.Cells("AvenueSizeX")
.Cells("AvenueSizeY") = OriginalPage.Cells("AvenueSizeY")
.Cells("RouteStyle") = OriginalPage.Cells("RouteStyle")
.Cells("PageLineJumpDirX") = OriginalPage.Cells("PageLineJumpDirX")
.Cells("PageLineJumpDirY") = OriginalPage.Cells("PageLineJumpDirY")
.Cells("LineAdjustTo") = OriginalPage.Cells("LineAdjustTo")
.Cells("LineToNodeX") = OriginalPage.Cells("LineToNodeX")
.Cells("LineToNodeY") = OriginalPage.Cells("LineToNodeY")
.Cells("LineToLineX") = OriginalPage.Cells("LineToLineX")
.Cells("LineToLineY") = OriginalPage.Cells("LineToLineY")
.Cells("LineJumpFactorX") = OriginalPage.Cells("LineJumpFactorX")
.Cells("LineJumpFactorY") = OriginalPage.Cells("LineJumpFactorY")
.Cells("LineJumpCode") = OriginalPage.Cells("LineJumpCode")
.Cells("LineJumpStyle") = OriginalPage.Cells("LineJumpStyle")
.Cells("XRulerOrigin") = OriginalPage.Cells("XRulerOrigin")
.Cells("YRulerOrigin") = OriginalPage.Cells("YRulerOrigin")
.Cells("XRulerDensity") = OriginalPage.Cells("XRulerDensity")
.Cells("YRulerDensity") = OriginalPage.Cells("YRulerDensity")
.Cells("XGridOrigin") = OriginalPage.Cells("XGridOrigin")
.Cells("YGridOrigin") = OriginalPage.Cells("YGridOrigin")
.Cells("XGridDensity") = OriginalPage.Cells("XGridDensity")
.Cells("YGridDensity") = OriginalPage.Cells("YGridDensity")
.Cells("XGridSpacing") = OriginalPage.Cells("XGridSpacing")
.Cells("YGridSpacing") = OriginalPage.Cells("YGridSpacing")
End With

ActivePage.Paste 'Paste the copied group from the Clipboard.
ActiveWindow.Selection(1).Cells("PinX") = ShapeDropX
'Position the group.
ActiveWindow.Selection(1).Cells("PinY") = ShapeDropY
ActiveWindow.Selection(1).Ungroup 'Ungroup the group.

ActiveWindow.Page = OriginalPage.ContainingPage.Name
'Go back to the original page...

ActiveWindow.SelectAll '...then select the group...
ActiveWindow.Selection(1).Ungroup '...and ungroup it...
ActiveWindow.DeselectAll ' and then deselect all of the shapes.

MsgBox DuplicatePage.ContainingPage.Name & " has been created."
End Sub


For more information about how to use the sample code in this article, click the following article number to view the article in the Microsoft Knowledge Base:

277011 How to run sample code from Knowledge Base articles

Alternatively, visit the following Microsoft Web site:


Article ID: 290581 - Last Review: Aug 8, 2008 - Revision: 1