PDA

View Full Version : [SOLVED:] Restrict Find to specific paragraph



srh124
09-09-2015, 08:11 AM
Hi all.
I've been around my code for some days and it sounds i can't make it to work, as expected.
I wanna iterate through paragraphs and find ENGLISH WORDS in each paragraph , then do some work on them. It's necessary for this operation to be done on ONE paragraph, not the whole document.

This is my code:


For Each p In doc1.Paragraphs
EnglishCount = 0

Dim rng As Range
Set rng = p.Range

rng.Find.ClearFormatting
rng.Find.LanguageID = wdEnglishUS
With rng.Find
.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True

End With


Do While rng.Find.Execute
EnglishCount = EnglishCount + 1

SelStartPos(EnglishCount) = rng.Start
SelEndPos(EnglishCount) = rng.End
MsgBox SelStartPos(EnglishCount) & " " & rng.Text
Loop

Next


The problem with my code is that it in every iteration of FOR..NEXT, it searches entire document and i want it to search in just specified paragraph
I've also used Selection object instead of Range, but no help.

Any help would be greatly appreciated. and Sorry for my english writing.
Regards

gmaxey
09-09-2015, 01:31 PM
It is hard to make sense of your code when you don't declare your variables.


Do While .Execute
If rng.InRange(p.Range) Then
Beep
Else
Exit Do
End If
Loop