Need to find text only in paragraph

S

Sheila

Within a paragraph, I need to find text that is tagged by start and end tags, do something with the text, and keep looping for more text until no more found. So the user has the cursor somewhere in the paragraph. I am having problems setting the range object to the current paragraph, it keeps changing. Appreciate any help, I have spent too much time and losing all my hair

'move to beginning of paragrap
Selection.MoveUp unit:=wdParagraph, Count:=

'set range to be current paragrap
Set myRange = Selection.Paragraphs(1).Rang

'using line below from another help email I rea
Set selRange = myRange.Duplicat

'loop to find the start tag within the rang

While selRange.Find.Execute(FindText:=startTag, Forward:=True, Format:=False, Wrap:=wdFindStop

'use selection to find start tag and end tag, select evertyhing in-betwee
Selection.Find.Execute FindText:=startTag, Forward:=True, Format:=Fals
Selection.Exten
Selection.Find.Execute FindText:=endTag, Forward:=True, Format:=Fals

'got the text, do something with it ..
strkey = Selection.Range.Tex

'move cursor,
Selection.MoveRight wdCharacter,

Wen
 
P

Peter Hewett

Hi anonymous

The problem is within the second find. You need to use a range object for
the second Find so you don't mess with the Selection the outer Find is
using.

Check the following code, the layouts different and it's using wildcards,
but you'll see the important parts:

Public Sub NestedReplace()
Dim rngSubFind As Word.Range

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(~Bp~)*(~Bp~)"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Do While Selection.Find.Execute
Set rngSubFind = Selection.Range
'MsgBox rngSubFind.Text
With rngSubFind.Find
.Text = " "
.Replacement.Text = "^s"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
rngSubFind.Find.Execute Replace:=wdReplaceAll
Loop
End Sub

HTH + Cheers - Peter
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

See the article “Finding and replacing characters using wildcards” at:

http://word.mvps.org/FAQs/General/UsingWildcards.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
S

Sheila

Thanks for the link .... it does help me in cutting down the finds and specifying the start and end tags everytime. But I am still unable to get it to search only within the current paragraph. My code currently

Selection.MoveUp unit:=wdParagraph, Count:=

'find start and end tag
While Selection.Find.Execute(FindText:="\[*\]", Forward:=True, Format:=False, Wrap:=wdFindStop

strkey = Selection.Tex
'do something with selected text .....
Selection.EscapeKey 'lose the highlighted-bit part thingy of text ?
Selection.MoveRight wdCharacter,

Wen
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Use this, modified to do what you want with the found text

Dim myrange As Range
Set myrange = Selection.Paragraphs(1).Range
myrange.Select
Selection.Collapse wdCollapseStart
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="\[*\]", MatchWildcards:=True,
Wrap:=wdFindStop, Forward:=True) = True
If Selection.Range.Start > myrange.Start And Selection.Range.End
< myrange.End Then
Selection.Range.Font.Bold = True
End If
Loop
End With


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
S

Sheila

Thanks very much for all the help. I've ended up doing this:

Sub InsertLinkPara()
Dim tempIndex As Integer
Dim myRange As Range

Call initialiseSession

'goto beginning of paragraph and search until the end of the paragraph
Selection.MoveUp unit:=wdParagraph, Count:=1
Selection.Find.ClearFormatting

'have to set range
tempIndex = ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Paragraphs.Count
Set myRange = ActiveDocument.Paragraphs(tempIndex).Range

StartLoop:
With Selection.Find
.Text = "\[*\]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
While Selection.Find.Execute
'check that there are matching [ and ] in the selection
'Selection.MoveRight wdCharacter, 1
'If Selection.Find.Execute(FindText:="\[") Then
' End If


'check that we are still searching in the paragraph range
If Selection.InRange(myRange) Then
strkey = Selection.Text

Else

GoTo TheEnd
End If

Wend

TheEnd:

'not in current para anymore, take user back to para
Call Selection.Find.Execute(FindText:="\[*\]", Forward:=Back, Format:=False, Wrap:=wdFindStop, MatchWildcards:=True)
Selection.MoveRight wdCharacter, 1

If errorList <> "" Then
resp = MsgBox(errorList, vbOKOnly, "Missing references")
Else
MsgBox "Hyperlink conversion completed for " + CStr(Linkcount) + " references"
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top