Why Word object keep hanging

K

kdw

I have been been using the following sections of code for opening a new
template and populating it with data for a while and no problem. Recently,
it seems to take much longer to populate the doc and also when I try to do a
Print Preview on the new doc, it would "hang" or not respond. The only piece
of code I remember changing before the problem arose was setting the doc to
be visible or not based on a flag (bPreview). My guess is that I am not
referencing/de-referencing my objects properly and it's now coming back to
bite me but I'm not sure where. Can someone please help?

Public Function PrintLetter(sIDNum As String, _
Optional bPreview As Boolean = False) As Boolean

Dim objWordApp As Word.Application
Dim objWordDoc As Word.Document

Set objWordDoc = CreateWordObj("LetterTemplate.dot", _
Application.CurrentProject.Path,
bPreview)

If objWordDoc Is Nothing Then
Exit Function
End If

Set objWordApp = objWordDoc.Parent

'Populate the letter here

If bPreview Then
objWordApp.Visible = bPreview
Else
objWordDoc.PrintOut
objWordApp.Quit savechanges:=wdDoNotSaveChanges
End If

Set objWordDoc = Nothing
Set objWordApp = Nothing

PrintLetter= True

Exit Function

'--------------------------------------
Public Function CreateWordObj(Optional sTemplateName As String, _
Optional sPath As String) _
As Word.Document

Dim objWordApp As Word.Application
Dim objWordDoc As Word.Document
Dim fs

'Check to see that there is a template
If Not (sTemplateName = "" And sPath = "") Then

Set fs = CreateObject("Scripting.FileSystemObject")

If Not fs.FileExists(sPath & "\" & sTemplateName) Then

MsgBox "File " & sTemplateName & " can not be found!", vbCritical, _
"File Not Found"

Set CreateWordObj = Nothing

Exit Function

End If

End If


Set objWordApp = CreateObject("Word.application")

If Err Or (objWordApp Is Nothing) Then
Call MsgBox("Could not create Word application object. Please check
Word installation.", _
vbCritical, "Word Error")

Set objWordApp = Nothing

Set CreateWordObj = objWordApp

End If

'Continue if can create application


'Create file from template if needed else just blank document
If Not (sTemplateName = "" And sPath = "") Then

Set objWordDoc = objWordApp.Documents.Add(sPath & "\" & sTemplateName,
, True)

Else

Set objWordDoc = objWordApp.Documents.Add

End If

Set CreateWordObj = objWordDoc

'Should objWordApp be set to Nothing here???

Exit Function
 
J

Jezebel

I can't really fathom your code; but you can use the task manager to check
whether the Word application is created and terminated as you expect.
Strange things happen if you instantiate Word like this and don't deal
adequately with errors that might occur; and then try to instantiate a new
instance of Word.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?a2R3?=,

I don't know if this will help at all with your problem, but here are a
couple of observations.
If Err Or (objWordApp Is Nothing) Then
Call MsgBox("Could not create Word application object. Please check
Word installation.", _
vbCritical, "Word Error")

Set objWordApp = Nothing

Set CreateWordObj = objWordApp

End If
Don't set CreateWordObj = objWordApp, even if objWordApp is nothing. It was
created as a different object type. Set CreatWordObj = Nothing, explicitly.
'Should objWordApp be set to Nothing here???
Yes. You should explicitly set all these object variables to nothing before
exiting the function.

Checking Len(string) = 0 is faster than string = ""

You need End Function, not Exit Function, at the end of the first function.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

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

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