UrbiJr
UrbiJr

Reputation: 133

VBA - Use standard instead of immediate printing to print word doc with Printout

So basically I generate a word document from a ms access table with vba, and then I print it.
The code is the following:

With rs
    'ensure the recordset is populated
    If Not .BOF And Not .EOF Then

        Set objWord = CreateObject("Word.Application")
        'objWord.Visible = True
        Set doc = objWord.Documents.Add
        doc.SaveAs CurrentProject.Path & "\report.doc"

        'not necessary but good abtitude
        .MoveLast
        .MoveFirst

        While (Not .EOF)
            If Not IsNull(.Fields("REGIONE SOCIALE")) Then
                regioneSociale = .Fields("REGIONE SOCIALE").Value
                regioneSociale = UCase(regioneSociale)
            End If
            If Not IsNull(.Fields("INDIRIZZO")) Then
                INDIRIZZO = .Fields("INDIRIZZO").Value
            End If
            If Not IsNull(.Fields("CAP")) Then
                CAP = .Fields("CAP").Value
            End If
            If Not IsNull(.Fields("LOCALITÀ")) Then
                LOCALITA = .Fields("LOCALITÀ").Value
            End If
            If Not IsNull(.Fields("CODICE FISCALE")) Then
                codFiscale = .Fields("CODICE FISCALE").Value
            End If
            If Not IsNull(.Fields("CODICE STALLA")) Then
                codStalla = .Fields("CODICE STALLA").Value
            End If
            If Not IsNull(.Fields("NOTE")) Then
                NOTE = .Fields("NOTE").Value
            End If
            'Debug.Print regioneSociale & CAP & LOCALITA & codFiscale & codStalla & NOTE
            objWord.Selection.Font.Bold = True
            objWord.Selection.Font.Underline = True
            objWord.Selection.TypeText regioneSociale
            objWord.Selection.Font.Bold = False
            objWord.Selection.TypeParagraph

            objWord.Selection.Font.Italic = True
            objWord.Selection.TypeText "INDIRIZZO:"
            objWord.Selection.Font.Underline = False
            objWord.Selection.Font.Italic = False
            objWord.Selection.TypeText " " & INDIRIZZO
            objWord.Selection.TypeParagraph

            objWord.Selection.Font.Underline = True
            objWord.Selection.Font.Italic = True
            objWord.Selection.TypeText "LOCALITÀ:"
            objWord.Selection.Font.Underline = False
            objWord.Selection.Font.Italic = False
            objWord.Selection.TypeText " " & CAP & " " & LOCALITA
            objWord.Selection.TypeParagraph

            objWord.Selection.Font.Underline = True
            objWord.Selection.Font.Italic = True
            objWord.Selection.TypeText "CODICE FISCALE:"
            objWord.Selection.Font.Underline = False
            objWord.Selection.Font.Italic = False
            objWord.Selection.TypeText " " & codFiscale
            objWord.Selection.TypeParagraph

            objWord.Selection.Font.Underline = True
            objWord.Selection.Font.Italic = True
            objWord.Selection.TypeText "CODICE STALLA:"
            objWord.Selection.Font.Underline = False
            objWord.Selection.Font.Italic = False
            objWord.Selection.TypeText " " & codStalla
            objWord.Selection.TypeParagraph

            objWord.Selection.Font.Underline = True
            objWord.Selection.Font.Italic = True
            objWord.Selection.TypeText "NOTE:"
            objWord.Selection.Font.Underline = False
            objWord.Selection.Font.Italic = False
            objWord.Selection.TypeText " " & NOTE
            objWord.Selection.TypeParagraph

            objWord.Selection.TypeParagraph
            .MoveNext
        Wend
     Else
        MsgBox "No record selected/empty table"
        Exit Sub
    End If
    .Close
End With

doc.Save
'this instruction gives immediate printing instead of the standard one...
doc.PrintOut , , , CurrentProject.Path & "\report.doc", , , , , , , True
doc.Close  

The fact is that actually I'd like to give the user the possibility to change the printer or whatever else option before printing.

I'm talking about giving him this viewenter image description here

before printing, instead of printing the document immediately.

Is it possible with vba?

Fragment of code where I use @Sergey 's advice:

End With
objWord.Dialogs(wdDialogFilePrint).Show
doc.Save
doc.Close

Upvotes: 0

Views: 259

Answers (1)

Sergey S.
Sergey S.

Reputation: 6336

You can use

doc.Activate 'just to make sure that user didn't select other document while generating
objWord.Dialogs(wdDialogFilePrint).Show

instead of doc.PrintOut

Also as a small code optimization I would suggest remove all If-s for fields data and modify word related section as follows:

        With objWord.Selection
            .Font.Bold = True
            .Font.Underline = True
            .TypeText UCase(Nz(rs![REGIONE SOCIALE].Value, ""))
            .Font.Bold = False
            .TypeParagraph

            .Font.Italic = True
            .TypeText "INDIRIZZO:"
            .Font.Underline = False
            .Font.Italic = False
            .TypeText " " & Nz(rs!INDIRIZZO.Value, "")
            .TypeParagraph

            .............
        End With

Upvotes: 1

Related Questions