简体   繁体   English

列出“标题1”样式的所有实例

[英]List all instances of the “Heading 1” style

I'm trying to list every instance of the "Heading 1" style within my document (to eventually list them in a combobox on a form). 我试图列出文档中“标题1”样式的每个实例(最终将它们列出在表单的组合框中)。

The following code appears to find the instances of "Heading 1", as there are the correct number of entries listed within the Immediate Window, but .text is returning nothing. 下面的代码似乎可以找到“标题1”的实例,因为“立即窗口”中列出了正确数量的条目,但是.text不返回任何内容。

What am I doing wrong? 我究竟做错了什么? Thanks. 谢谢。

Dim blnFound As Boolean
Dim i as Integer

i = 1

With ThisDocument.Range.Find
    .Style = "Heading 1"

    Do
        blnFound = .Execute
        If blnFound Then
            Debug.Print i & " " & .Text
            i = i + 1
        Else
            Exit Do
        End If
    Loop
End With

I don't believe the object you have has a .Text property. 我不相信您拥有的对象具有.Text属性。 Selection has a .Text property. 选择具有.Text属性。 Try this: 尝试这个:

Sub FindHeadings()
' October 28, 2014
Dim blnFound As Boolean
Dim i As Integer
i = 1
' Set up the find conditions
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, count:=1

'Repeat while you find the style
blnFound = True
While blnFound
    With Selection.Find
        .Execute
        If .Found = True Then
                Debug.Print i & " " & Selection.Text
                i = i + 1
            ' Move to the next character
            Selection.MoveRight Unit:=wdCharacter, count:=1
        Else
            Debug.Print "Done"
            blnFound = False
        End If
    End With
Wend
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM