【问题标题】:vba word: If Then find and replacevba word: If Then 查找并替换
【发布时间】:2018-03-24 17:31:07
【问题描述】:

在这段代码中,我想做以下事情:

  • 如果单词以字母 M 结尾,则替换为字母 N。
  • 如果单词以字母 N 结尾,则替换为字母 M。
  • 我不太了解如何使用 IF - Then 语句。 任何帮助将不胜感激。

    Sub find_end()   
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "[nm]>"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .MatchWildcards = True
            Selection.Find.Execute
            With Selection
            If Selection.Find.Found = n Then
                Selection.TypeText Text:=m
            ElseIf Selection.Find.Found = m Then
                Selection.TypeText Text:=n
            End If
    End Sub
    

【问题讨论】:

  • 您的代码需要进行多次更正(缺少两个 'End With';引号围绕 = "m" 和 = "n" 等。但是,您的代码不会遍历所有事件。看看以下作为如何查找所有事件的示例。如果您将其更改为“If Selection.Text = “n””,您的 if 将起作用,但您有“Selection.Find.Found”,它返回一个真/假。

标签: vba if-statement ms-word find replacewith


【解决方案1】:

我修改了在以下位置找到的代码:Repeating Microsoft Word VBA until no search results found 它会遍历文档并替换每个单词的最后一个字母(如果它是“m”或“n”)。注意如果您可能发现超过 2000 m 或 n 的代码,您可能希望删除该代码中的“循环检查”。

Option Explicit

' The following code adapted from: https://stackoverflow.com/questions/13465709/repeating-microsoft-word-vba-until-no-search-results-found
Sub SearchFN()
    Dim iCount  As Integer
    Dim lStart  As Long
    'Always start at the top of the document
    Selection.HomeKey Unit:=wdStory

    'find a footnote to kick it off
    With Selection.Find
        .ClearFormatting
        .Text = "[nm]>"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchFuzzy = False
        .MatchWildcards = True
        .Execute
    End With

    'Jump back to the start of the document.
    Selection.HomeKey Unit:=wdStory

    'If we find one then we can set off a loop to keep checking
    'I always put a counter in to avoid endless loops for one reason or another
    Do While Selection.Find.Found = True And iCount < 2000
        iCount = iCount + 1

        Selection.Find.Execute

        'On the last loop you'll not find a result so check here
        If Selection.Find.Found Then
            ' Exit if we start back at the beginning
            If Selection.Start < lStart Then
                Exit Do
            End If

            'Reset the find parameters
            With Selection.Find
                .ClearFormatting
                .Text = "[nm]>"
                If Selection.Text = "m" Then
                    Debug.Print "found 'm' at position: " & Selection.Start
                    Selection.Text = "n"
                ElseIf Selection.Text = "n" Then
                    Debug.Print "found 'n' at position: " & Selection.Start
                    Selection.Text = "m"
                End If
                lStart = Selection.Start
'                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchKashida = False
                .MatchDiacritics = False
                .MatchAlefHamza = False
                .MatchControl = False
                .MatchByte = False
                .MatchAllWordForms = False
                .MatchSoundsLike = False
                .MatchFuzzy = False
                .MatchWildcards = True
            End With
        End If
    Loop
End Sub

【讨论】:

  • 非常感谢,非常感谢您的帮助。
猜你喜欢
  • 2013-11-09
  • 2012-03-08
  • 1970-01-01
  • 2014-10-12
  • 2014-04-06
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
  • 2014-07-15
相关资源
最近更新 更多