Word performance problems

M

Mcollette

I have a fairly complex macro that combs a document for acronyms and
their definitions, then writes them in a new document.

The macro uses the find object to look for words consisting of two or
more capital letters. When it finds a word, it performs some checks to
see whether the word should be ignored. It looks for bold formatting,
whether the word is in a table, and whether the word appears in a string
of commonly capitalized words that we use (uses the InStr comparison to
accomplish the latter.) Any of these conditions causes it to ignore the
word and move on.

If it doesn't skip the word, it then checks to see whether the word is
inside parentheses. If it is, it uses the find object again to search
for capitalized words in front of the acronym, and records those words
as the definition. The acronyms and definitions are stored in two long
strings, and at the end of the macro, it pulls the information from
those strings to write in the new documents.

For a 94-page document, it's taking about 1 minute and 30 seconds to
run. Some of the documents we work with are going to be much larger than
this, and I can only imagine that the process will slow even more the
further it gets into a document.

I've dropped some Document.UndoClear lines in the loops, and that might
have helped a little, but I think the biggest problem is repaginating.
Even with code that switches the document to normal view and turns
background repagination off, Word is still repaginating while the macro
runs. I'm not sure what code is triggering this.

I've tried running it with the application invisible, but that doesn't
seem to help much. Also, I don't really want to keep the application
invisible because I'm giving the user the option of being prompted when
the macro encounters an acronym. The acronym is highlighted so the user
can see it in context, decide whether it should be in the table, and
then press Yes or No.

Any advice on improving speed?
 
J

Jay Freedman

The big problem occurs when you use Selection.Find to do the searches. That
forces Word to move the insertion point/selection through the document (even
with screen updating turned off), which is what's triggering the
repagination.

Instead, declare a Range object, set it equal to the document's range, and
use that object's .Find member to do all searching and replacement. Use
another Range object to find the defining term. Here's some "air code":

Dim oRg As Range
Dim oDefRg As Range

Set oRg = oDoc.Range
With oRg.Find
.Text = "[A-Z]{2,}"
.MatchWildcards = True
.Formatting = True
' etc.
While .Execute
' at this point oRg points to the found text;
' decide whether to replace the occurrence...
' if so, then
Acro(x, 0) = oRg.Text

' get the definition
Set oDefRg = oRg.Duplicate
' whatever you're doing to find the definition,
' do it with oDefRg.Find
Acro(x, 1) = oDefRg.Text

' avoid finding the same occurrence again:
oRg.Collapse wdCollapseEnd
Wend
End With

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
M

Mcollette

I was already using the range object to search. I've gone back through
and made sure that any use of the selection object has been removed, and
it's still repaginating. I have no idea what's triggering it.


The big problem occurs when you use Selection.Find to do the searches.
That
forces Word to move the insertion point/selection through the document
(even
with screen updating turned off), which is what's triggering the
repagination.

Instead, declare a Range object, set it equal to the document's range,
and
use that object's .Find member to do all searching and replacement.
Use
another Range object to find the defining term. Here's some "air
code":

Dim oRg As Range
Dim oDefRg As Range

Set oRg = oDoc.Range
With oRg.Find
.Text = "[A-Z]{2,}"
.MatchWildcards = True
.Formatting = True
' etc.
While .Execute
' at this point oRg points to the found text;
' decide whether to replace the occurrence...
' if so, then
Acro(x, 0) = oRg.Text

' get the definition
Set oDefRg = oRg.Duplicate
' whatever you're doing to find the definition,
' do it with oDefRg.Find
Acro(x, 1) = oDefRg.Text

