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

MS Word macro memory problem

Status
Not open for further replies.

tonupkid

MIS
Apr 3, 2006
4
NL
Hi,

I’ve a problem with a macro in Word. After running the macro several times I receive “the Run-time error 5112, there is not enough memory or disk space to complete the operation”. Restarting Outlook (using Word as editor) and Word solve this problem until the next error. First I use a script in Outlook and then the one in Word witch is giving the problem (I believe).

OUTLOOK:
Private Sub GetContact()
Dim objApp As Application
Dim objNS As NameSpace
Dim objSelection As Selection
Set objApp = CreateObject("outlook.application")
Set objNS = objApp.GetNamespace("MAPI")
Set objSelection = objApp.ActiveExplorer.Selection
If objSelection.Count <> 1 Then
MsgBox "Niet meer dan één item selecteren a.u.b."
GoTo Exit_GetContact
Else
Set m_objitem = objSelection.Item(1)
If m_objitem.Class <> olContact Then
MsgBox "Selecteer een contactpersoon."
GoTo Exit_GetContact
End If
End If
Exit_GetContact:
Set objSelection = Nothing
Set objNS = Nothing
Set objApp = Nothing
End Sub

Private Sub Opendoc(strtemplate As String)
Dim objword As Word.Application
On Error Resume Next
Set objword = GetObject(, "Word.application")
If objword Is Nothing Then
Set objword = CreateObject("word.application")
End If
On Error GoTo 0
objword.Visible = True
Set m_objDoc = objword.Documents.Add(strtemplate)
Set objword = Nothing
Set m_objDoc = Nothing
End Sub

Private Sub FillFormFields()
On Error Resume Next
m_objDoc.Bookmarks("CompanyName").Range.InsertBefore m_objitem.CompanyName
m_objDoc.Bookmarks("FullName").Range.InsertBefore m_objitem.FullName
m_objDoc.Bookmarks("BusinessFaxNumber").Range.InsertBefore m_objitem.BusinessFaxNumber
On Error GoTo 0
End Sub
Private Sub FillFormFieldsBrief()
On Error Resume Next
m_objDoc.Bookmarks("CompanyName").Range.InsertBefore m_objitem.CompanyName
m_objDoc.Bookmarks("FullName").Range.InsertBefore m_objitem.FullName
m_objDoc.Bookmarks("BusinessAddressStreet").Range.InsertBefore m_objitem.BusinessAddressStreet
m_objDoc.Bookmarks("BusinessAddressPostalCode").Range.InsertBefore m_objitem.BusinessAddressPostalCode
m_objDoc.Bookmarks("BusinessAddressCity").Range.InsertBefore m_objitem.BusinessAddressCity
On Error GoTo 0
End Sub

Private Sub GaNaar()
ActiveDocument.Bookmarks("GaNaar").Range.Select
End Sub
Private Sub UpdateField()
ActiveDocument.Fields.Update
End Sub

Sub TechnicalFaxOrderNL()
Call GetContact
Call Opendoc("\\Jaczon_srv3\templates\Technical_Department\FaxorderNL.doc")
Call FillFormFields
Call UpdateField
Call GaNaar
Set m_objDoc = Nothing
Set m_objitem = Nothing
End Sub

WORD:
Sub SaveOrder()
Dim dlgSaveAs As FileDialog
Set dlgSaveAs = Application.FileDialog( _
FileDialogType:=msoFileDialogSaveAs)
dlgSaveAs.InitialFileName = ActiveDocument.Bookmarks("PoNumber").Range.Text & "-" & ActiveDocument.Bookmarks("CompanyName").Range.Text
dlgSaveAs.Show
If dlgSaveAs.InitialFileName <> "" Then dlgSaveAs.Execute
Set dlgSaveAs = Nothing
ActiveDocument.Close
End Sub

 
You are setting the objects to Nothing, which is good. But you are not quitting the applications.

objApp.Quit

and

objDoc.Quit...THEN = Nothing.

Gerry
 
I went yesterday a little further with my search. I ignored the macro in Outlook and concentrated myself on the macro in Word. It’s seems when I open just one document with the right bookmarks and select the macro, everything is working fine. I can repeat this for more than 20 times. But when I open 3 documents at once and want to save them with the macro the first two are working fine and the third is giving the memory error. So it’s definitely the Word macro what giving the error.
 
It is not an error really. It is a statement.

Please clarify EXACTLY what you mean by "open just one document and select the macro." This implies that the macro is in the document opened. Please describe EXACTLY how you are opening the documents.

If you are using the code above to open the document, then each time you do so, you are making an instance of Word. Each of those instances use resources.

So please describe exactly what you are doing. Track precisely the steps.

Gerry
 
Sorry for my dreadful explanation. Ok what I’m doing is the following.
1) starting Word
2) menu file, open, open a document (fax order) with bookmarks. The bookmarks represents the order number and the company name where the document is faxed to.
3) On the toolbar standard I inserted a button witch starts the macro SaveOrder. This macro is in the normal file. So not in the document I open.
4) By clicking on the button the macro starts and opens the file dialog save as. In the file name a combination of the order number and company is automatically filed. So the user has only to change the directory, if he wants. After saving the document closes.

So far nothings going wrong. When I open not one document, but 3, I get the statement.


Brigitte
 
Hi Brigitte.

OK...not a problem with that. I hope you will not take this as being critical, as it is not meant to be. When I ask that you describe EXACTLY, I mean exactly. This is crucial when trying to debug something.

Here is what I have from you.

1. You open Word. You have a blank document.
2. You use File > Open to open a document.
3. On the toolbar, you have a button that fires a macro stored in normal.dot (SaveOrder).
4. After firing SaveOrder it CLOSES the active document. You open another document.
5. You fire SaveOrder. This CLOSES the document.
6. You open another document, and try to fire SaveOrder. It fails from a memory error.

Correct?

OK. The first question is - you state that you have more than one document open. How is that possible if SaveOrder in fact closes the document? Please explain EXACTLY how you have more than one document open.

Second - are you running the other parts of code you previously posted? Are you running it AT ALL?

The documents you open are obviously previously saved. SaveOrder essentially does a SaveAs using the data in the bookmarks as the new name. How is the data getting in the bookmarks, if you are NOT running the previous code?

Third - why are you doing all the code in SaveOrder anyway? Why are you do the dialog at all?
Code:
Sub SaveOrder()
 NewFilename = ActiveDocument.Bookmarks("PoNumber").Range.Text & "-" _
     & ActiveDocument.Bookmarks("CompanyName").Range.Text
  If NewFilename <> "" Then
   ActiveDocument.SaveAs Filename:= NewFilename
  End If
ActiveDocument.Close
End Sub
I fail to see the need for the dialog.

Gerry
 
I have solved my problem by creating a new macro. This one works.

[VBA]Sub SaveOrder()
Dim oDlg As Dialog ' Object Dialog
Dim sPth As String ' String Path

sPth = "\\documents\everyone\orders\"
Set oDlg = Dialogs(wdDialogFileSaveAs)
oDlg.Name = sPth & ActiveDocument.Bookmarks("PoNumber").Range.Text & "-" & ActiveDocument.Bookmarks("CompanyName").Range.Text
oDlg.Show
Set oDlg = Nothing
ActiveWindow.Close
End Sub
[/VBA]

Thanks for the help.

Brigitte
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top