使用 VBA 以编程方式对 Visio 文档中的页面进行排序

Programatically sort pages in a Visio Document using VBA

有谁知道使用 VBA 按字母顺序对 Visio 页面进行排序的方法吗?

我查看是否存在 vzdVisioDocument.Pages.Sort 之类的方法,但在文档中或通过互联网搜索没有找到任何内容。

我是否需要使用 Application.ActiveDocument.Pages.ItemU("Page Name").Index 属性 编写自己的排序函数?好像是录制动作宏建议的方法

所以这并不像预期的那么痛苦。将 vzdVisioDocument 作为已定义的 Visio.Document:

' Make a collection of titles to iterate through
Dim colPageTitles As Collection
Set colPageTitles = New Collection

Dim intPageCounter As Integer
For intPageCounter = 1 To vzdVisioDocument.Pages.Count
    colPageTitles.Add vzdVisioDocument.Pages.Item(intPageCounter).Name
Next intPageCounter

' For each title in the collection, iterate through pages and find the appropriate new index
Dim intPageIndex As Integer
Dim varPageTitle As Variant
For Each varPageTitle In colPageTitles
    For intPageIndex = 1 To vzdVisioDocument.Pages.Count
        ' Check to see if the title comes before the index's current page title
        If StrComp(varPageTitle, vzdVisioDocument.Pages.Item(intPageIndex).Name) < 0 Then
            ' If so, set the new page index
            vzdVisioDocument.Pages.ItemU(varPageTitle).Index = intPageIndex
            Exit For
        End If
    Next intPageIndex
Next varPageTitle

' Clean up
Set colPageTitles = Nothing

我在另一条评论中提到了这一点,但是当我制作一些测试页面时,它总是在我 运行 时将页面乱七八糟,因为我的实现方式,我不相信Exit For 应该在里面。

由于个人喜好以及 for 循环的顺序,我还交换了与 StrCompare 的比较。

Sub PageSort()

    Dim titlesColl As Collection
    Set titlesColl = New Collection

    Dim i As Long
    For i = 1 To ActiveDocument.Pages.Count
        titlesColl.Add ActiveDocument.Pages.Item(i).Name
    Next i

    Dim title As Variant
    For i = 1 To ActiveDocument.Pages.Count
        For Each title In titlesColl
            If StrComp(ActiveDocument.Pages.Item(i).Name, title, vbTextCompare) < 0 Then
                ActiveDocument.Pages.Item(title).index = i
            End If
        Next title
    Next i

    Set titlesColl = Nothing

End Sub

Private Sub reorderPages()

Dim PageNameU() As String

Dim isBackgroundPage As Boolean
Dim vsoPage As Visio.Page
Dim vsoCellObj As Visio.Cell

'// Get All Pages
Dim i As Integer
For Each vsoPage In ActiveDocument.Pages

    i = i + 1
    ReDim Preserve PageNameU(i)
    PageNameU(i) = vsoPage.NameU

Next vsoPage

For i = 1 To UBound(PageNameU)

    Set vsoPage = vsoPages.ItemU(PageNameU(i))
    Set vsoCellObj = vsoPage.PageSheet.Cells("UIVisibility")

    isBackgroundPage = vsoPage.Background

    '// Make foreground page to set page index
    If isBackgroundPage = True Then
        vsoCellObj.FormulaU = visUIVNormal
        vsoPage.Background = False
    End If

    vsoPage.Index = NumNonAppSysPages + i

    '// Set to background page
    If isBackgroundPage = True Then
        vsoCellObj.FormulaU = visUIVHidden
        vsoPage.Background = True
    End If

Next i

结束子