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

problem opening several word documents with 1 word object

Status
Not open for further replies.

Painkiller

Programmer
May 18, 2001
97
NL
Hi all,

I'm facing the following problem: I'm searching different word documents for a certain text. At first I made a loop in which

a word object was made, the document was opened, actions took place, the document was closed and the object was destroyed. In

code:

Set Word = New Word.Application
Word.Visible = False
Word.Documents.Open strWordFile
...
[actions]
...
Word.Documents.Close
Set Word = Nothing

However for some reason in win NT this took a lot of memory. Research showed that thare were a couple of 20 winword.exe

processes running in the background (I did open 20 word docs). So I figured I only had to create 1 word object, and then in a

loop, open a document, actions and then close the document. The loop takes place 20 times (for 20 documents) and then the

word object is destroyed.


In the loop this leaves just the following code (with the wordobject beind created at the start of the program and being

destroyed at the end of the program):

Word.Documents.Open strWordFile
...
[actions]
...
Word.Documents.Close

However this gives the following error:

91 Object variable or With block variable not set

does anyone why I get this error? And why when using the first method 20 winword.exe processes were created?

Thanx in advance

Sujesh
 
Use the Object in memory rather than creating 20 or so "new" Word Objcets:

Code:
Dim WordWasNotRunning As Boolean
Dim WrdApp as Word.Application 
    
    ' Test to see if there is a copy of Microsoft Excel already running.
    On Error Resume Next   ' Defer error trapping.
    ' Getobject function called without the first argument returns a
    ' reference to an instance of the application. If the application isn't
    ' running, an error occurs.
    Set WrdApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then WordWasNotRunning = True
    Err.Clear   ' Clear Err object in case error occurred.
    'Reset Error handling
    On Error GoTo err_handler
    If WordWasNotRunning Then
        Set WrdApp = CreateObject(&quot;Word.application&quot;)
    End If


Mark
 
Finally figured it out. The second method works like a charm. I had only forgotten to remove a statement from the first method in the actions: set word = nothing. This is why the error followed.

Thanks for your response Mark.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top