Very, very slow macro

J

John Svendsen

Hi All,

I need to delete words that are marked in red. So I put together this little
macro, but it is very, very slow!!!

Sub DeleteWordsInRed()
Selection.WholeStory
For xxx = Selection.Words.Count To 2 Step -1
If Selection.Words(xxx).Font.Color = wdColorRed Then
Selection.Words(xxx).Select
Selection.Delete
Selection.WholeStory
End If
Next
End Sub


Can someone give me a clue on how to speed this up?

Thanks os much, JS
 
T

Tony Jollans

You are potentially asking Word to do a lot of counting through its
collection of words. In a big document it could be slow. There are some ways
of changing it which might affect performance but the biggest single change
you could make in this case is to use Find and Replace.

Find *
With the cursor in the Find box, click on Format, select Font, and choose
colour Red

Replace with (leave empty)
Check Use Wildcards

Hit Replace All

Record yourself doing it if you need code.
 
J

John Svendsen

Hi Tony,

Thanks so much for your input - truly, you are correct, it is a lot faster
(I should have thought of using find&Replace.... in the first place).

Actually, the macro I built is a derivation on another one, which puts an
"\" between words that have different formats, colors, etc. (This is the one
that is REALLY slow). I'm adding it below so maybe you can think of a way to
speed it up.
Again, thanks so much for your help.
Rgds, JS

Sub InsertCharBetweenBoldItalicUnderlineWords()
Selection.WholeStory
For xxx = Selection.Words.Count To 2 Step -1
If Selection.Words(xxx).Bold And Not Selection.Words(xxx - 1).Bold Or _
Not Selection.Words(xxx).Bold And Selection.Words(xxx - 1).Bold Or _
Selection.Words(xxx).Italic And Not Selection.Words(xxx - 1).Italic Or _
Not Selection.Words(xxx).Italic And Selection.Words(xxx - 1).Italic Or _
Selection.Words(xxx).Underline And Not Selection.Words(xxx - 1).Underline
Or _
Not Selection.Words(xxx).Underline And Selection.Words(xxx - 1).Underline _
Then
Selection.Words(xxx).Select
Selection.InsertBefore "\"
Selection.WholeStory
End If
Next
End Sub
 
T

Tony Jollans

Hi John,

Sorry for taking some time to get back - I looked briefly and decided it
needed some time.

I have now had a bit of a play with it and it is indeed very slow. I can
speed it up a little by making some small changes but basically what it is
doing is inherently slow and really needs doing in a different way. I would
guess some code built around Find and Replace would be better but I need to
run a couple of tests.

A couple of questions ...

Do you have words which are both, say, italic and bold?
Do you have any, say, bold text which is not a complete word?
 
J

John Svendsen

Hi Tony,

First of all, thanks for your interest :)

The idea here is to insert a "\" between any two words which have ANY
different formatting. I'll explain: I want to first run a machine
translation on paragraphs before revising them for correctness - however,
all machine translators I've seen use the first word's format for the whole
sentence it translates (sentences are defined by a carriage return or ".").
To get arround this I first insert a "\" between differently formatted words
and repalce them with "^p" (carriage returns) so when I run the translator
the formatting of the words is preserved (even though not good, but better
to fix than reformatting all the words again).

Again. thanks for your help.

Best Regards, JS
 
T

Tony Jollans

I'm not a user of machine translation but I would have thought you were
introducing translation problems by splitting sentences, but for what it's
worth ...

You can make the code a bit more efficient by using a With construct,
something like this ..

With Selection.Words(xxx)
If .Bold And Not .Previous.Bold Or _
Not .Bold And .Previous.Bold _
Or _
etc.

But really it is still so painfully slow that I would be more inclined to
simply do a search for all bold text and surround it with "\", then repeat
for italic and underline (a little trickier). Then replace all multiple
instances of the "\" character with one. It isn't perfect - but then what
you're doing isn't either - but it can be done in real time :)

Using Find and Replace, set the Find format to Bold (and leave the box
empty), and the Replace text to \^&\ (backslash, caret, ampersand,
backslash)
For underlining you need to select each style of uinderline separately which
is a bit painful if you use several styles (but no big deal in code I
suppose). One point to note if you do this manually and record it to get
code, the macro recorder doesn't record setting formats properly (you need
to use Selection.Find.Font.Bold = True, etc., I think)

I'm happy to help if you have any specific problems but the general one I
can't see an easy way round I'm afraid.
 
J

John Svendsen

Tony,

Again, thank you very much for your time.

I will try this

Best Regards,

Js
 
G

George Lee

Four ways to speed this up:
* As Tony mentioned, use find and replace
* Use For...Each loop as in
For Each myWord In myDoc.Words
next
* You didn't say either way, but run that code as VBA from within a
document, rather than as a seperate VB application. There is a time consuming
overhead whenever VB has to cross the boundary in to VBA or a document (as
seen by using "dot" commands such as myDoc.Words)
* Again, you didn't say you did, but it is common to have a DoEvents or
screen updating (such as label1.caption = "Word = " & theWordCount). This
also slows down the processing. However in some cases, your interface will
need some sort of updating message.
 
G

George Lee

As a bonus suggestion:
* Don't use Selection, use Range. In almost all instances, Range is faster.
 
J

John Svendsen

Hi Tony,

It's taken me sometime to come around... But I'd like to thank you for your
tips - I did not know that ==>^&<== repeated whatever was selected in the
Find box...

With this, things are now much easier.

Regards, JS
 

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