Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error 4605 2

Status
Not open for further replies.

KenWK

Programmer
May 25, 2001
76
US
I have a Word macro that uses a custom dialog to navigate and select multiple files for processing. The dialog is then unloaded and file names are kept in and processed from an array. That portion of the macro works properly and will display the names of all the files selected looping through the array with a msgbox command.

The macro works on RTF files which are inserted into a new document, then another very extensive macro is called where many search and replaces and modification of styles among other things happens.

Everything works perfectly EXCEPT that I get the following message after some files have been processed:

4605: The AutoFormatReplaceQuotes method or property is not available because there is a memory or disk problem.

Commenting out that line errors on the very next lines.

I then have to restart Word and run it again. The files I'm testing are small and only 14 will process at a time. I just started a project that has over 1000 files so I'd like to be able to do more than 14 at a time!

This problem also occurred in Word 2000 (the macro has been around a while and updated here and there).

I've tried using ActiveDocument.UndoClear throughout to free up some memory but to no avail.

Any ideas would be most welcome.

Ken
 
This is hard to answer as we do not know what you are doing in not only the original piece of code, but also the "very extensive macro".

This is also not very informative...

"Commenting out that line errors on the very next lines."

"that line"????? What line? One could assume it is a line that uses the AutoFormatReplaceQuotes method.

OK. How about telling us what "that line" actually is?

Since this appears (possibly) to be a memory issue, I am also wondering if there is something that could be done to make your "extensive macro" more efficient. I have no real idea of what you are doing, but I do know that often the code people use for search and replace can be greatly improved.

One question regarding that...are you using Range for these operations, or Selection?

If you are using Selection, then I can almost guarantee that any search and replace can be made MUCH more efficient. For example if (in your search and replace) you are doing anything like:
Code:
Selection.Range.AutoFormat
which could give you that error possibly, then this is very inefficient.

You also do not state what you have set for AutoCorrect. If you have "Straight quotes with smart quotes" checked, then you could have a thousand instances of straight quotes and the following will change ALL of them with one instruction (and one use of memory as it use the Range of the document).
Code:
ActiveDocument.Content.AutoFormat
But again, hard to say, as we do not have enough information from you.

Gerry
 
I'm using a method of search and replace that I use ALL the time including macros that process many files, so I wouldn't THINK that would be the issue. It looks like this:
Code:
   Selection.Find.ClearFormatting
   Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "º"
        .Replacement.Text = "fl"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute replace:=wdReplaceAll
The error is occurring on the following that saves the auto replace quotes and auto format settings, then turns them off so the macro doesn't inadvertantly change them.
Code:
    autofrq = Options.AutoFormatReplaceQuotes
    autofaq = Options.AutoFormatAsYouTypeReplaceQuotes
    Options.AutoFormatReplaceQuotes = False
    Options.AutoFormatAsYouTypeReplaceQuotes = False
If I comment out the first line above it errors on the 2nd, comment that one and it errors on the third!

I didn't THINK there was much here that is different from other macros with which I've not had trouble, but I'll have to look again.

Is there anything else you can suggest that might be "inefficiently" coded that might suck up so much memory as to croak the macro?

Thanks for the time,
Ken
 
I am probably venturing in unknown terrotitory, but what type of variable are autofrq and autofaq.

Also, what is the size of what you are storing in these variables.

"Knowing that you know is the greatest sign of stupidity, knowing that you are ignorant is the best proof of intelligence.
 
Those settings are just boolean, true or false.

Again, I don't THINK its the command itself because if I comment one out the error just occurs on the next line.

There is something, somewhere that his chewing up memory.
 
I'm passing 2 small string arrays (10 to 15 items max) from routine to routine.

Any idea if THAT might be causing this?
 



KenWK,

I notice that over the past 7 years, you have posted 23 threads and have received many good tips related to your stated needs. Yet, you have responded NOT ONCE, to
[blue]
Thank Tek-Tip Contributor
for this valuable post!
[/blue].

The little purple Stars accomplish several important things.

First, it gives positive feedback to contributors, that their posts have been helpful.

Second, it identifies threads as containing helpful posts, so that other members can benefit.

