Visio2000: Sample Macro to Add Pages and Number the Pages (275443)



The information in this article applies to:

  • Microsoft Visio 2000 Enterprise Edition
  • Microsoft Visio 2000 Professional Edition
  • Microsoft Visio 2000 Standard Edition
  • Microsoft Visio 2000 Technical Edition

This article was previously published under Q275443

SUMMARY

This article contains a sample Microsoft Visual Basic for Applications macro (Sub procedure) to add additional pages and page numbers to your Visio project.

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.
The following sample Visual Basic macro displays a Visual Basic for Applications UserForm and prompts the user to specify how many pages the user would like to add. The macro then adds the pages and numbers them in the bottom-left corner.

NOTE: A second macro is included in this sample to delete the pages.
Public Sub AddPages()

    Dim i As Integer
    Dim NumPages As Integer
    
    Dim pagsObj As Visio.Pages
    Dim pagObj As Visio.Page
    Dim shpsObj As Visio.Shapes
    Dim shpObj As Visio.Shape
    Dim txtObj As Object
        
    Set pagsObj = Visio.ActiveDocument.Pages
    
    ' Get the number of pages to add form the user.
    NumPages = InputBox("Enter Number of Pages to add to this document", "Add Pages...")
    
    ' If first page, then drop shape on first page
    If pagsObj.Count = 1 Then
        Set pagObj = pagsObj(1)
        Set shpsObj = pagObj.Shapes
        pagObj.DrawRectangle 0, 1, 1, 0
        Set shpObj = shpsObj(1)
        shpObj.Characters.AddField visFCatPage, visFCodePageNumber, visFmtNumGenNoUnits
    End If
        
    ' Add the pages to the active document, and drop the Page Shape
    ' master on each one.
    For i = pagsObj.Count + 1 To pagsObj.Count + NumPages
        pagsObj.Add
        Set pagObj = pagsObj(i)
        Set shpsObj = pagObj.Shapes
        pagObj.DrawRectangle 0, 1, 1, 0

        Set shpObj = shpsObj(1)
        shpObj.Characters.AddField visFCatPage, visFCodePageNumber, visFmtNumGenNoUnits
        
    Next i

End Sub

Public Sub DeletePages()

    Dim i As Integer
    Dim PageCount As Integer
    
    Dim pagsObj As Visio.Pages
    Dim pagObj As Visio.Page
    Dim shpsObj As Visio.Shapes
    Dim shpObj As Visio.Shape
        
    Set pagsObj = Visio.ActiveDocument.Pages
        
    PageCount = pagsObj.Count
    
    For i = PageCount To 2 Step -1
        Set pagObj = pagsObj(i)
        pagObj.Delete (1)
    Next i
    
End Sub
				

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbhowto KB275443