Set word = nothing takes a looong time

  • Thread starter Betina Y Andersen
  • Start date
B

Betina Y Andersen

I have a VB6 app that uses vba against Word 2000.

When I runs the line Set WordObj = Nothing it takes from 1 minut up to 10
minutes to get freed from memory.

This problem has come as I have added some code to one of my procedures, I
have added the following

FilNavn = Mid(CStr(VisWord), 7, Len(CStr(VisWord)) - 6)
WordObj.Selection.WholeStory
WordObj.Selection.Copy
WordObj.Documents.Add
WordObj.Selection.Paste
WordObj.ActiveDocument.SaveAs FileName:=FilNavn
WordObj.ActiveDocument.Close
WordObj.Selection.HomeKey

Then I return to VB and does some code before calling another procedure that
end Word:

WordObj.ActiveDocument.PrintOut Background:=False
WordObj.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
WordObj.Quit
Set WordObj = Nothing

It is the last line that takes up all my CPU and uses from 1 munute and up
to complete.

What can I do to speed this up, cos it is unbearable to sit and wait for it
to complete and I use this a lot in my VB app?

Regards Betina
 
C

Cindy Meister -WordMVP-

Hi Betina,

The first thing that strikes me, on looking at your code, is how one can never
be quite sure "where you are". You don't really use Word's object model to
work with things; it's more like a user would be sitting there - except of
course no one is. So there's no way to be sure something "unexpected" isn't
going on. I'd approach what you show us more like this:

Dim doc1 as Word.Document, doc2 as Word.Document
Dim FilNavn as String

Set doc1 = WordObj.ActiveDocument
FilNavn = Mid(CStr(VisWord), 7, Len(CStr(VisWord)) - 6)
Set doc2 = WordObj.Documents.Add
doc2.Range.FormattedText = doc1.Range.FormattedText
doc2.SaveAs FilNavn
doc2.Close wdDoNotSaveChanges
Set doc2 = Nothing
'Whatever
doc1.PrintOut Background:=False
doc1.Close wdDoNotSaveChanges
Set doc1 = Nothing
WordObj.Quit wdDoNotSaveChanges
Set WordObj = Nothing

Please note, also, that the "Background" parameter is not very reliable. Better
to set the option directly, in a separate line of code!
I have a VB6 app that uses vba against Word 2000.

When I runs the line Set WordObj = Nothing it takes from 1 minut up to 10
minutes to get freed from memory.

This problem has come as I have added some code to one of my procedures, I
have added the following

FilNavn = Mid(CStr(VisWord), 7, Len(CStr(VisWord)) - 6)
WordObj.Selection.WholeStory
WordObj.Selection.Copy
WordObj.Documents.Add
WordObj.Selection.Paste
WordObj.ActiveDocument.SaveAs FileName:=FilNavn
WordObj.ActiveDocument.Close
WordObj.Selection.HomeKey

Then I return to VB and does some code before calling another procedure that
end Word:

WordObj.ActiveDocument.PrintOut Background:=False
WordObj.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
WordObj.Quit
Set WordObj = Nothing

It is the last line that takes up all my CPU and uses from 1 munute and up
to complete.

What can I do to speed this up, cos it is unbearable to sit and wait for it
to complete and I use this a lot in my VB app?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan 24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
B

Betina Y Andersen

Hi Cindy

Thanks a lot, that really helped, now I have speed again :)

Regards Betina
 

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

Similar Threads


Top