PDA

View Full Version : Recording the different colours in a selection



GOGGS75
06-15-2008, 02:57 AM
Hi,

I am trying to set up a macro that will record the different colours in a large selection as fast as possible.

The best I have done so far is set up a macro that works by first looking at the colour of each paragraph, then looks at the colour of each word if the paragraph was not all one colour and finally looks as the colour of each character if a word was not all one colour.

As this is taking over ten seconds for a 25 page document with eight colours I have been trying to improve this. A completely diffirent approach I thought may be faster is to use the "Selection.Find.Format.Font.Color" command in some way. The problem with this is that I cannot find a way of getting a colour not equal to another found.

"Selection.Find.Format.Font.Color =" works;

but

"Selection.Find.Format.Font.Color <>" does not.

Does anyone know a way round this ?


Thanks

Goggs75

gwkenny
06-15-2008, 05:17 AM
You should post an attachment with your code and sample test document.

I have a sense that you are using selection quite often in your code. That's going to slow you up a lot cause the code has to continually return to the document to process. Better off using some type of variable.

GOGGS75
06-15-2008, 05:41 AM
Hi,

Attached is a copy of some of the document containing the macro. The full macro runs when selecting to run COLOURDEL. A selection has to be made from the document before running this.

Cheers

Goggs75

fumei
06-17-2008, 09:11 AM
1. You really should use Option Explicit!

2. Use of Full Caps is not normally used, except for constants. I take it that your background is older programming, which used full caps.

3. Could you explain why you would have such a document with scattered colours like that? It seems very strange.

4. As gw mentions, using Selection will always slow things down.

5. have you actually tried stepping through your code? Have you actually looked at what it is doing?

You have:


If COLOURCODE <> 9999999 Then

which, if True, calls COLOURFIND

But....in the Sub COLOURFIND, you have:
If COLOURCODE = -16777216 Then Exit Sub

If you stepped through your code, you would immediately see that the majority of execution calls the Sub COLOURFIND...and immediately exits. In other words, it is a pointless Call. However, it is called again, and again, and again.

This decidely slows you down. This is a basic logic issue.

Call COLOURFIND only when you need to. Logically, what you are doing is:

If COLOURCODE <> 9999999

and If COLOURCODE <> -16777216....then do the instructions in COLOURFIND.

So...code that way.

GOGGS75
06-17-2008, 11:55 AM
Fumei,

1. I have seen option explicit on lost of code but when investigating this before could see the only advantange as being a program not crashing when more than one word document was open. Is this the case ?

2. The use of full caps for variables is optional, I use Word 2000 and have always used full caps to distinguish between variables and commands. Would changing this speed the macro up a bit ?

3. There are lots of scattered colours on the document as it is a proforma (template) that has various wording sometimes applying and sometimes not. Rather than use fields to try to automate this the macro automatically edits a 30 page document using find and replace.

4. By using Range rather than Selection is there likely to be much change ?

5. I had stepped through the program a number of times and was thinking about changing the condition where you suggested. As there will be more code in the program after changing it I was initially thinking the current code would be more efficient but am not so sure now.


My initial attachment had one For each loop for paragraphs and two For loops for words and characters. I have now managed to get For each working for all three and it has reduced the time the macros takes on a 30 page document from roughly 10 seconds down to 6 seconds. I am thinking of adding a fourth loop for sentences between paragraphs and words to see if this helps also.


Thanks for your advise.

Goggs75

fumei
06-17-2008, 12:27 PM
1. I have seen option explicit on lost of code but when investigating this before could see the only advantange as being a program not crashing when more than one word document was open. Is this the case ?

NO! NO! It most certainly is not the case. That is not what Option Explicit is about, at all. You get Option Explicit automatically in any new module when you check Require Variable Declaration in Tools > Options.

It does just that. It requires you to declare your variables. This is extremely useful as it will not compile if you make spelling mistakes, or...do not declare your variables, or, declare invalid types, or use invalid values for the type of variable declared.

Use it. You did not investigate well enough. It has nothing whatsoever to do with open documents. I have NO idea where your investigation would have turned up that idea.

2. No, full caps (or not) makes no difference in performance speed. It does make a difference in readability though.

3. Huh????? That makes no sense at all to me. What has colours to do with whether wording is applies, or not? That makes no sense to me.

"Rather than use fields to try to automate this the macro automatically edits a 30 page document using find and replace."

I can make even less sense of the statement above. Again, what does Find / Replace have anything to do with colours? And I have no clue what you mean by rather than use fields.

4. Yes. Selection operates through the GUI, it is what is on screen. Range does not. Range has no interaction with the screen, until whatever it is doing...is done. It depends on what you are doing, but using Range over Selection will - at minimum, although I am guessing - likely double your processing speed, for ANY operation. Likely it is more than that, and for any extended processing it makes a HUGE HUGE HUGE difference.

5. "and was thinking about changing the condition where you suggested. " Don't think about it. Do it. It is major logic flaw and likely contributes to a significant portion of the slow down.

I would very strongly suggest you use Styles. It would avoid all of this crap.

I have no idea what is the process of either your Find/Replace (and you most DEFINITELY should use Range for that...), or your wordings (or not), but:

1. Unless you can convince me with some heavy duty reasoning, I strongly doubt there is a valid reason for the colour changes.

2. If you insist, then use Character styles...not manually formatting.

Word is designed around the use of Styles. I have created thousands of documents, doing so is my living. Not a single one has any manual format at all.

GOGGS75
06-18-2008, 12:01 PM
Fumei,

1. I am going to start using Option Explicit from now on as I can see there are lots of advantages.

3. By using find and replace with formatting also word can automatically delete characters that are a specific colour. This allows the automatic removal of wording on a template but not appropriate in a specific document.

The document I use this macro on has to be adjusted between referring to "Trustee" or "Trustees" many times with the grammer changing dependent on which one in relevant. There is also other wording that is sometimes relevant and sometimes irrelevant.

After reading a word automization book I was under the impression fields can come up with alternative wording depedent on a condition. The macro I made up automatically edits the document keeping it simpler.

4. I will try to use range rather than selection to see if there is a big difference

5. I will also change the condition to avoid entering the COLOURFIND subroutine.


The main objective of the macro is not to format any characters but to remove the characters that are not relevant.


Thanks for your advise

Goggs75

fumei
06-18-2008, 02:06 PM
Again, you are not being specific really, but...

"This allows the automatic removal of wording on a template but not appropriate in a specific document."

Ummmm, this sounds like you are not using templates vs document properly.

There are many efficient ways to validate and change text. What you are doing does not sound efficient, which I believe was your original request for information. However, if you are OK with it, then...you are OK with it.