Skip...take a nap. Although I do understand the reason for your comments.
Ken: "Unfortunately the code is necessary. "
No Ken, that code is absolutely NOT necessary. Further, if this is the code that is operating across many documents, I can see why it is sucking up resources.
The fact that you have "literally, 100's of occurrances of Selection.Find references and crunches multiple files with extensive macros " does not in the least change the
fact that doing it that way is inefficient.
That is why I made my comment:
Me said:
If you are using Selection, then I can almost guarantee that any search and replace can be made MUCH more efficient.
I will extend that...I can definitely guarantee using Range instead of Selection will make your code run faster, and better. Yes, as you know, using Selection will work. Yes, it will work. You can dig a hole with a fork, it will work, but using a spade is better.
In the hope these may help, I would like to add some comments.
1. For i% = 1 To ActiveDocument.Styles.Count
Using a counter can slow things down. Using objects is better. If I understand correctly, you are trying to process ALL styles, yes? But...
really all styles? I doubt that very much. Are you aware that the code line:
Code:
For i% = 1 To ActiveDocument.Styles.Count
will action, LITERALLY, all styles? Including...
1/1.1/1.1.1
1/a/i
Article/Section
Balloon Text
Block Text
....
Caption
Closing
Comment Reference
...and on and on and on.
I will say it again. It will LITERALLY action all styles. It has to. You are telling it to do something from 1 to .Styles.Count. And,
every one of those actions must be parsed and executed.
Code:
Dim oStyle As Style ' declare a style object
For Each oStyle In ActiveDocument.Styles
If oStle.[b]InUse[/b] = True Then
' do your stuff
End If
Next
This makes a style object, and starts to run through the Styles Collection. If it is InUse it will do whatever. If it is not in use, it jumps to the next style. Depending on your situation, does it not make sense to action only those styles actually used?
2. Application.StatusBar = "Fixing Style: " & ActiveDocument.Styles(i%).NameLocal
Do you REALLY need this? Maybe you do, maybe you don't. But it DOES use resources to action it, and remember it has to use the GUI and a refresh for each action.
3. Do you REALLY need all those (in my mind) extraneous instructions? Like these:
.NoLineNumber = False
.Hyphenation = False
If not...then get rid of them. Efficient code does exactly, and ONLY, what you want it to do. I suspect that all those format property instructions came from a recorded macro. Recorded macros ONLY, repeat ONLY, use Selection.
4. "Fixing Style" Hmmmmmm. Word is designed to use styles, but it is better if you use them properly. Properly, Styles come from the
template. "Fixing" - changing? - styles in documents is generally not a good idea. Yes, it can be done, but that does NOT mean it is a good idea. I have no idea why there is, apparently, a need to "fix" the styles, but I am wondering why all of them become Times Roman and 12 pts. Seems very odd to me.
Look, I am not not not trying to make you feel uncomfortable, or trash you, or your code. Believe or not, the reason I do this is to try and help people use Word better. From my perspective, a great deal of helping is trying to educate (hopefully politely). It is an effort to assist in
understanding, so not only the HOW, but more importantly, the WHY.
WHY is using Range (rather than Selection) better.
Here is one reason (there are others): on a test I just did, processing the
exact same actions - searching for specific text and changing it, across a number of files - and timing it, using Range was 11 times faster.
Ta-da. There ya go.
I am glad you seem to have some sort of solution for your problem. Or at least I think you have...have you?
Finally, regarding " I doubt I'll have the time to try and find another way to do it"
I have been trying to offer you another way. I still can not fully, as - although I did ask repeatedly - I still do not know what you are really trying to do. I still do not know what you are doing with the multiple files, or how you are doing it. You did not answer my questions.
I have no doubt though, that your "extensive" macros could be improved...ummmmm...extensively.
"I'm done here unless, of course, I do figure out exactly where the problem lies, then I'll repost."
If I knew what you were doing, I believe I could help you figure it out.
In any case, heh....shrug...thank you for your efforts to communicate. I commend you on retaining politeness in the face of Skip's (somewhat justified) comments.
Skip: you know I love ya, but using "perverted" may have been pushing it.
Ken, Skip is one of the most valuable persons on this site. His vast knowledge and assistance has helped literally thousands of people. Take a look at
his stats. Anyone can look at anyone's "activities", sort of. One of the things that
may - repeat MAY - indicate someone is not really contributing is looking at their stats.
If someone NEVER gives a star; if the ONLY threads they write in are their own (i.e. it seems they only are interested in their stuff); if they do not respond to suggestions; if they...ahem...do not answer legitimate questions, then this can indicate that person is not contributing.
This site is a community, not a Help Desk, or a code/solution center. Frankly, the more you give, the more you get back. People who only take, well, that is not what this is about, and those people eventually get identified and...
we sic Skip on 'em.
Just kidding, and I am not saying you are one of those. You have clearly expressed that with:
"but I do ALWAYS try to express my true gratitude for any help I get and try to give back when I can."
Good.
Seriously though, Skip is one of our best, and I believe he feels personally connected with Tek-Tips as a community.
As do I.
Anyway, all the best. I hope you work things out. I believe the "offending" code can be done better, and I hope I have suggested ways that can be achieved.
Gerry