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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where does Word use up memory?

Status
Not open for further replies.

ycim

Programmer
Feb 9, 2005
95
0
0
CA
I have an Access program whereby I am importing some information into access from Word documents. The code also looks for certain criteria in the Word documents, and breaks it up into smaller word documents based upon those criteria. A typical word file that I am trying to break down into smaller units could contain as many as 500 different pieces, but each of those pieces is less than a page long (usually).

The code is very intense and quite lengthy so I won't post it all here, but basically, the flow is as follows..

1. Create an object (Set WD = createObject("Word.Application")
2. Open the original word document
3. Find my content in the document
4. Set a range for that content
5. Copy the range
6. Create a new document (and yes, another instance of Word running)
7. Paste in the content
8. Close the new document
9. Return to my original document and find the next piece, and then repeat steps 3-9.

I know that the code that I have is not opening tons of word applications. At most, I only have 2 sessions running.

The program successfully breaks down the documents and it copies about the first 20 documents, and then I get an "out of memory" problem, and I obviously can't get it to go further.


I am not asking anyone to write the code, but rather I am asking for advice on where to start looking for memory leaks? Although I am a power user in both Access and Word, this is my first program that I have accessed Word through VBA (I have done tons of Access VBA programming, though!)

Can anyone suggest where to start my hunt? I am afraid that I simply don't know where to look.

Thanks for your help.
 
I use duel monitors and often will have 2 instances open (one on each monitor) so I only have to click from one to the other.
Well....yes, that is certainly a reasonable use of multiple instances!

Although, I do not quite understand:
This is especially useful when automating a Word document as it allows me to easily check formatting and such with the original.
What automating would that be? I understand this is off thread, but I am very curious as to what you mean by that.

I use templates, so format for me only comes from the templates. I do not need to check format to an original as my documents are always formatted exactly as the originating template is formatted.

Gerry
My paintings and sculpture
 
Aside from the "off thread" stuff, I still have a pending issue.

oh my...this is going to be interesting! According to this thread here


you cannot clear the office clipboard.

I think I will start another thread with this issue, and see what people can come up with.

Thanks
 
Gerry,

Unfortunetly some of the documents I have to work with were not put together correctly in the first place. Strike that, I can't think of one that was put together that wasn't haphazard. Some examples. The documents are sometimes from an ancient word processor using fonts not available in Word (Word does a substitution). Paragraphs are seperated by a CR/LF. When they want to go to the next page they have just put in enough CR/LF to get to a new page. Places where the user are suppose to put in information are composed of __________. Yep, the user would enter the information with underline and then have to delete the same number of ____. They almost never have tables but have the document laid out to resemble tables. One of the main requirements is that when printed they match the original. With 2 instances of Word I can look at both documents side by side in print preview to get a rough idea how close they are.
 
CBA - what does using two instances give you that a single one wouldn't? Nothing you have said so far would seem to require the second one - although I don't suppose you lose much by using it if it suits you.

ycim - I replied, with a question, in your other thread before reading this thread. If the clipboard really is causing memory issues (and I suppose it is possible if you're copying large volumes) then it may be necessary to reduce your use of copy and paste (I haven't looked closely at your posted code yet) but, as an experiment, could you first try copying a small volume of data (a single space perhaps) 24 times to reduce memory usage by the clipboard?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Interesting discussion.

As for the Office Clipboard clearing, I am having difficulties getting the code from that link to accept my application object...still working on it. There are, BTW, syntax errors in the code, if you copy it to try.

Tony, I am in agreement vis-a-vis multiple instances, although having two monitors with separate instances may be valid. Although you certainly could split your screen and check format between them on one screen (and one instance). Nevertheless, having two documents separately on two monitors (and separate instances to handle them) does make some sense. That way you could look at them separately.

CBA - uh, yes, well, if the documents in questions are buggered up then that is an issue, and doing it with two monitors and two instances seems valid. But again, this seems to me a very special case, and does not in itself promote the idea of commonly using multiple instances.

Further, working with the documents as you describe seems very backward. If there is more than one (and I get the impression this is the case) then it would be FAR better to move these to a real, and properly designed, template.

I stand by my thought that multiple instances are generally not needed, nor desired. A single instance of Word can handle just about anything. Can there be circumstances that would call for multiple instances? Sure, I guess, but I think it is rare.

Gerry
My paintings and sculpture
 
du[red]e[/red]l monitors, eh? For first person shoot-em-ups, I expect... [smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I am a bit short on time today so here is a suggestion that is a bit rough. Going back to a previous post:
Upon further review, it might be a better idea to set what you want to copy to some temp variable and then insert it.
You should be able to eliminate the copy/paste with something along these lines using a temporary variable:
Code:
Sub aTest()
Dim sTemp As Variant
' Select 2 lines, move the cursor, insert the text
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=2, Extend:=wdExtend
sTemp = Selection

Selection.MoveDown Unit:=wdLine, Count:=1
Selection.InsertAfter (sTemp)
    
End Sub

Gerry,
I am recreating the documents with a proper template. It is far easier having the original side by side with the new document template. I agree that "multiple instances are generally not needed". I don't think I implied otherwise. I just gave 2 examples where they might be advantageous.

Tony,
Two instances, on different monitors, allows me to see both documents at the same time. Selecting either document is one mouse click away. In opposition, Window --> select document, is not very convenient. So to answer your question, convenience is what I get.


 
fumei...I am glad to see that you took a visit to that site and get the code, and are working with it. I am hopeful that someone as experienced as yourself can break through it and get it working! From what I read on this site about the office clipboard and the many frustrations, you will be a true hero!

I will stay posted on this one!
Thank you!
 
A variation on the temporary variable approach from CBA.

Sub mymacro()

Dim rng As Range
Set rng = Selection.Range

With Documents.Add()
.Bookmarks.Add "bm"
.Bookmarks("bm").Range = rng
End With
Set rng = nothing

End Sub
 
HughLerwill - what is the purpose of this again? I am not seeing the point of the Range object. You are not doing anything with it, other than to make a bookmark.
Code:
ActiveDocument.Bookmarks.Add Name:="bm", Range:=Selection.Range
will do the same thing without the need for a Range object. You are using the Selection.Range anyway, so i am not following the extra step.

ycim - I have been playing with your code. I have some comments. While I am not sure to what extent the massive use of Selection is to your memory issues, the fact of the matter is that you ARE using Selection for just about everything. Any use of Selection does (very much so) use resources. Some of the things you are doing are really not needed, and may wastes those resources.

For example:

Code:
WD.Selection.WholeStory
WD.Selection.Collapse wdCollapseEnd
This selects the entire document, essentially putting the whole thing into memory, then you immediately copllapse it to the end. This puts the Selection at the end of the document.
Code:
WD.Selection.EndKey Unit:=wdStory
does the exact same thing, but without that step of selecting the whole document.

Ther are numerous places that you could be using:
Code:
With WD.Selection
   ...
End With
that could improve things.

You use:
Code:
WD.Selection.WholeStory
  'no automatic indenting is allowed!
WD.Selection.Paragraphs.Outdent
WD.Selection.Paragraphs.Outdent
WD.Selection.Paragraphs.Outdent
I am not sure why there are the three Outdent instructions, but in any case, it is still using Selection.
Code:
Dim oPara As Word.Paragraph
For Each oPara In ActiveDocument.Paragraphs
   oPara.Outdent
Next
or a variant of that would work more efficiently, and does not use Selection at all.

I am not being critical. And I don't know if how much the major use of Selection is affecting your memory problem...but I would not be surprised if it IS affecting it.

Remember, every time you use something like Selection.WholeStory, the entire document content is allocated some memory. Unfortunately Word is not super good at releasing memory. It has always been that way. In the case I cited above, this use of Selection is absolutely not needed.

This is the reason why Range is used instead of Selection as much as possible. Why use UI resources when you don't have to? It slows things down as well.

Gerry
My paintings and sculpture
 
Gerry,

<<You are not doing anything with it>>
Well in my test it did copy the selection from the Active document into the new one without using the OPs problematic copy/ paste.

I concur with all your other comments re nesting the majority of the code within a With/ For each block(s).

Happy New Year Hugh,
 
Ah, yes, sorry, I was not paying attention...it was the use of Documents.Add and placing the bookmark range. You are quite correct, and an efficient piece of code.

Gerry
My paintings and sculpture
 
Two instances, on different monitors, allows me to see both documents at the same time. Selecting either document is one mouse click away. In opposition, Window --> select document, is not very convenient. So to answer your question, convenience is what I get.
Er ... on Word 2003 I can do this with a single instance using the Compare Side by Side option ...
 
Er ... I am using Office 2000 as the company I work for has no compelling reason spend the money to change versions.
 
Er ... In Word 2000 (in which 'Windows in Taskbar' is imposed) you see each document in a separate window in a single instance and, as far as I know, the user experience you describe is the same whether you have one or two instances open - in later versions you would need to set 'Windows in Taskbar' (although on is the default) to achieve the effect in a single instance.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Lots of posts in the last 24 hours since I have been on...

Let's see...

from fumei
While I am not sure to what extent the massive use of Selection is to your memory issues, the fact of the matter is that you ARE using Selection for just about everything.

The reason is simple (not good, but it is simple). I have never really worked with Word through Access (lots of Access VBA...just not on Word), until about 2 weeks ago, and as one learns, you use a method which is "quick and dirty" until you get a better feel for it. That is why I am using selection for most things. It is the way that I learned first. (see...i told you it was not good!)

At any rate, you had some good suggestions, espeically .wholestory! The doucment is massive and I can see why memory would be tagged up there. I have made some coding modifications, and although it hasn't fixed my issue it certainly is much cleaner.

....CBA & Hugh...
I see the suggestion in the coding! Egads! I hadn't thought of that! Let me try it...hmm....

Thanks to all for the great suggestions and the er..interesting discussion...I do appreciate it. I hope to work on it this weekend, and I will be back here on Monday to let you khow how it goes!

Thanks again!
 
Tony,

Use one instance of word and open 2 documents. In one of the documents open up a dialog box (Format --> Style is one I use a lot). Now try to select the other document. Won't work.

Now try the same thing with 2 instances of Word and two documents. Opening a dialog box in one doesn't stop you from going to the second document.

The documents I am working on now are terrible (see example in previous post). While creating a new template I have to go back and forth between documents to get font, paragraph spacing ect to create a style. Since I am no spring chicken, my memory can be a bit short at times. It is pretty much guaranteed that as soon as I have the style dialog box open that I will forget one of the settings. It helps to not have to close the dialog box just to find out what the paragraph spacing should be.
 
ycim,

If preserving the formatting is important look at the code Hugh posted. The range object should preserve formatting while my example will not.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top