Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-09-2020, 11:03 AM
PrincessApril PrincessApril is offline How to loop once through document Windows 10 How to loop once through document Office 2019
Competent Performer
How to loop once through document
 
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
  #2  
Old 04-09-2020, 12:41 PM
Seti Seti is offline How to loop once through document Windows 10 How to loop once through document Office 2019
Novice
 
Join Date: Apr 2020
Posts: 2
Seti is on a distinguished road
Default

Hi,


It can be repeated forever if you move to the end of the line after making the edit, I think. The code below works for me, anyways:


Code:
Sub Invited()


    'Go to the beginning of the document
    Selection.HomeKey Unit:=wdStory
    
    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

    While 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
    

        'Without line this it runs forever

        Selection.EndKey Unit:=wdLine
        
    Wend
    
End Sub
I'm guessing the reason it kept going forever before was that the Selection kept on jumping to the start of the line, before the previous thing it found, and then went on to find the previous thing it found, again, and so on!
Reply With Quote
  #3  
Old 04-09-2020, 02:44 PM
PrincessApril PrincessApril is offline How to loop once through document Windows 10 How to loop once through document Office 2019
Competent Performer
How to loop once through document
 
Join Date: Nov 2019
Posts: 102
PrincessApril is on a distinguished road
Default

Awesome, thank you!
Reply With Quote
  #4  
Old 04-10-2020, 01:48 AM
Seti Seti is offline How to loop once through document Windows 10 How to loop once through document Office 2019
Novice
 
Join Date: Apr 2020
Posts: 2
Seti is on a distinguished road
Default

You're welcome
Reply With Quote
  #5  
Old 04-12-2020, 06:06 AM
gmaxey gmaxey is offline How to loop once through document Windows 10 How to loop once through document Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

You could also use a range variable:

Code:
Sub Invited()
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "invited"
    .Wrap = wdFindStop
    While .Execute
      oRng.Select
      Selection.HomeKey Unit:=wdLine
      Selection.Characters.First = ChrW(55357) & ChrW(56395)
      oRng.Collapse wdCollapseEnd
    Wend
  End With
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Tags
loop macro

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
vba for next loop phoenixmoto Word VBA 4 11-06-2018 01:59 PM
How to loop once through document Loop thru document and create bookmarks jeffreybrown Word VBA 3 09-22-2018 06:04 AM
Loop true all opned word document elbartje Word VBA 1 06-12-2017 05:27 PM
VB for Word - Need macro to loop each line in a document NDeane Word VBA 5 12-22-2015 08:12 PM
How to loop once through document Loop through files and Copy Table Row and Paste into Different Document spiderman1369 Word VBA 2 10-15-2014 08:30 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:23 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft