Out of memory

J

Jes

I have created a template which runs a macro containing a form and some code
in a module.
But if Word has (or have had) several instances of a document created from
the template open the user gets a “out of memory†error when the macro runs.

If all instances of word are closed the macro runs again and functions
probably.

I figure it’s because the form and the module isn’t unloaded probably. I
have tried to insert an “Unload Me†statement in the form instead of hide.
But it’s the same.

How can I avoid getting “Out of memory†errors?
 
J

Jezebel

Almost certainly the problem is that your code isn't cleaning up properly.
Yes you need to unload your forms. Hard to make specific suggestions about
code structure; but better than 'Unload me' within the form is for the
calling code to take care of it, eg ---

Dim pForm as frmMyForm

Set pForm = new frmMyForm
pForm.Show
:

unload pForm
Set pForm = nothing


In other words, treat all forms as objects: instantiate them and destroy
them as needed. A basic prinicple of object-oriented coding is that objects
are never responsible for their own lifespans. Thus a form should never try
to destroy itself.
 
J

Jes

Hi Jezebel,

Thanks!

Right, is must be my pure garbage collection skills.

I now have the following code:

Dim pForm As UserFormTUSKrav
Private Sub Document_New()
Set pForm = New UserFormTUSKrav
pForm.initUserFormTUSKrav
Unload pForm
Set pForm = Nothing
End Sub

Private Sub Document_Close()
Set pForm = New UserFormTUSKrav
pForm.initUserFormTUSKrav
pForm.saveDoc
Unload pForm
Set pForm = Nothing
End Sub

But I still get the same error message – “out of memoryâ€â€¦ when several
instances of the template has been run.

Any ideas?
/Jes

"Jezebel" skrev:
 
J

Jezebel

Put a messagebox or debug.print statement in the form's Terminate event, and
check that it really does terminate. (And similarly for any class modules
you may have.) The sorts of things that will form from unloading are
variables that point to objects on the form itself -- such as a variable
containing a reference to a form control.
 

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