View Single Post
 
Old 04-09-2020, 11:03 AM
PrincessApril PrincessApril is offline Windows 10 Office 2019
Competent Performer
 
Join Date: Nov 2019
Posts: 102
PrincessApril is on a distinguished road
Default How to loop once through document

Hi all,

I have a macro that works well, and I want to have it repeat by going through the document one time, from the first line to the last line (the number of lines will always be different in the document at hand).

I can't repeat forever or it will never stop (because the word that it finds will still be in the document). The macro finds the word "invited," jumps to the beginning of the line, and overwrites the first character with the waving hand emoji. I want that to happen one time each for any line in the document that contains the word "invited."

Code:
Sub Invited()

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "invited"
        .Replacement.Text = " "
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = True
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.HomeKey Unit:=wdLine
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    
    Dim strW As String
    strW = ChrW(55357) & ChrW(56395)
    Selection.TypeText Text:=strW

    
End Sub
I tried putting this at the bottom but it didn't type the emoji in the second instance (I was testing in a document with two positive instances):

Code:
Dim lastPos As Long
lastPos = -1
Do While Selection.Find.Execute = True
    If lastPos > Selection.Start Then Exit Do
    Selection.EndKey Unit:=wdLine
    Selection.TypeParagraph
Loop
Any ideas? Thank you so much for any time and assistance you can provide!
Reply With Quote