遍历整个Word文档,使某些上标文本成为全文

问题描述 投票:0回答:1

我不知道如何使用查找和替换在Word文档中循环。

我曾尝试以多种不同方式使用每种,但找不到任何有效的方法。我是VBA的初学者,对在编程中使用对象了解不多。

Sub Find()

    Dim Rng As Range
    Dim Fnd As Boolean


    Set Rng = Selection.Range
    With Rng.Find
        .ClearFormatting
        .Execute FindText:="4th", Forward:=True, _
                 Format:=False, Wrap:=wdFindStop
        Fnd = .Found
    End With

    If Fnd = True Then
        With Rng
            .MoveStart wdCharacter, 1
            .Font.Superscript = True

        End With
    End If

    Do Until Fnd = False
        With Rng.Find
            .ClearFormatting
            .Execute FindText:="4th", Forward:=True, _
                     Format:=False, Wrap:=wdFindStop
            Fnd = .Found
        End With

        If Fnd = True Then
            With Rng
                .MoveStart wdCharacter, 1
                .Font.Superscript = True
            End With
        End If
    Loop

End Sub

我期望循环通过并更改单词文档中找到字符串'4th'的每个实例的后两位,但是它只会更改单词'4th'的第一个实例,然后结束程序。最终,我需要将1st,2nd,3rd等的最后2个字符的所有实例更改为上标。我找不到通配符来执行此操作,因此,如果可以使用通配符来执行此操作,请告诉我并包含代码。谢谢!

word-vba
1个回答
0
投票

下面的代码会将上标应用于任何序数后缀(st,nd,rd,th)

Public Sub MakeOrdinalSuffixesSuperscript()
' makes any of st, nd, rd, th superscripted text when preceded by a
' number and followed by a character not a number or letter

' find a (single number) followed by (two lowercaseletters) from the above
' followed by a (character that is not a letter or a number)
Const FIND_TEXT                               As String = "([0-9])([dhnrst]{2,2})([!0-9a-zA-Z])"
Dim mySearchRange                             As Word.Range

    ' there are a number of ways in which the document content can be selected
    ' this version I feel is best as it is explicit about which text story
    ' in which it is searching
    Set mySearchRange = ActiveDocument.StoryRanges(wdMainTextStory)

    With mySearchRange

        ' Set up the find object
        ' when used with a range the find object will remember the previous settings
        ' so if there is no change in what we search for the execute method
        ' can be called repeatedly without resetting all the find object parameters
        With .Find

            .ClearFormatting
            .Text = FIND_TEXT
            .Forward = True
            .Wrap = wdFindStop
            .MatchWildcards = True
            .Format = False

        End With

        Do While .Find.Execute
            ' mySearchRange is now the found text so all of the dot notations refer
            ' to mySearchRange

            ' if we didn't want to interfere with the search range we would dim a
            ' new search range variable and make a copy of the search range using
            ' the .duplicate method.

            ' Shrink the range to the two ordinal suffix characters, note the -1 for moveend
            .MoveStart unit:=wdCharacter, Count:=1
            .MoveEnd unit:=wdCharacter, Count:=-1

            ' apply superscript to the oridinal suffix characters
            .Font.Superscript = True

            ' reset the range to include the text from after mySearchRange to
            ' the end of the document
            .MoveStart unit:=wdCharacter, Count:=.Characters.Count + 1
            .End = ActiveDocument.StoryRanges(wdMainTextStory).End

        Loop

    End With

    Beep

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.