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

时间:2018-09-21 19:11:20

标签: vba visio-vba

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

我查看是否存在诸如vzdVisioDocument.Pages.Sort之类的方法,但在文档中或通过互联网搜索都找不到。

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

3 个答案:

答案 0 :(得分:2)

所以这没有预期的那么痛苦。使用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

答案 1 :(得分:1)

我在另一条评论中提到了这一点,但是当我创建一些测试页面时,它总是在我运行页面时对其进行改组,因为我以这种方式实现它,我不认为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

答案 2 :(得分:0)

私人子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

结束子