• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

LOOP That Stops Once No Matches Are Found

Looking for help with a loop that stops after searching entire document and finding zero matches.


I am using MS Word.


Other codes I have tried so far were either very slow, or they froze up on me.


The code I pasted below is much quicker and works fine;


However,


I will not always know how many times I need to loop, therefore I was hoping someone could please provide feedback on a loop that finishes once no more instances are found.


[pre]<br />
Sub SPECIFIED_LOOP()<br />
Application.ScreenUpdating = False<br />
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br />
Dim i As Integer<br />
i = 1<br />
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br />
Do Until i = 436 'I WAS HOPING I WOULDN'T HAVE TO SPECITY A NUMBER HERE<br />
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''</p>
<p>'my code is here, and I don't know if it's necessary for getting help so I will paste it below</p>
<p>'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br />
i = i + 1<br />
Loop<br />
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br />
Application.ScreenUpdating = True<br />
End Sub<br />



Here is the inserted code I was using in case this is necessary:


<br />
Selection.Find.ClearFormatting<br />
With Selection.Find<br />
.Text = "OPENBRACKET*.txt"<br />
.Replacement.Text = "ENDBRACKET"<br />
.Forward = True<br />
.Wrap = wdFindContinue<br />
.Format = False<br />
.MatchCase = False<br />
.MatchWholeWord = False<br />
.MatchAllWordForms = False<br />
.MatchSoundsLike = False<br />
.MatchWildcards = True<br />
End With<br />
Selection.Find.Execute<br />
Selection.EndKey Unit:=wdLine<br />
Selection.MoveLeft Unit:=wdCharacter, Count:=4<br />
Selection.TypeText Text:=")"<br />
[/pre]
 
Hi Indi Visual,


Since the number of line times is not definitive you can try to count the total lines in the document and then execute your loop accordingly.


Dim num As Long

num = ThisDocument.ComputeStatistics(wdStatisticLines, False)


If you are not able to resolve send me the sample file at sharma.vijay1-at-gmail.com


and I will do it for you.


HTH


~Vijay
 
Back
Top