0
votes

I have a VBA code in word that iterates through two arrays of strings, finds the text in the document from one array, and replaces it with the corresponding string in the other array like so:

with application.activedocument.content.find
for i=1 to 100
.text=array1(i)
.replacement.text=array2(i)
.forward=true
.matchcase=true
.wrap=wdFindContinue
.matchwholeword=true
.matchwildcards=false
.matchallwordforms=false
.matchprefix=true
.matchsuffic=true
.matchsoundslike=false
.execute replace:=wdReplaceAll
next
end with

This replaces all the cases indiscriminately. Is there a way to include an if clause that makes it so it won't replace a word if it is the first word in a line? I'm not sure what the vba code for testing if something is the first word of a line.

Any help would be appreciated.

1
There isn't an If clause as such within the Find and Replace, but you can include special characters such as the Paragraph Mark or Manual Line Break character which can sometimes be useful. If your word appears in the middle of a paragraph, but happens to be at the start of a new line, do you want to replace it?Jane
I only want to skip if it is at the beginning so if the first word appears in the middle then I want to replace it.Carl
A word could be in the middle of the paragraph, but at the start of a line. For example my first comment contains the word "the" several times. The text is all one paragraph, but on my screen, the word "the" appears at the start of the second and third lines. Do you want to keep the existing word if it is at the beginning of the line, or only if it is at the beginning of the paragraph?Jane
Don't care about paragraphs. I just care if it is at the beginning of a line. It's about where the word is physically on the page, and not about any sort of language structure like a paragraph. ThanksCarl

1 Answers

1
votes

As there isn't a conditional in the Find and Replace to not replace words at the start of a line, there are 2 options that I can think of:

  • Executing each Find statement and then checking whether the selected word is at the start of a line before deciding whether to replace the text
  • Looping through each line of the document and performing a Find and Replace on all in that line except the first word

Neither of these are particularly elegant, but I went with the second option. I generally prefer to use a Range object rather than using Selection, but in this instance the Selection seemed easier to work with. Note that you'll need to turn off the wrap for this to work.

    For i = 1 To 100
        Selection.MoveStart WdUnits.wdStory, Count:=-1
        Do
            Selection.MoveRight unit:=wdWord, Count:=1
            Selection.EndKey unit:=wdLine, Extend:=wdExtend

            With Selection.Find
                .Text = array1(i)
                .Replacement.Text = array2(i)
                .Forward = True
                .MatchCase = True
                .MatchWholeWord = True
                .MatchWildcards = False
                .MatchAllWordForms = False
                .MatchPrefix = True
                .MatchSuffix = True
                .MatchSoundsLike = False
                .Execute Replace:=wdReplaceAll
            End With
            Selection.MoveStart unit:=wdLine, Count:=1
        Loop Until Selection.End = ActiveDocument.Range.End
    Next