Hi All,

I've been trying to figure out there is a way to select an entire paragraphs
based on a keyword within the paragraphs (e.g., I'd like to change the
entire paragraphs containing the keyword "^pCHAPTER" [paragraph mark +
uppercase-word chapter].
Is there a way to do this via Find&Replace or another way.
Thanks so much, rgds, JS

Re: How to format whole paragraph if it contains a keyword? by Greg

Greg
Sun Aug 06 13:23:43 CDT 2006

Not sure I understand exactly what you are trying to find, but if it is
literrally ^pCHAPTER then try:

Sub ScracthMacro()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "^^pCHAPTER"
.Execute
If .Found Then
oRng.Expand wdParagraph
oRng.Select
End If
End With
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


JS wrote:
> Hi All,
>
> I've been trying to figure out there is a way to select an entire
> paragraphs based on a keyword within the paragraphs (e.g., I'd like
> to change the entire paragraphs containing the keyword "^pCHAPTER"
> [paragraph mark + uppercase-word chapter].
> Is there a way to do this via Find&Replace or another way.
> Thanks so much, rgds, JS



Re: How to format whole paragraph if it contains a keyword? by Helmut

Helmut
Sun Aug 06 13:39:45 CDT 2006

Hi JS,

you are talking about _two paragraphs_.
No paragraph can contain another paragraph.
But you probably mean a paragraph starting with "CHAPTER".

Have a look at this one:

Sub Test5041()
' stepping through in single step mode
' in the VBA editor [F8]
Dim oPrg As Paragraph
For Each oPrg In ActiveDocument.Paragraphs
If Left(oPrg.Range.Text, 7) = "CHAPTER" Then
oPrg.Range.Select ' what now?
End If
Next
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"



Re: How to format whole paragraph if it contains a keyword? by JS

JS
Sun Aug 06 14:15:49 CDT 2006

Hi Greg and Helmut:

First, thanks so much for replying. I'm sorry if I was not clear - let me
try to be clearer: I'd like to change all entire paragraphs, that contain
the word CHAPTER (upper case), to Arial Narrow. Is the function "oRng.Expand
wdParagraph" or something similar in Find&Replace (i.e. by finding CHAPTER
[matchcase + find whole words selected] and if found EXPAND this selection
to the entire paragraph containing the found text)? Or does F&R NOT have
this capacity and this has to done by a macro?

Again, thanks a lot for yoru help - rgds, JS


"Helmut Weber" <nbhymsjxdgcn@mailinator.com> wrote in message
news:2adcd2tqk5di8l20q7rd1k82ggfu15hpae@4ax.com...
> Hi JS,
>
> you are talking about _two paragraphs_.
> No paragraph can contain another paragraph.
> But you probably mean a paragraph starting with "CHAPTER".
>
> Have a look at this one:
>
> Sub Test5041()
> ' stepping through in single step mode
> ' in the VBA editor [F8]
> Dim oPrg As Paragraph
> For Each oPrg In ActiveDocument.Paragraphs
> If Left(oPrg.Range.Text, 7) = "CHAPTER" Then
> oPrg.Range.Select ' what now?
> End If
> Next
> End Sub
>
> --
> Greetings from Bavaria, Germany
>
> Helmut Weber, MVP WordVBA
>
> Win XP, Office 2003
> "red.sys" & Chr$(64) & "t-online.de"
>
>



Re: How to format whole paragraph if it contains a keyword? by Helmut

Helmut
Sun Aug 06 14:22:22 CDT 2006

Like this:

Sub Test5041xxx()
Dim oPrg As Paragraph
For Each oPrg In ActiveDocument.Paragraphs
If InStr(oPrg.Range.Text, "CHAPTER") > 0 Then
oPrg.Range.Font.Name = "Arial Narrow"
End If
Next
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Re: How to format whole paragraph if it contains a keyword? by JS

JS
Sun Aug 06 15:12:43 CDT 2006

Hi Helmut - thanks!!! This did the trick - Tks, JS

"Helmut Weber" <nbhymsjxdgcn@mailinator.com> wrote in message
news:9agcd2du8eihe34tb83lg9mom9u9mfb0h6@4ax.com...
> Like this:
>
> Sub Test5041xxx()
> Dim oPrg As Paragraph
> For Each oPrg In ActiveDocument.Paragraphs
> If InStr(oPrg.Range.Text, "CHAPTER") > 0 Then
> oPrg.Range.Font.Name = "Arial Narrow"
> End If
> Next
> End Sub
>
> --
> Greetings from Bavaria, Germany
>
> Helmut Weber, MVP WordVBA
>
> Win XP, Office 2003
> "red.sys" & Chr$(64) & "t-online.de"



Re: How to format whole paragraph if it contains a keyword? by Greg

Greg
Sun Aug 06 16:46:02 CDT 2006

Helmut,

Yes that works, but isn't it inefficient to look at every paragraph for a
string? Seems we should find the string then do the deed. Something like
this:

Sub ScracthMacro()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "CHAPTER"
While .Execute
If .Found Then
With oRng
.Expand wdParagraph
.Font.Name = "Arial Narrow"
.Collapse wdCollapseEnd
End With
End If
Wend
End With
End Sub

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Helmut Weber wrote:
> Like this:
>
> Sub Test5041xxx()
> Dim oPrg As Paragraph
> For Each oPrg In ActiveDocument.Paragraphs
> If InStr(oPrg.Range.Text, "CHAPTER") > 0 Then
> oPrg.Range.Font.Name = "Arial Narrow"
> End If
> Next
> End Sub



Re: How to format whole paragraph if it contains a keyword? by HelmutWeber

HelmutWeber
Mon Aug 07 02:30:02 CDT 2006

Hi Greg,

hmm...

Often, the answers here depend on one's personal working situation.
As I usually edit very small and very simple docs
and have a lightning fast PC in the office,
I don't ponder much over questions of speed.

I'll see, whether I can check how much time either method needs,
unless someone of the experts here knows for sure which is faster.

Have a nice day.

--
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000 (german versions)




Re: How to format whole paragraph if it contains a keyword? by Greg

Greg
Mon Aug 07 05:45:19 CDT 2006

Helmut,

Understand and agree that in most cases there is probably no concern with
efficiency. However, if one where processing say War and Peace it seems
there could be a noticeable difference. Only mentioned to stimulate thought
and discussion ;-)


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Helmut Weber wrote:
> Hi Greg,
>
> hmm...
>
> Often, the answers here depend on one's personal working situation.
> As I usually edit very small and very simple docs
> and have a lightning fast PC in the office,
> I don't ponder much over questions of speed.
>
> I'll see, whether I can check how much time either method needs,
> unless someone of the experts here knows for sure which is faster.
>
> Have a nice day.



