Hi Rodney
This was reasonably complex, but give this a try. It still needs a little
work on checking if the first word contains an uppercase character. But
this is 95% of what's required (all the hard stuff) and it would be useful
to you to work out the rest:
Public Sub NestedSearchWithInnerAction()
Dim rngOuterFind As Word.Range
Dim rngInnerFind As Word.Range
Dim rngWord As Word.Range
Dim rngInnerPart As Word.Range
' Setup outer find to search the entire document
Set rngOuterFind = ActiveDocument.Content
With rngOuterFind.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\<\<hd[0-9]\>\>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
' Do outer find...
Do While rngOuterFind.Find.Execute
' Range object starts at the end of the outer range
' and extends to the end of the paragraph
Set rngInnerFind = rngOuterFind.Duplicate
rngInnerFind.Collapse wdCollapseEnd
rngInnerFind.MoveEnd wdParagraph, 1
' MsgBox "Outer find: " & rngInnerFind.Text
' Setup inner find to search specified part of the paragraph
With rngInnerFind.Find
.Text = "<[A-Z]*>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
' So inner search
Set rngInnerPart = rngInnerFind.Duplicate
Do While rngInnerFind.Find.Execute
' Select the word starting with the character found
With rngInnerFind
Set rngWord = .Duplicate
' Exclude this word if its the first in the range
If rngWord.Start <> rngInnerPart.Start Then
rngWord.InsertBefore "["
rngWord.InsertAfter "]"
End If
' MsgBox "Inner find: " & rngWord.Text
' Setup the range where the search should resume
.Start = rngWord.End
.End = rngInnerPart.End
' Exit loop if we've hit the end of the search
If .Start = .End Then
Exit Do
End If
End With
Loop
Loop
End Sub
What it does:
1. It searches for "<<hdN>>", where N is a number between 0 and 9.
2. It then searches the rest of the paragraph for words starting with an
upper case character, if it finds a matching word it inserts "[]" around
it.
3. If the word following the "<<hdN>>" character sequence starts with an
upper case character the word is ignored. You need to do your thing here
4. It processes the entire document
NOTE: I've left the MsgBox statements in, but commented out to aid any
debugging you need to do. Do note it processes paragraphs rather than
lines.
Sample output:
<<hd1>>This is my
with a [Cap] in it
<<hd3>>This is [My] heading
The quick brown fox jumps over the lazy dog.
<<hd2>>It may [Be] something else [Hey]
<<hd9>>Her [Name] is [Destiny]
The quick brown fox jumps over the lazy dog.
<<hdX>>His Name is Fate
<<hd0>>Mine [Is] [Not] [To] [Reason] [Why], mine is [But] to [Do] or [DIE]
HTH + Cheers - Peter
I have SGML-like documents in which the headings are coded <<hd#>>,
where # is a number:
<<hd3>>This is my heading
I need to
--find each head (<<hd#>>) in the doc
--if any "word" in that head has a capital letter, surround it with
brackets
Before:
<<hd1>>This is my Heading with a Cap in it
After:
<<hd1>>This is my
with a [Cap] in it
It's not necessary for the first word unless there's a cap in the
middle.
<<hd1>>[DelMonte] packages peaches
I'm not quite sure where to begin. I can search for "<<hd" and use
Selection.Extend, or perhaps Selection.MoveEnd, but once I have the
heading selected, I'm at a bit of a loss as to how to test it.
Thanks.