Processing paragraphs with find

C

Conny Roloff

Hi,

with VBScript I process all paragraphs of a doc with the following code:

1 For Each parag In myDoc.Paragraphs
2 If parag.Range.Information(wdWithInTable) = True then
3 WScript.Echo "--- This paragraph is part of a table"
4 End If
5 myText = parag.Range.Text
6 ' ... do something with myText ...
7 Next

This code works but it is slow. I read that it is much faster with find
to walk thru all paragraphs but neither I could find an example (only
hints) nor I could find it out by myself.

Can somebody help? Many thanks in advance.

Happy New Year
` | ´
- * -
´ | `

--Conny
 
H

Helmut Weber

Hi Conny,

VBScript? This group is about VBA.

How about excluding anything that is not in a table at first, like

Dim oTbl As Table
Dim oPrg As Paragraph
For Each oTbl In ActiveDocument.Tables
For Each oPrg In oTbl.Range.Paragraphs
' do something with prg
oPrg.Range.Select ' for testing
Next
Next

But paragraphs in cells are different from ordinary paragraphs,
as every cell is in a way a paragraph defined by the end of cell mark,
and may contain other paragraphs of this kind ¶.
And there is the end of row mark in addition.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
C

Conny Roloff

Hi, Helmut,

thanks for your answer.

Helmut Weber wrote:
[...]> VBScript? This group is about VBA.

Right, but my problem is more related to the word object model and there
is no group for VBscript.
How about excluding anything that is not in a table at first, like

I want to process *all* paragraphs and then handle the ones which are
part of a table in a special way.

--Conny
 
H

Helmut Weber

Hi Conny,

if you want to process all paragraphs (in the main story),
then your approach was the right one.
Though Your code snippet didn't do anything
to paragraphs, that were not in a table.
So I excluded them from being processed.

What is it, that you want to do?
And how does your doc look?
100 pages? 200 Tables?

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/





Hi, Helmut,

thanks for your answer.

Helmut Weber wrote:
[...]> VBScript? This group is about VBA.

Right, but my problem is more related to the word object model and there
is no group for VBscript.
How about excluding anything that is not in a table at first, like

I want to process *all* paragraphs and then handle the ones which are
part of a table in a special way.

--Conny
 
C

Conny Roloff

Hi, Helmut,

thanks again for your answer!

I thought I could improve the performance by using find instead of the
range property since it seems to be very slow. First I noticed that the
performance of my code was acceptable (1 or 2 seconds) with

' Needs about 2 seconds
For Each parag In myDoc.Paragraphs
With parag
WScript.Echo .Range.Text ' just for demonstration
End With
Next

But it needed 56 (!) seconds when I add the test whether the paragraph
is part of a table:

' Needs about 56 seconds
For Each parag In myDoc.Paragraphs
With parag
If .Range.Information(wdWithInTable) = True then
' do something
End If
WScript.Echo .Range.Text ' just to do something
End With
Next

Helmut Weber wrote:
[...]
What is it, that you want to do?
And how does your doc look?
100 pages? 200 Tables?
[...]

That's about the size of my docs.

The docs contain Questions and Answers but are structured differently
because they have different authors: Some are structured just with
paragraphs, others have tables where the questions in the left column
and answer in the right column. I want to extract the questions and the
assigned answers and output it in a concrete XML format.

I want to analyze first which kind of structure the doc has (table based
or only paragraphs) and then create the XML file.

I wonder how .Range.Information() needs so much time. Any ideas?

--Conny
(from Munich)
 
H

Helmut Weber

Hi Conny,
I wonder how .Range.Information() needs so much time.
Any ideas?

well, ranges in tables are something special,
as I think Word organizes tables internally in a linear way.
The may not be 2 dimensional arrays really,
but need a conversion each time from 1 dimensional
to 2 dimensional data. But that is mere speculation.
Selection, however, is sometimes, not to say always, faster in tables.
Here is what I found out, for 10 pages and 10 tables:

Sub TestSlow()
Dim bInT As Boolean ' in Table
Dim oPrg As Paragraph
Dim lTim As Long
lTim = Timer
Application.ScreenUpdating = False
For Each oPrg In ActiveDocument.Paragraphs
bInT = oPrg.Range.Information(wdWithInTable)
Next
MsgBox Timer - lTim ' 22 Seconds
End Sub
' ---
Sub TestFast()
Dim bInT As Boolean ' in Table
Dim oPrg As Paragraph
Dim lTim As Long
lTim = Timer
Application.ScreenUpdating = False
For Each oPrg In ActiveDocument.Paragraphs
oPrg.Range.Select
bInT = Selection.Information(wdWithInTable)
Next
MsgBox Timer - lTim ' 1 Second
End Sub

Greetings from Landsberg am Lech
Bavaria, a region in Europe
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
C

Conny Roloff

Hi, Helmut,

Helmut Weber wrote:
[...]
Sub TestFast()
Dim bInT As Boolean ' in Table
Dim oPrg As Paragraph
Dim lTim As Long
lTim = Timer
Application.ScreenUpdating = False
For Each oPrg In ActiveDocument.Paragraphs
oPrg.Range.Select
bInT = Selection.Information(wdWithInTable)
Next
MsgBox Timer - lTim ' 1 Second
End Sub

That helps a lot! I got it down to 4 seconds instead of 56!

Danke, Helmut.

--Conny
 

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