【问题标题】:Replace range text then insert a symbol替换范围文本然后插入一个符号
【发布时间】:2019-07-04 12:42:37
【问题描述】:

我正在尝试在页脚中插入文本,在插入的文本之后插入符号时遇到问题,以便在以后调用代码时轻松替换该文本。

在下面的代码中,调用 sub 后 Word 会崩溃,我怀疑它在 oRng.Collapse wdCollapseEndoRng.InsertSymbol 行失败,也许它未能退出循环?

Public Sub UpdateFooter()

    Dim objRange As Range
    Dim strCurrentView As String
    Dim objSection As Section
    Dim objHeaderFooter As HeaderFooter
    Dim rng As Word.Range

    ' Turn off screen updating
    Application.ScreenUpdating = False

    ' Loop through sections
    For Each objSection In ActiveDocument.Sections

        Set rng = objSection.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range

        Dim oRng As Word.Range

        Set oRng = rng

        oRng.Collapse wdCollapseStart

        ' Find existing U+61472 symbol, which means footer text has already been inserted
        With oRng.Find
            .ClearFormatting
            .Text = ChrW(61472)
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            Do While (.Execute = True) = True
                If .Found = True Then
                    ' Found, select first word through to and including the symbol
                    oRng.MoveStart wdWord, -1
                    oRng.MoveEnd wdCharacter, 1
                Else
                    ' Not found
                    oRng.MoveEnd wdStory, 1
                End If

                ' Insert new text
                oRng.Font.Name = "Arial"
                oRng.Font.Size = 8
                oRng.Text = "TEST_TEXT"

                ' Insert symbol after the new text so that we can replace in future
                'oRng.Collapse wdCollapseEnd
                'oRng.InsertSymbol Font:="Wingdings", CharacterNumber:=-4064, Unicode:=True
            Loop
        End With
    Next

    ' Set view back to Print View and enabled screen updating
    ActiveDocument.PrintPreview
    ActiveDocument.ClosePrintPreview
    Application.ScreenUpdating = True

End Sub

此代码将对符号 U+61472 执行查找,如果找到,将选择从行首到符号的文本并替换文本,如果未找到符号,则插入文本.

如果我删除了

oRng.Collapse wdCollapseEnd
oRng.InsertSymbol Font:="Wingdings", CharacterNumber:=-4064, Unicode:=True

页脚文本被插入,但没有符号,任何未来的保存都会将文本重新插入为重复而不是被替换。

如何在所选范围内插入文本,然后在插入的文本后添加符号?

【问题讨论】:

  • 能否提供完整的minimal reproducible example。这意味着从头到尾显示行为的完整Sub。当然,这将包括有关保存部分的信息:如何初始化保存?那时代码是如何被触发的?如果没有这些信息,就无法解决问题。
  • 我已更新问题以包含一个功能性子程序,该子程序演示了我正在经历的行为。如果您删除标识的两行,则 sub 将成功。
  • 它对我来说不会崩溃,但我确实将InsertSymbol 行更改为oRng.Text = ChrW(61472),因为这样更简单?但是您可能会发现使用 Bookmarks 而不是使用这种方法更容易/更快。
  • 我认为 Word 允许您为此使用书签。您有一个包含现有书签的文档,或者您使用 VBA 创建一个。然后,您可以覆盖书签。 docs.microsoft.com/en-us/office/vba/api/word.bookmarks

标签: vba ms-word


【解决方案1】:

我最终通过在 while 循环外插入符号解决了这个问题。

Public Sub UpdateFooter()
Dim objRange As Range
Dim strCurrentView As String
Dim objSection As Section
Dim objHeaderFooter As HeaderFooter
Dim rng As Word.Range

' Turn off screen updating
Application.ScreenUpdating = False

' Loop through sections
For Each objSection In ActiveDocument.Sections

    Set rng = objSection.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range

    Dim oRng As Word.Range

    Set oRng = rng

    oRng.Collapse wdCollapseStart

    ' Find existing U+61472 symbol, which means footer text has already been inserted
    With oRng.Find
        .ClearFormatting
        .Text = ChrW(61472)
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        Do While (.Execute = True) = True
            If .Found = True Then
                ' Found, select first word through to and including the symbol
                oRng.MoveStart wdWord, -1
                oRng.MoveEnd wdCharacter, 1
            Else
                ' Not found
                oRng.MoveEnd wdStory, 1
            End If

            ' Insert new text
            oRng.Font.Name = "Arial"
            oRng.Font.Size = 8
            oRng.Text = "TEST_TEXT"


        Loop
    End With
 Insert symbol after the new text so that we can replace in future
 oRng.Collapse wdCollapseEnd
 oRng.InsertSymbol Font:="Wingdings", CharacterNumber:=-4064, Unicode:=True

Next

' Set view back to Print View and enabled screen updating
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
Application.ScreenUpdating = True

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 2020-01-14
    • 2017-03-20
    • 2017-09-12
    • 2018-01-28
    相关资源
    最近更新 更多