0

我在下面提供了我的代码。现在,我的代码中有一个错误。我希望页码按顺序为每页列出 1、2、3、4、5,但它却像 5、5、5、5、5 一样列出。

可能是因为我添加的代码:

Dim PageNum As String
            PageNum = CStr(PageCnt)
            TOCEntry.Text = PageNum + " -------- " + PageObj.Name
 

这是完整的代码:

Option Explicit

Sub TableOfContents()
     ' creates a shape for each page in the drawing on the first page of the drawing
     ' then add a dbl-clk GoTo to each shape so you can double click and go to that Page
     
    Dim PageObj   As Visio.Page
    Dim TOCEntry  As Visio.Shape
    Dim CellObj   As Visio.Cell
    Dim PosY      As Double
    Dim PageCnt   As Double
     
     ' ActiveDocument.Pages.Count will give the number of pages, but we are interested
     ' the number of foreground pages
    PageCnt = 0
    For Each PageObj In ActiveDocument.Pages
        If PageObj.Background = False Then PageCnt = PageCnt + 1
    Next
     
     ' loop through all the pages
    For Each PageObj In ActiveDocument.Pages
        If PageObj.Background = False Then ' Only foreground pages
             
             ' where to put the entry on the page?
            PosY = (PageCnt - PageObj.Index) / 4 + 1
             
             ' draw a rectangle for each page to hold the text
            Set TOCEntry = ActiveDocument.Pages(1).DrawRectangle(1, PosY, 4, PosY + 0.25)
             
             
             
             
             ' write the page name in the rectangle
            Dim PageNum As String
            PageNum = CStr(PageCnt)
            TOCEntry.Text = PageNum + " -------- " + PageObj.Name
            
            
             
             ' add a link to point to the page to you can just go there with a Double Click
            Set CellObj = TOCEntry.CellsSRC(visSectionObject, visRowEvent, visEvtCellDblClick) 'Start
            CellObj.Formula = "GOTOPAGE(""" + PageObj.Name + """)"
             
        End If
    Next
     
     'Clean Up
    Set CellObj = Nothing
    Set TOCEntry = Nothing
    Set PageObj = Nothing
End Sub
4

1 回答 1

0

您已将 PageCnt 设置为非背景页面的数量,但随后将该总数用作页面索引。您不需要计算页数,因此删除第一个循环并将 PageCnt 初始设置为 1,然后在设置 CellObj.Formula 值后将其递增。

于 2021-04-09T19:17:39.107 回答