' avoid finding the same occurrence again:
oRg.Collapse wdCollapseEnd
Wend
End With

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: 'Home' (http://word.mvps.org)
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so
all may benefit.
I have a fairly complex macro that combs a document for acronyms and
their definitions, then writes them in a new document.

The macro uses the find object to look for words consisting of two or
more capital letters. When it finds a word, it performs some checks to
see whether the word should be ignored. It looks for bold formatting,
whether the word is in a table, and whether the word appears in a
string of commonly capitalized words that we use (uses the InStr
comparison to accomplish the latter.) Any of these conditions causes
it to ignore the word and move on.

If it doesn't skip the word, it then checks to see whether the word is
inside parentheses. If it is, it uses the find object again to search
for capitalized words in front of the acronym, and records those words
as the definition. The acronyms and definitions are stored in two long
strings, and at the end of the macro, it pulls the information from
those strings to write in the new documents.

For a 94-page document, it's taking about 1 minute and 30 seconds to
run. Some of the documents we work with are going to be much larger
than this, and I can only imagine that the process will slow even
more the further it gets into a document.

I've dropped some Document.UndoClear lines in the loops, and that
might have helped a little, but I think the biggest problem is
repaginating. Even with code that switches the document to normal
view and turns background repagination off, Word is still
repaginating while the macro runs. I'm not sure what code is
triggering this.

I've tried running it with the application invisible, but that doesn't
seem to help much. Also, I don't really want to keep the application
invisible because I'm giving the user the option of being prompted
when the macro encounters an acronym. The acronym is highlighted so
the user can see it in context, decide whether it should be in the
table, and then press Yes or No.

Any advice on improving speed?
 
T

Tony Jollans

Firstly, how, exactly, do you know that repagination is happening?

Secondly, it is difficult to give general pointers about performance beyond
the fairly generic (such as Jay has already given regarding use of the
Selection). Your description of what the code does does not include any
Undo-able actions until right at the very end, so you saying you have
removed Undo.Clear statements makes me suspicious about whether you may have
other unnecessary code, or whether you may be doing more than you say.
Without seeing your code it is difficult to speculate but nothing you
describe ought to take very long.

Finally you don't say what version of Word you are using, but there are some
innocuous statements that perform very poorly in Word 2007 - although,
again, nothing in your description would lead me to believe you were using
any of the ones I know about.

--
Enjoy,
Tony

www.WordArticles.com

Mcollette said:
I was already using the range object to search. I've gone back through
and made sure that any use of the selection object has been removed, and
it's still repaginating. I have no idea what's triggering it.


The big problem occurs when you use Selection.Find to do the searches.
That
forces Word to move the insertion point/selection through the document
(even
with screen updating turned off), which is what's triggering the
repagination.

Instead, declare a Range object, set it equal to the document's range,
and
use that object's .Find member to do all searching and replacement.
Use
another Range object to find the defining term. Here's some "air
code":

Dim oRg As Range
Dim oDefRg As Range

Set oRg = oDoc.Range
With oRg.Find
.Text = "[A-Z]{2,}"
.MatchWildcards = True
.Formatting = True
' etc.
While .Execute
' at this point oRg points to the found text;
' decide whether to replace the occurrence...
' if so, then
Acro(x, 0) = oRg.Text

' get the definition
Set oDefRg = oRg.Duplicate
' whatever you're doing to find the definition,
' do it with oDefRg.Find
Acro(x, 1) = oDefRg.Text

' avoid finding the same occurrence again:
oRg.Collapse wdCollapseEnd
Wend
End With

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: 'Home' (http://word.mvps.org)
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so
all may benefit.
I have a fairly complex macro that combs a document for acronyms and
their definitions, then writes them in a new document.

The macro uses the find object to look for words consisting of two or
more capital letters. When it finds a word, it performs some checks to
see whether the word should be ignored. It looks for bold formatting,
whether the word is in a table, and whether the word appears in a
string of commonly capitalized words that we use (uses the InStr
comparison to accomplish the latter.) Any of these conditions causes
it to ignore the word and move on.

If it doesn't skip the word, it then checks to see whether the word is
inside parentheses. If it is, it uses the find object again to search
for capitalized words in front of the acronym, and records those words
as the definition. The acronyms and definitions are stored in two long
strings, and at the end of the macro, it pulls the information from
those strings to write in the new documents.

For a 94-page document, it's taking about 1 minute and 30 seconds to
run. Some of the documents we work with are going to be much larger
than this, and I can only imagine that the process will slow even
more the further it gets into a document.

I've dropped some Document.UndoClear lines in the loops, and that
might have helped a little, but I think the biggest problem is
repaginating. Even with code that switches the document to normal
view and turns background repagination off, Word is still
repaginating while the macro runs. I'm not sure what code is
triggering this.

I've tried running it with the application invisible, but that doesn't
seem to help much. Also, I don't really want to keep the application
invisible because I'm giving the user the option of being prompted
when the macro encounters an acronym. The acronym is highlighted so
the user can see it in context, decide whether it should be in the
table, and then press Yes or No.

Any advice on improving speed?
 
M

Mcollette

Actually I found the problem. I have the macro keeping track of the
numbers of the pages on which it finds the acronyms. It's doing this by
writing the page number in a string:

strPageNumsFound = strPageNumsFound & "#" &
oRange.Information(wdActiveEndPageNumber)

I'm guessing that when Word reads wdActiveEndPageNumber, it's
repaginating first to make sure it gets the correct number. (I can tell
it's repaginating because I see it very briefly in the status bar.)
Anybody have a workaround to get the page number of the range without
repaginating?




Firstly, how, exactly, do you know that repagination is happening?

Secondly, it is difficult to give general pointers about performance
beyond
the fairly generic (such as Jay has already given regarding use of the
Selection). Your description of what the code does does not include
any
Undo-able actions until right at the very end, so you saying you have
removed Undo.Clear statements makes me suspicious about whether you may
have
other unnecessary code, or whether you may be doing more than you say.
Without seeing your code it is difficult to speculate but nothing you
describe ought to take very long.

Finally you don't say what version of Word you are using, but there are
some
innocuous statements that perform very poorly in Word 2007 - although,
again, nothing in your description would lead me to believe you were
using
any of the ones I know about.

--
Enjoy,
Tony

'Word Articles Home Page' (http://www.WordArticles.com)

Mcollette said:
I was already using the range object to search. I've gone back through
and made sure that any use of the selection object has been removed, and
it's still repaginating. I have no idea what's triggering it.


The big problem occurs when you use Selection.Find to do the searches.
That
forces Word to move the insertion point/selection through the document
(even
with screen updating turned off), which is what's triggering the
repagination.

Instead, declare a Range object, set it equal to the document's range,
and
use that object's .Find member to do all searching and replacement.
Use
another Range object to find the defining term. Here's some "air
code":

Dim oRg As Range
Dim oDefRg As Range

Set oRg = oDoc.Range
With oRg.Find
.Text = "[A-Z]{2,}"
.MatchWildcards = True
.Formatting = True
' etc.
While .Execute
' at this point oRg points to the found text;
' decide whether to replace the occurrence...
' if so, then
Acro(x, 0) = oRg.Text

' get the definition
Set oDefRg = oRg.Duplicate
' whatever you're doing to find the definition,
' do it with oDefRg.Find
Acro(x, 1) = oDefRg.Text

' avoid finding the same occurrence again:
oRg.Collapse wdCollapseEnd
Wend
End With

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: 'Home' ('Home' (http://word.mvps.org))
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so
all may benefit.

Mcollette wrote:
I have a fairly complex macro that combs a document for acronyms and
their definitions, then writes them in a new document.

The macro uses the find object to look for words consisting of two
or
more capital letters. When it finds a word, it performs some checks
to
see whether the word should be ignored. It looks for bold
formatting,
whether the word is in a table, and whether the word appears in a
string of commonly capitalized words that we use (uses the InStr
comparison to accomplish the latter.) Any of these conditions causes
it to ignore the word and move on.

If it doesn't skip the word, it then checks to see whether the word
is
inside parentheses. If it is, it uses the find object again to
search
for capitalized words in front of the acronym, and records those
words
as the definition. The acronyms and definitions are stored in two
long
strings, and at the end of the macro, it pulls the information from
those strings to write in the new documents.

For a 94-page document, it's taking about 1 minute and 30 seconds to
run. Some of the documents we work with are going to be much larger
than this, and I can only imagine that the process will slow even
more the further it gets into a document.

I've dropped some Document.UndoClear lines in the loops, and that
might have helped a little, but I think the biggest problem is
repaginating. Even with code that switches the document to normal
view and turns background repagination off, Word is still
repaginating while the macro runs. I'm not sure what code is
triggering this.

I've tried running it with the application invisible, but that
doesn't
seem to help much. Also, I don't really want to keep the application
invisible because I'm giving the user the option of being prompted
when the macro encounters an acronym. The acronym is highlighted so
the user can see it in context, decide whether it should be in the
table, and then press Yes or No.

Any advice on improving speed?
 
J

Jay Freedman

Yes, I think the Information call may be causing repagination.

I don't know whether this will help, but it may be worth a try. Background
pagination is always turned on when the Print Layout view is active, but you
can change an option to turn it off when you're in Normal view (called Draft
view in Word 2007). So the following scratch macro illustrates a method that
might work:

Sub x()
Dim oRg As Range

With ActiveDocument
Set oRg = .Bookmarks("bk").Range

.Repaginate
ActiveWindow.View = wdNormalView
Options.Pagination = False

MsgBox oRg.Information(wdActiveEndAdjustedPageNumber)

ActiveWindow.View = wdPrintView
End With
End Sub

Actually I found the problem. I have the macro keeping track of the
numbers of the pages on which it finds the acronyms. It's doing this
by writing the page number in a string:

strPageNumsFound = strPageNumsFound & "#" &
oRange.Information(wdActiveEndPageNumber)

I'm guessing that when Word reads wdActiveEndPageNumber, it's
repaginating first to make sure it gets the correct number. (I can
tell it's repaginating because I see it very briefly in the status
bar.) Anybody have a workaround to get the page number of the range
without repaginating?

Firstly, how, exactly, do you know that repagination is happening?

Secondly, it is difficult to give general pointers about performance
beyond
the fairly generic (such as Jay has already given regarding use of
the Selection). Your description of what the code does does not
include any
Undo-able actions until right at the very end, so you saying you have
removed Undo.Clear statements makes me suspicious about whether you
may have
other unnecessary code, or whether you may be doing more than you
say. Without seeing your code it is difficult to speculate but
nothing you describe ought to take very long.

Finally you don't say what version of Word you are using, but there
are some
innocuous statements that perform very poorly in Word 2007 -
although, again, nothing in your description would lead me to
believe you were using
any of the ones I know about.

--
Enjoy,
Tony

'Word Articles Home Page' (http://www.WordArticles.com)

Mcollette said:
I was already using the range object to search. I've gone back
through and made sure that any use of the selection object has been
removed, and it's still repaginating. I have no idea what's
triggering it.



Jay Freedman;132524 Wrote:
The big problem occurs when you use Selection.Find to do the
searches. That
forces Word to move the insertion point/selection through the
document (even
with screen updating turned off), which is what's triggering the
repagination.

Instead, declare a Range object, set it equal to the document's
range, and
use that object's .Find member to do all searching and replacement.
Use
another Range object to find the defining term. Here's some "air
code":

Dim oRg As Range
Dim oDefRg As Range

Set oRg = oDoc.Range
With oRg.Find
.Text = "[A-Z]{2,}"
.MatchWildcards = True
.Formatting = True
' etc.
While .Execute
' at this point oRg points to the found text;
' decide whether to replace the occurrence...
' if so, then
Acro(x, 0) = oRg.Text

' get the definition
Set oDefRg = oRg.Duplicate
' whatever you're doing to find the definition,
' do it with oDefRg.Find
Acro(x, 1) = oDefRg.Text

' avoid finding the same occurrence again:
oRg.Collapse wdCollapseEnd
Wend
End With

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: 'Home' ('Home'
(http://word.mvps.org)) Email cannot be acknowledged; please post
all follow-ups to the newsgroup so
all may benefit.

Mcollette wrote:
I have a fairly complex macro that combs a document for acronyms
and their definitions, then writes them in a new document.

The macro uses the find object to look for words consisting of
two or more capital letters. When it finds a word, it performs
some checks to see whether the word should be ignored. It looks
for bold formatting, whether the word is in a table, and whether
the word appears in a string of commonly capitalized words that
we use (uses the InStr comparison to accomplish the latter.) Any
of these conditions causes it to ignore the word and move on.

If it doesn't skip the word, it then checks to see whether the
word is inside parentheses. If it is, it uses the find object
again to search for capitalized words in front of the acronym,
and records those words as the definition. The acronyms and
definitions are stored in two long strings, and at the end of the
macro, it pulls the information from those strings to write in
the new documents.

For a 94-page document, it's taking about 1 minute and 30 seconds
to run. Some of the documents we work with are going to be much
larger than this, and I can only imagine that the process will
slow even more the further it gets into a document.

I've dropped some Document.UndoClear lines in the loops, and that
might have helped a little, but I think the biggest problem is
repaginating. Even with code that switches the document to normal
view and turns background repagination off, Word is still
repaginating while the macro runs. I'm not sure what code is
triggering this.

I've tried running it with the application invisible, but that
doesn't seem to help much. Also, I don't really want to keep the
application invisible because I'm giving the user the option of
being prompted when the macro encounters an acronym. The acronym
is highlighted so the user can see it in context, decide whether
it should be in the table, and then press Yes or No.

Any advice on improving speed?


--
Mcollette
------------------------------------------------------------------------
Mcollette's Profile:
'The Code Cage Forums - View Profile: Mcollette' (http://www.thecodecage.com/forumz/member.php?userid=36)
View this thread: 'Word performance problems - The Code Cage Forums'
(http://www.thecodecage.com/forumz/showthread.php?t=36730)
 
M

Mcollette

Yeah, I've already got the thing switching to normal view and turning
off background repagination, but that one line still triggers it. There
must be some other way to get a page number, but I'm stumped. Thanks for
all your help, by the way. This forum is really good.
 
T

Tony Jollans

OK - I'm guessing a little here ...

In Normal View without background repagination, Word doesn't keep track of
pages and doesn't know what page is what and so repagination - from the
beginning - is necessary for any page-related function.

Maybe going the other way and switching *on* background repagination -
either explicitly, or, maybe, by switching to Print layout View and turning
on Screen refreshing - would mean that Word would know that its information
was correct and not feel the need to constantly confirm it.
 

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