vbaif-statementms-wordfindreplacewith

vba word: If Then find and replace


in this code, I want to do the following:


Solution

  • I modified the code found at: Repeating Microsoft Word VBA until no search results found and it will spin thru a document and replace the last letter (if it's an 'm' or 'n') of each word. NOTE there is a 'loop check in that code that you may want to remove if it's possible you may find over 2000 m or n's.

    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