[AccessD] Word closes before it shows the user the document

connie.kamrowski at agric.nsw.gov.au connie.kamrowski at agric.nsw.gov.au
Thu Apr 7 01:25:50 CDT 2005


Hi all,

Now trying to fix a 2nd database which is not working, written in Access97.
Problem is there is a Form letter function (code below) when I call this
function it opens word but no document appears in the window. The popup to
ask if it printed OK appears but the document is not visible to the user
and no printing occurs. If I comment out the line
'.Documents(strMainDocument).Close SaveChanges:=wdDoNotSaveChanges I get a
program monitor error but the document is visible.

Any ideas?

Connie Kamrowski

Analyst/Programmer
Information Technology
NSW Department of Primary Industries
Orange


Public Sub Export_Word()
Dim rsA0001 As Recordset

Dim objWord As Word.Application
Dim strMainDocument As String

Dim dbExportWord As Database
Dim rsExportWord As Recordset
Dim booPrintBackground As Boolean


gfLetterPrintedOK = False 'Value will be set to true if the user verifies
that the letter printed

' Resume to the next line following the error.
On Error Resume Next

' Attempt to reference Word which is already running.
Set objWord = GetObject(, "Word.Application")
If Err = 429 Then
    Set objWord = New Word.Application
    Err = 0
End If


' If true, Word is not running.
If objWord Is Nothing Then
        ' Create a new instance of the Word application.
        Set objWord = New Word.Application
        ' If true, MS Word is not installed.
        If objWord Is Nothing Then
                MsgBox "MS Word is not installed on your computer"
                GoTo ExitRoutine
        End If 'objWord Is Nothing
End If 'objWord Is Nothing

'Check to Ensure the temporary RTF file has been deleted, if not, Delete it
    gstrWordDirectory = Dir$(strTempRTFFile, vbNormal)
        If gstrWordDirectory <> "" Then
                On Error GoTo Failure 'Displays 'File Access Error (#71) if
a form letter is already open
                    Kill strTempRTFFile
                On Error Resume Next
        End If 'gstrWordDirectory <> ""

    If gstrAccessWordQuery = "" Then
                    'Print Blank Form
                        objWord.Documents.Add (gstrTemplate)
                Else
                    DoCmd.OutputTo acOutputQuery, gstrAccessWordQuery, _
                        acFormatRTF, strTempRTFFile, False
                    objWord.Documents.Add (gstrTemplate) 'Template document
'*.dot'
                    strMainDocument = objWord.ActiveDocument.Name 'Sets the
name of the 'Main Document' (template document)
                    'so that it can be closed later

                    With objWord.ActiveDocument.MailMerge
                        .SuppressBlankLines = True
                        .ViewMailMergeFieldCodes = False
                        .Destination = wdSendToNewDocument
                        .Execute
                    End With

                    With objWord 'Close Main Document
                        .Documents(strMainDocument).Activate
                        .Documents(strMainDocument).Close
SaveChanges:=wdDoNotSaveChanges
                           ' Store background printing option.



                     End With

                objWord.Activate    ' Activates Word
                objWord.Visible = True    ' Show Word to the user.
                objWord.WindowState = wdWindowStateMaximize
                Set objWord = Nothing    ' Release the object variable.

            On Error GoTo Failure

                If MsgBox("Did your letter print correctly?", vbYesNo,
"Form Letters") = vbYes Then

                        gfLetterPrintedOK = True
                        Set dbExportWord = CurrentDb()
                        Set rsExportWord =
dbExportWord.OpenRecordset("tblLettersSent", dbOpenDynaset, dbFailOnError)

                        With rsExportWord
                            .AddNew
                                !LTRLPBNumber = gstrRLPBNumber
                                !LTPropertyID = glngMainPropertyID
                                !LTFormLetterID = gstrFormLetterID
                                !LTComment = gstrFormLetterComment
                                !LTDatePrinted = Date
                                !LTTimePrinted = Time
                            .Update
                        End With

                        Select Case getgstrFormLetterID()
                            Case "A0001"
                                dbExportWord.Execute "A000104",
dbFailOnError

                        End Select
                End If 'MsgBox("Did your letter print correctly?"...
      End If 'gstrAccessWordQuery = ""


ExitRoutine:
    On Error Resume Next
        rsExportWord.Close
        Set rsExportWord = Nothing
        dbExportWord.Close
        Set dbExportWord = Nothing
        rsA0001.Close
        Set rsA0001 = Nothing
        Set objWord = Nothing    ' Release the object variable.
    Exit Sub

Failure:
        Call ErrorHandler(strProcedureName:="Export_Word",
lngErrorNumber:=Err.Number, _
            strErrorDescription:=Err.Description, _
            strErrorSource:=Err.Source)
        Resume ExitRoutine

 End Sub




This message is intended for the addressee named and may contain
confidential information. If you are not the intended recipient or received
it in error, please delete the message and notify sender. Views expressed
are those of the individual sender and are not necessarily the views of
their organisation.





More information about the AccessD mailing list