Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping Through a Word Document

Status
Not open for further replies.

Malc8179

MIS
Oct 8, 2007
28
GB
Hi Guys

Im new to coding in word i have an issue im trying to set up a loop that will find a section of text carry out a process, the issue i have is looping until the search text can't be found, I have set the start of the loop to Selection.Find.Found = False, this seems to stop the loop starting



Sub FindSNO()

Selection.HomeKey Unit:=wdStory

Do Until Selection.Find.Found = False
Selection.Find.ClearFormatting
With Selection.Find
.Text = "RFF+SNO'"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Selection.Find.Execute


Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.MoveRight Unit:=wdCharacter, Count:=8, Extend:=wdExtend
If Selection = "RFF+IFN:" Then
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=8
Selection.MoveRight Unit:=wdCharacter, Count:=6, Extend:=wdExtend
Selection.Copy

Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=8
Selection.MoveUp Unit:=wdLine, Count:=2
Selection.MoveRight Unit:=wdCharacter, Count:=7
Selection = ":"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Paste
Selection.MoveLeft Unit:=wdCharacter, Count:=14

strError = strError & "RFF+SNO' Error " & Selection.Range.Information(wdActiveEndPageNumber) & "-" & Selection.Range.Information(wdFirstCharacterLineNumber) & ", "
End If



Loop
End Sub






 
Well, since you haven't executed a search before you start your loop, "Selection.Find.Found" cannot yet be true. Perhaps you can set a Boolean variable to True, loop until that variable is false, and inside the loop set the value of that variable to "Selection.Find.Found".

_________________
Bob Rashkin
 
Thanks Bob

shortly after i posted this i spotted this fopar I changed the code all works ok but I get a message telling me that no more can be found do I want to start from the beginning again (Yes NO) Yes being the default, I have tired to hide alerts but as yes is the default I get stuck in the loop Ideally we would not like to see the message
 
Hi Malc8179!

1) Full stops are quite a useful thing: they increase readibnility and understandabilty.
:)

2) Check this line:
Code:
           .Wrap = wdFindAsk
This is why the message pops up. You specifically TELL word to ask you.
Change it to
Code:
           .Wrap = wdFindContinue
and it will stop appearing.
;-)

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
You have a lot of Selection. It is much better to not use Selection at all. Use Range.

That being said, you have a great deal of moving Selection around, down lines, expanding, extending, even doing two separate instructions (right after each other) that are the same except for the amount.
Code:
 Selection.MoveLeft Unit:=wdCharacter, Count:=1
 Selection.MoveLeft Unit:=wdCharacter, Count:=8
Why not just do a MoveLeft count of 9? Why a count of 1, and then - right after - a count of 8?

In any case, again, it would be better to not use Selection at all. If you describe exactly what you are trying to do, I am pretty sure it could be done with Range.

Regarding the looping until a not found...
Code:
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
   Do While .Execute (FindText:="RFF+SNO", _
                Forward:=True) = True
    [COLOR=red]'  do your stuff[/color red]
   Loop
End With
will search for "RFF+SNO" until it can not find any.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top