Word宏VBA查找特定样式/列表并转换为文本

时间:2017-03-10 20:58:28

标签: vba ms-word word-vba

我正在尝试使用Word宏来选择所有带有样式的文本" Number_List"并在其上调用.ConvertNumbersToText函数。我在查找列表或特定样式时遇到了麻烦。

Dim selBkUp As Range
Set selBkUp = ActiveDocument.Range(ActiveDocument.Range.Start, ActiveDocument.Range.End)
With ActiveDocument.Range.Find
    .Style = ActiveDocument.Styles("Number_List")
    .Forward = True
    .Wrap = wdFindContinue
    Dim SearchSuccessful As Boolean
    SearchSuccessful = .Execute

    If SearchSuccessful Then
        selBkUp.Select
        Selection.Range.ListFormat.ConvertNumbersToText
    Else
        ' code
    End If
End With

我选择整个文档并将所有列表编号转换为文本,但我只想选择具有该特定样式的文档,或者避免使用可能存在或不存在的其他5种样式。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

以下代码将搜索一种样式,如果找到,将转换为另一种样式。在此代码下面是另一个子例程,它将列出文档中找到的所有样式。

' From http://forums.codeguru.com/showthread.php?448185-Macro-to-Change-Styles-in-Word
' This code will search for a specified Style and convert that to another Style
Sub FindReplaceStyle()
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting

    With Selection.Find
         .Style = "Normal"                      ' Look for 'Normal'
         '.Text = ""
         .Replacement.Style = "Heading 1"       ' Change to 'Heading 1'
    '     .Replacement.Text = ""
         .Forward = True
         .Wrap = wdFindStop
         .Format = True
         .MatchCase = False
         .MatchWholeWord = False
         .MatchWildcards = False
         .MatchSoundsLike = False
         .MatchAllWordForms = False
    End With

    Do While Selection.Find.Execute()
        If Selection.Start = Selection.Paragraphs.First.Range.Start Then
            Selection.Style = "Heading 1"
        End If

        Selection.Collapse wdCollapseEnd
    Loop

End Sub

下面的代码将生成文档中找到的所有样式的列表。

此外,我在尝试使用“Number_List”

时收到错误消息
' Following code from: http://www.vbaexpress.com/forum/showthread.php?41125-How-to-get-all-the-applied-Paragraph-Styles-of-a-document
 Sub GetActiveStyles()
    Application.ScreenUpdating = False
    Dim RngStory As Range, oSty As Style, StrType As String, StrStyles As String
    With ActiveDocument
        For Each oSty In .Styles
            For Each RngStory In .StoryRanges
                With RngStory.Find
                    .ClearFormatting
                    .Text = ""
                    .Replacement.Text = ""
                    .Style = oSty.NameLocal
                    .Forward = True
                    .Wrap = wdFindStop
                    .Format = True
                    .Execute
                    If .Found Then
                        Select Case oSty.Type
                        Case wdStyleTypeCharacter: StrType = "Character"
                        Case wdStyleTypeList: StrType = "list"
                        Case wdStyleTypeParagraph: StrType = "Paragraph"
                        Case wdStyleTypeTable: StrType = "Table"
                        End Select
                        StrStyles = StrStyles & oSty.NameLocal & " (" & StrType & ")" & vbCr
                        Exit For
                    End If
                End With
            Next RngStory
        Next oSty
    End With
    Debug.Print StrStyles
    'MsgBox StrStyles
    Application.ScreenUpdating = True
End Sub