finding two consecutive paragraphs starting with the same word

N

Nomey

I Could use some help with this problem: how can I find two consecutive
paragraphs starting with the same word in a long, long glossary? I have
been trying to achieve this with range objects, but I can't get working.

Best,
Shirley Nomey

(ps: I already posted this question to m.p.w.vba but I got no answer;
this group seems to be much more active, so I give it another try; sorry
for cross-posting)
 
H

Helmut Weber

Hi Nomey,

like this:

Sub Macro11()
Dim rDcm As Word.Range
Dim lPrg As Long
Set rDcm = ActiveDocument.Range
For lPrg = 1 To rDcm.Paragraphs.Count - 1
If rDcm.Paragraphs(lPrg).Range.Words(1) = _
rDcm.Paragraphs(lPrg + 1).Range.Words(1) Then
' for testing
rDcm.Paragraphs(lPrg).Range.Words(1).Select
Stop
' 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"
 
N

Nomey

Hi Helmut,

Your code worked with a minor modification (only to mark the 1st word of
the second paragraph), but my document has almost 90000 paragraphs, and
it will take hours to get through it (I tested the macro on a small part
of it). Is there a way to speed up the process?

This is the code I've used:

Sub MarkDoubleFirstWords()

Dim rDcm As Word.Range
Dim lCnt As Long
Set rDcm = ActiveDocument.Range
For lCnt = 1 To rDcm.Paragraphs.Count - 1
If rDcm.Paragraphs(lCnt).Range.Words(1) = _
rDcm.Paragraphs(lCnt + 1).Range.Words(1) Then
rDcm.Paragraphs(lCnt + 1).Range.Words(1).HighlightColorIndex = wdRed
End If
Next

End Sub

============

S. Nomey
 
H

Helmut Weber

Hi Shirley,

optimizing for speed is a special challenge.
For 2000 paragraphs the code from the previous posting
needed here and now 41 seconds.

The following needed 1,5 seconds,
due to using constants as index, I guess.


Sub Test1003()
Dim rDcm As Word.Range
Dim lCnt As Long
Set rDcm = ActiveDocument.Range
With rDcm.Paragraphs
For lCnt = 1 To rDcm.Paragraphs.Count - 1
If .Item(1).Range.Words(1) = .Item(2).Range.Words(1) Then
.Item(2).Range.Words(1).HighlightColorIndex = wdYellow
End If
rDcm.start = .Item(2).Range.start
Next
End With
End Sub

And I got a feeling, there are still ways of improvement.
 
N

Nomey

Hi Helmut,

Great! I was thinking that switching off Word's Undo buffer might also
improve the performance. Do you happen to know whether that can be done
programmatically?
 
H

Helmut Weber

Hi Nomey,
Great! I was thinking that switching off Word's Undo buffer might also
improve the performance. Do you happen to know whether that can be done
programmatically?

it's always dangerous to say that something cannot be done,
but I don't know of a way. You have to check,
if clearing the undo buffer would speed up the code still more.
Activedocument.UndoClear

But I doubt that.
If very few words have to be marked, it would be of no use.
If very many words have to me marked, one would need
an additional variable, to clear the undo buffer after,
lets say, every 50th marking. But it was just the
elimination of the use of variable, which brought the speed gain.

As speed depends a lot on the structure of the data,
e.g. if 30,000 Word have to be highlighted or only 100,
it's hard to make a suggestion.

Welcome anybody who knows one trick more!

Though I think, there are only some seconds more to gain.

If you can, send me a sample document to both addresses.

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
"h.weber" & Chr$(64) & "mi-verlag.de"
 
N

Nomey

Hi Helmut,

The second method was blindingly fast: less than a minute to mark a
couple of hundreds of words in a list of 90,000 - without disabling or
clearing the undo buffer.

The list is proprietary, so I'm not allowed to 'divulge' it, but this is
the structure:

magnetization, mag·ne·ti·za·ti·on, [noun]
magnetize, mag·ne·ti·ze, [verb], mag·ne·ti·zed

etcetera, etcetera, etcetera.

Thanks again,
Shirley Nomey
 

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