Re: How to format whole paragraph if it contains a keyword? by HelmutWeber

HelmutWeber
Mon Aug 07 07:33:02 CDT 2006

Hi Greg,

> Only mentioned to stimulate thought and discussion.

Naturally. :-)
And I'm glad you did.

I created a sample doc of 138 pages, containing
1000 paragraphs, of which 310 contain "chapter".
All these 310 paragraphs are in the second half on the doc.

And what do you say?

Sub ScracthMacro()
Dim lngTim1 As Long
Dim lngTim2 As Long
lngTim1 = GetTickCount
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "CHAPTER"
While .Execute
If .Found Then
With oRng
.Expand wdParagraph
.Font.Name = "Arial Narrow"
.Collapse wdCollapseEnd
End With
End If
Wend
End With
lngTim2 = GetTickCount
MsgBox lngTim2 - lngTim1 ' 750; 703; 719 msecs
End Sub
' --------------------------------------------
Sub Test5041xxx()
Dim lngTim1 As Long
Dim lngTim2 As Long
lngTim1 = GetTickCount
Dim oPrg As Paragraph
For Each oPrg In ActiveDocument.Paragraphs
If InStr(oPrg.Range.Text, "CHAPTER") > 0 Then
oPrg.Range.Font.Name = "Arial Narrow"
End If
Next
lngTim2 = GetTickCount
MsgBox lngTim2 - lngTim1 '469; 468; 468 msecs
End Sub

Ha!

For putting "CHAPTER" in the paragraphs, I used:

Sub Zufall()
Dim lCnt As Long
Dim pCnt As Long
Randomize
For lCnt = 1 To 1000
pCnt = Int((1000 - 1 + 1) * Rnd + 1)
If pCnt > 500 Then
ActiveDocument.Paragraphs(pCnt).Range.Words(2) = "CHAPTER"
End If
Next
End Sub

Enjoy!

--
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000 (german versions)










Re: How to format whole paragraph if it contains a keyword? by Greg

Greg
Mon Aug 07 08:15:02 CDT 2006

Helmut,

Your novel has short chapters ;-). I tested using 4000 paragraphs
divided into 20 chapters of roughly 200 paragraphs each.

Using Find: .12 seconds
Using Instr: .53 seconds

Comparing our tests it appears clear that manipulating the range will
take longer per individual occurance but that the real varialbe for
determining the more efficient method is the unknown number occurances
within the total paragraph count.

Interesting.


Helmut Weber wrote:
> Hi Greg,
>
> > Only mentioned to stimulate thought and discussion.
>
> Naturally. :-)
> And I'm glad you did.
>
> I created a sample doc of 138 pages, containing
> 1000 paragraphs, of which 310 contain "chapter".
> All these 310 paragraphs are in the second half on the doc.
>
> And what do you say?
>
> Sub ScracthMacro()
> Dim lngTim1 As Long
> Dim lngTim2 As Long
> lngTim1 = GetTickCount
> Dim oRng As Range
> Set oRng = ActiveDocument.Range
> With oRng.Find
> .Text = "CHAPTER"
> While .Execute
> If .Found Then
> With oRng
> .Expand wdParagraph
> .Font.Name = "Arial Narrow"
> .Collapse wdCollapseEnd
> End With
> End If
> Wend
> End With
> lngTim2 = GetTickCount
> MsgBox lngTim2 - lngTim1 ' 750; 703; 719 msecs
> End Sub
> ' --------------------------------------------
> Sub Test5041xxx()
> Dim lngTim1 As Long
> Dim lngTim2 As Long
> lngTim1 = GetTickCount
> Dim oPrg As Paragraph
> For Each oPrg In ActiveDocument.Paragraphs
> If InStr(oPrg.Range.Text, "CHAPTER") > 0 Then
> oPrg.Range.Font.Name = "Arial Narrow"
> End If
> Next
> lngTim2 = GetTickCount
> MsgBox lngTim2 - lngTim1 '469; 468; 468 msecs
> End Sub
>
> Ha!
>
> For putting "CHAPTER" in the paragraphs, I used:
>
> Sub Zufall()
> Dim lCnt As Long
> Dim pCnt As Long
> Randomize
> For lCnt = 1 To 1000
> pCnt = Int((1000 - 1 + 1) * Rnd + 1)
> If pCnt > 500 Then
> ActiveDocument.Paragraphs(pCnt).Range.Words(2) = "CHAPTER"
> End If
> Next
> End Sub
>
> Enjoy!
>
> --
> Greetings from Bavaria, Germany
> Helmut Weber, MVP WordVBA
> "red.sys" & chr(64) & "t-online.de"
> Word 2002, Windows 2000 (german versions)