Hi Everyone,

I have an Access form. When user clicks button, print a word report, for the displayed employee.

The report builds nicely, I'm having 2 problems:

1) ending gracefully.
I want to open in preview mode, for user to print or exit.

The preview and zoom commands are giving me the following error:
error no: 91 description: object variable or with block variable not set

2) if I comment out the preview/zoom commands, sometimes the document prints and closes word gracefully, and sometimes I get the error message:
error no: 462 The remote server does not exist or is unavailable

Below is the code. I would gladly email, the word doc with code, if that would help.

Thanks in advance. code below:

Public Sub TestWithoutFormButton()

Call cmdWordReport_Click("John Doe", "111-11-1111", "Neurologist", "Medicine", "05/01/04")

End Sub


Public Sub cmdWordReport_Click(strLastName As String, _
strSsn As String, _
strTitle As String, _
strDept As String, _
strExamDate As String)



On Error GoTo ErrorHandler


Dim appWord As Word.Application
Dim docs As Word.Documents
Dim doc As Word.Document
Dim prps As Object

Dim strTemplateDir As String
Dim strLetter As String



' ============================
strLetter = "FinalClearanceLetter3.doc"

'Open Word
Set appWord = GetObject(, "Word.Application")


strTemplateDir = "c:\MedBoard\"
strLetter = strTemplateDir & strLetter
Debug.Print "Opening document based on template: " & strLetter


'open copy of template
Set docs = appWord.Documents
docs.Add strLetter

'Set doc = appWord.ActiveDocument

Set prps = appWord.ActiveDocument.CustomDocumentProperties

With prps
.Item("EmpName").Value = Nz(strLastName)
.Item("SSN").Value = Nz(strSsn)
.Item("Title").Value = Nz(strTitle)
.Item("Dept").Value = Nz(strDept)
.Item("ExamDate").Value = Nz(strExamDate)
End With

With appWord
.Visible = True
.Activate
.Selection.WholeStory

' This will make sure all fields are updated when you open the file.
' It is also possible to update specific fields instead all.

.Selection.fields.Update
.Selection.MoveDown Unit:=wdLine, Count:=1

End With


If appWord.PrintPreview = False Then
' open preview at 65%
ActiveDocument.PrintPreview

ActiveWindow.ActivePane.View.Zoom.Percentage = 65

End If



ActiveDocument.PrintOut
appWord.Quit wdDoNotSaveChanges

'=================================================
==
ErrorHandlerExit:
Exit Sub



ErrorHandler:
If Err = 429 Then
'Word is not running; open Word with CreateObject
Set appWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description


Resume ErrorHandlerExit
End If



End Sub