And third, it identifies the original poster (that's YOU, BTW), as a grateful member, that not only receives, but is willing to give tokens of thanks.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Memory leaks can be hard to track down. If your behavior is consistent, and it appears that it is, I'd try a test where the script is built back up from very simple pieces until the leak occurs. Then you can see what is causing it.

_________________
Bob Rashkin
 
There is something, somewhere that his chewing up memory. "

That is quite likely, and I have mentioned some possibilities.

You are still not giving enough information. You mention that there are a number of files being actioned. HOW? By what means? You are opening them, yes? How many? Do you close them properly? Who knows, as you are not saying.

Again, using Selection is not efficient.

You are using Selection. Selection uses resources as Selection actions the GUI. In other words, every action using Selection changes the GUI, and that uses resources.
Code:
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
  .ClearFormatting
  .Text = "º"
  .Replacement.Text = "fl"
  .Execute Replace:=wdReplaceAll
End With
use a Range object, not Selection and therefore does not use GUI resources at all.

The above code actions the entire document...but I do not know if that is what you are doing, as you do not state, exactly, what you are doing.

Why are you using the boolean autofrq at all? What is the purpose of this? I can see possibly changing Options once, but I see no reason it to change more than that. So why do you have a variable for it...PLUS, you make no mention of when (or if) you actually use it.

"so the macro doesn't inadvertantly change them."

Huh? Code should never change ANYTHING "inadvertantly". Code does exactly (for the most part) what you tell it to do, and if there are unexpected results, the reason is almost always the programmers fault. Not "inadvertant" action by the code.

It does seem that you are sucking up resources in some way, but unless you state what it is you ARE doing I do not see how we can help.

"Is there anything else you can suggest that might be "inefficiently" coded that might suck up so much memory as to croak the macro?"

My bolding. Anything "else"??? How about actually trying some suggestions?

1. Do NOT use Selection unless you absolutely have to (and those instances are very rare). It uses resources.

2. actually tell us what you are doing, and why you are doing it that way.

I have code that processes 1,000s of files as well...I do not have the issues you seem to have. You are doing something that is sucking up resources inefficiently. I suspect it has to do with massive use of Selection, AND something to do with how you actioning those files.

Gerry
 
I think fumei diserves this for the effort he is doing, even though he is being ignored. As skip noted, show gratitude and he also will be of great help.

"Knowing that you know is the greatest sign of stupidity, knowing that you are ignorant is the best proof of intelligence.
 
Thank you IIHTP (pardon me if I use an abbreviation. I thought of using just ItIsHard to shorten things, but...ahem, thought better of it).

I appreciate that.

KenWK, I do not see how I can be much more help here.

Gerry
 
Yikes! A colleague looking over my shoulder earlier commented that this string of posts reads a bit like a group of computer geeks who have nothing better to do than go on line and tell people what they’re doing wrong. Personally I doubt that. She was curious though, Skip, what would possess someone to investigate someone else’s activities on a site like this? Are you an admin of some sort? I’m sure I’m showing my ignorance of how these things work, but please understand that I welcome the info as now I’m a better participant having received it.

I apologize for not having used the purple star thank you link, I simply didn’t know what it did but I do ALWAYS try to express my true gratitude for any help I get and try to give back when I can.

I also wasn’t “ignoring” fumei’s advice, just hadn’t had time to try and implement it yet (this is a secondary responsibility for me) and was trying to express that I had my doubts of that particular element of the macro being the problem as we use it all the time. Our most extensive template that is used every day by 8 to 15 editors as well as our word processing operator’s and has, literally, 100’s of occurrances of Selection.Find references and crunches multiple files with extensive macros has never had this problem.

fumi’s comment that different ways of writing the same operation might be more efficient with memory never really occurred to me and was very intriguing, especially the knowledge that the Selection object accesses the GUI. I was trying to avoid having my post be too laborious, going through every step of code, which is why I wondered about other things that might be using more resources than necessary.

I won’t bother with what I meant about “inadvertently” changing something, suffice to say it involves various fonts and word switching quotes when the auto settings are on.

For anyone still interested this is the offending code, without this code and leaving all the search and replace operations as they were (using the Selection object) I’ve been able to process 65 files at a shot without a hitch.
Code:
    On Error Resume Next
    For i% = 1 To ActiveDocument.Styles.Count
        Application.StatusBar = "Fixing Style: " & ActiveDocument.Styles(i%).NameLocal
        With ActiveDocument.Styles(i%)
            .Font.Name = "Times New Roman"
            .Font.Size = 12
            With .ParagraphFormat
                .LeftIndent = 0
                .RightIndent = 0
                .SpaceBefore = 0
                .SpaceBeforeAuto = False
                .SpaceAfter = 0
                .SpaceAfterAuto = False
                .LineSpacingRule = wdLineSpaceDouble
                .Alignment = wdAlignParagraphLeft
                .WidowControl = True
                .KeepWithNext = False
                .KeepTogether = False
                .PageBreakBefore = False
                .NoLineNumber = False
                .Hyphenation = False
                .FirstLineIndent = 0
                .CharacterUnitLeftIndent = 0
                .CharacterUnitRightIndent = 0
                .CharacterUnitFirstLineIndent = 0
                .LineUnitBefore = 0
                .LineUnitAfter = 0
            End With
        End With

        ActiveDocument.UndoClear

Next i%
Unfortunately the code is necessary. I doubt I’ll have the time to try and find another way to do it, especially before this large project is due, but if I manage to do so I’ll update this post with the answer.

And lastly, fumi, you definitely picked the right abbreviation.

All the best and all bygones.

Thanks again,
Ken
 


Ken,

"Yikes!!!"

Nothing better to do?

Tell people what they're doing wrong?

What a perverted perspective!!!

Gerry has found the time to share his vast knowledge and experience, FREELY. Do you realize what it might cost you on the market to get that level of expertice, not only in New England Dollars, but also in time? Did you discover that there's some direction in there to what's RIGHT?


Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I've offered enough advice on various forums over the years and have dealt with consulting fees from both ends enough that I ABSOLUTELY, 100 percent understand the value of Gerry's knowledge AND, even more so, his time as well as the value of forums like this. Both are great, indeed. I admire those who can make time to extend their assistance and wish I could do more of it myself.

Which is why I stated that I DID NOT share my colleague's view.

I'm sorry I, apparently, offended some sensibilities by seeming to "ignore" Gerry's advice, I wish assure him again that it was certainly NOT ignored and received with great appreciation and thought. I guess I also, regretfully, let my own sensibilities/ego become offended by the, apparent, tone of things, which I think is where my colleague got her notion.

I still don’t understand, Skip, how you even became involved with the post, though, again, I do greatly appreciate your advice on the thank you link. I certainly want to do my part in helping those who help me, especially something so simple.

I’m done here unless, of course, I do figure out exactly where the problem lies, then I'll repost.

Thank you again Gerry.
 



"Which is why I stated that I DID NOT share my colleague's view."

Well then, why expend the time, energy and thought in posting an opinion that you did not share and was not related to the contents of this thread?

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
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
 


I've been recovering from the creepin' crud: resting comfortably, thank you. ;-)

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Get fully better soon. Although I am curious as to what exactly is the "creeping crud". No! No! Never mind, don't tell me. I have a vivid enough imagination.





