0

我在几个 Word 文档中有几个书签,这些书签需要在所有书签之前和之后添加空格,其中一个或两个当前不存在单个空格。我只想能够解析当前文件。

我已经尝试了几种方法来做到这一点,其中几种方法会创建无限循环。

使用以下代码,我取得了一定程度的成功,但是它在此过程中创建了一个无限循环。我尝试浏览 Bookmark 对象,依次选择每个对象并在前后添加一个空格,这会导致将空格放在书签内,或者它会忽略空格应该放在的位置并将其放在后面。

我有一个在文档上运行的宏,它显示书签并将其放置在大于和小于符号之间,例如“ ««bookmarkname»»”,以使其更易于解析。

这是我的代码:

Sub new_test()
    Dim sT As String
    Dim boo As Boolean
    boo = False

    Selection.Find.ClearFormatting
    With Selection.Find
         .Text = "««*»»[ ]"
        .Forward = False
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
         Do While .Execute
            With Selection
                'sT = Selection.Text

                If (boo = False) Then
                    MsgBox "Added a character after bookmark"
                    Selection.InsertAfter (" ")
                    boo = True
                End If
            End With
            boo = False
        Loop
    End With
End Sub
4

1 回答 1

0

好的 - 解决了。也许它会证明对某人有用。

在我运行这个之前,我在文档上运行了另一个函数,它显示了所有的书签,并在它们周围放置了更多和更少的符号,如下所示:"««BOOKMARKNAME»»"

Sub bookmarks_ensure_space_beforeAfter()

    ' Before we can do any work, we need a list of bookmarks from the document
    Dim bmks As Variant
    bmks = create_array_of_bookmark_names() ' array of bookmark names

    ' This Assumes that there will not be more than 1000 bmks in the array fetched from the Word Doc

    For i = 0 To 1000

        If (bmks(i) <> "") Then
            ' if the 'bmk' is not null then process it
            ' there are likely to be several 100 that are empty

            Dim wrd As String
            Dim rng As Range

            Call select_a_string("««" & bmks(i) & "»»") ' select the bookmark
            wrd = "««" & bmks(i) & "»»"
            Set rng = Selection.Range

            ' now move the cursor two places the left of the bookmark
            Selection.MoveLeft Unit:=wdCharacter, count:=2
            ' now select the character infront of the cursor (which is now the character infront of the bmk)
            Selection.MoveRight Unit:=wdCharacter, count:=1, Extend:=wdExtend
            If (Selection.Text <> " ") Then
                ' if this character now selected is not a space - add one
                rng.InsertBefore " "
            End If

            ' now move the cursor to the right of the bookmark (using it's length as a character limit)
            Selection.MoveRight Unit:=wdCharacter, count:=Len(wrd) + 1
            ' due to bookmarks being fiddly, recreate the same bmk directly after the original
            With ActiveDocument.Bookmarks
                .Add Range:=Selection.Range, Name:=bmks(i)
                .DefaultSorting = wdSortByName
                .ShowHidden = False
            End With

            ' now we have a new bmk, select the character directly after the bmk)
            Selection.MoveRight Unit:=wdCharacter, count:=1, Extend:=wdExtend

            If (Selection.Text <> " ") Then
                ' if this character now selected is not a space - add one
                rng.InsertAfter " "
            End If

        End If
    Next
End Sub

Function create_array_of_bookmark_names() As Variant
    ' This function creates an array of bookmarks in the document and returns them as an array
    Dim array_of_bmk(1000) As Variant
    Dim c As Integer
    c = 0
    For Each mBookmark In ActiveDocument.Bookmarks()
        array_of_bmk(c) = mBookmark.Name
        c = c + 1
    Next
    ' now return this array
    create_array_of_bookmark_names = array_of_bmk

End Function
Sub select_a_string(str)
    ' This finds and selects a string of characters
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = str
        '.Replacement.Text = ""
        .Forward = True
        .MatchCase = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
End Sub
于 2012-12-04T15:35:35.963 回答