Printing a Word Doc From Access and specify # of copies

  • Thread starter Thread starter URQUILLA
  • Start date Start date
U

URQUILLA

I guess I fixed my own problem :). I looked every posting but just i coudn't
find what I was looking for. I needed to specify a #of copies to print. So
this is how the code looks like it if you need it.
For expert programmers please critize my code if there is something leaking
Or you need to add. Thank you.

Private Sub Command587_Click()

lblPath.Tag = strPath & "\FileName.doc"
lblPath.Caption = "Printing " & "... " & lblPath.Tag
lblPath.HyperlinkAddress = lblPath.Tag

doPrintBacking lblPath.Tag

End Sub

Public Function doPrintBacking(FileName As String)
On Error Resume Next

Dim strInput As Integer, noOfCopies As Integer

strInput = InputBox("Enter 1=for RICHO, Enter 2=for MINOLTA, 3=CANON")

If strInput > 0 Then
noOfCopies = InputBox("Enter # of Copies")

If noOfCopies > 0 Then
Set mobjWOrdApp = CreateObject("Word.Application")
mobjWOrdApp.Documents.Open conTEMPLATE_NAME

With mobjWOrdApp
.Visible = False

If strInput = 1 Then
.ActivePrinter = "\\PRNTRM\RICOH"
Forms!fPrintBacking!lblStatus.Caption = "-> Printing on RICOH \\ ->
" & noOfCopies & " Copies..."
ElseIf strInput = 2 Then
.ActivePrinter = "\\PRNTRM\MINOLTA"
Forms!fPrintBacking!lblStatus.Caption = "-> Printing on MINOLTA \\
-> " & noOfCopies & " Copies..."
ElseIf strInput = 3 Then
.ActivePrinter = "\\PRNTRM\Canon"
Forms!fPrintBacking!lblStatus.Caption = "-> Printing on CANON \\ ->
" & noOfCopies & " Copies..."
End If
DoEvents

.PrintOut Background:=False

.Documents.Application.PrintOut FileName:=conTEMPLATE_NAME,
Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=noOfCopies

.ActiveDocument.Close False
.Quit

End With
End If
End If

Set mobjWOrdApp = Nothing

End Function
 
I would suggest that you don't hard write values
(like printer names) into your code. If you add
another printer, or replace one with a different model,
you have to go back and rewrite the code.

You would be better off using a small popup form
(i.e. frmPrintOptions) where the user would enter
the number of copies in a text box and choose the
printer name from a combo box. Printer names could
be stored in a small lookup table. You would open
the form in Dialog mode in your code, which would
halt the code until the user is done with the form.
The form would have a command button (i.e. cmdContinue)
which would set the forms visible property to false,
allowing your code to continue. The form would still be
open so you could retrieve values from it. You may also
need a hidden check box on the form to use as a flag
to determine if the user entered values or not. you would
manipulate the check box True or False setting in the
Click event of the command button.

Your code might then look like;

Public Function doPrintBacking(FileName As String)
On Error Resume Next

Dim strPrinter As String
Dim intCopies As Integer

DoCmd.OpenForm frmPrintOptions,,,,,acDialog

If Forms!frmPrintOptions!chkFlag = True Then

strPrinter = Forms!frmPrintOptions!cboSelectPrinter
intCopies = Forms!frmPrintOptions!txtCopies

Set mobjWOrdApp = CreateObject("Word.Application")
mobjWOrdApp.Documents.Open conTEMPLATE_NAME

With mobjWOrdApp
.Visible = False
.ActivePrinter = "\\PRNTRM\" & strPrinter & """"

Forms!fPrintBacking!lblStatus.Caption = "-> Printing on " _
& strPrinter & " \\ -> " & intCopies & " Copies..."

DoEvents

.PrintOut Background:=False

.Documents.Application.PrintOut FileName:=conTEMPLATE_NAME,
Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=intCopies

.ActiveDocument.Close False
.Quit

End With

Set mobjWOrdApp = Nothing
End If

End Function

The code won't execute unless the chkFlag on the popup form
is True. If you add or change a printer in the future, you simply
put another printer name in the table. No need to go back and
modify your code.

just a (long winded) suggestion :-).
 
Back
Top