Oh...I wish I had not thought about that.

Gerry
 
Gerry, I came back here to see your code suggestions and found your utterly wonderful post!

:) "we sic Skip on 'em." :)

Just great! Thanks very much for light-hearted humor, it generated a much appreciated smile.

Skip, no hard feelings I hope. Guys like you and Gerry make these forums invaluable for hacks like me. I certainly meant no ill will at anytime.

Gerry, sorry for the lack of information, bring too brief I guess.

The RTF files I'm processing were typeset in and exported from Ventura Publisher, and are USUALLY going back to the author so they can revise the text for their next edition. Therefore the Times New Roman font change. The macro is intended to take the exported file that could have any number font changes in it as well as unwanted formatting and make it into a Word file that is as consistent in look as we can get it. We also have to assume the person using the file will not be an advanced or even a moderately skilled Word user so the plainer the better.

The macro first allows our operator to select the fonts that were used in the project from which special characters may have been used. Next it searches the document for characters in the selected fonts and puts a unique text string around them based on the font name. It then changes everything to Times New Roman.

The modification of the styles is an attempt to make the document remain consistent regardless of the computer on which it is opened and the normal.dot that gets attached. For that reason every style is preferable. I'm not a total lost cause, though, as I did think of removing the things from the style modification code that I knew wouldn't be coming out of VP. Just removing those did get a few more files to process before the memory error occurs. I've settled now, though, on using your great idea of modifying only the styles in use in the document. For 95-ish percent of what we'll use this for that should be fine and I've processed many more files at a shot without error.

Skip, I hope you're feeling better and Gerry thank you once again for the good-natured commentary and timely advice. Being able to process many more files at a time rather than just 14 will make this 1500 file project much more profitable.

All the best,
Ken
 



Thank you, I am.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top