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
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