预选页面范围的acCmdPrint

问题描述 投票:0回答:1

在MS Access中,我想打印预选的页面,但我希望用户选择输出打印机。

我已经尝试了PrintOutacCmdPrint命令,但我想要它们的混合。

如果我使用PrintOut命令,我可以选择在参数中设置页面范围但是对话框没有显示,文档将打印到默认打印机。无法选择以这种方式选择打印机。

如果我使用acCmdPrint命令,我没有选择预先选择页面。完整文档的范围显示在页面选择中(从1到最后一页),我想选择1到3或5到9而不是1到最终页面。好的部分是用户可以选择以这种方式选择打印机。

打印机和页面选择?有帮助吗?谢谢。

vba printing access-vba
1个回答
1
投票

也许您可以尝试执行以下操作:只需进行打印预览而不是打印,然后在查看报告时,用户应该能够选择文件,打印...,这将显示他想要的选项,如页面范围。

DoCmd.OpenReport "report", acViewPreview
DoCmd.RunCommand acCmdPrint

更新1:您可以使用xlDialogPrinterSetup调用打印机选择对话框

Dim myprinter As String

' Back up default printer.
myprinter = Application.ActivePrinter

' Let the user select a printer.
If Application.Dialogs(xlDialogPrinterSetup).Show Then
    ' Print to the selected printer
    cmd.PrintOut Preview:=False, ActivePrinter:=Application.ActivePrinter
End If

' Restore original printer.
Application.ActivePrinter = myprinter

更新2:好的,如果它是Access,似乎xlDialogPrinterSetup它不可用。一种选择是您可以使用以下代码创建自己的打印对话框表单,该代码列出了所有可用的打印机。代码来自这个link

Sub ShowPrinters()
    Dim strCount As String
    Dim strMsg As String
    Dim prtLoop As Printer

    On Error GoTo ShowPrinters_Err

    If Printers.Count > 0 Then
        ' Get count of installed printers.
        strMsg = "Printers installed: " & Printers.Count & vbCrLf & vbCrLf

        ' Enumerate printer system properties.
        For Each prtLoop In Application.Printers
            With prtLoop
                strMsg = strMsg _
                    & "Device name: " & .DeviceName & vbCrLf _
                    & "Driver name: " & .DriverName & vbCrLf _
                    & "Port: " & .Port & vbCrLf & vbCrLf
            End With
        Next prtLoop
    Else
        strMsg = "No printers are installed."
    End If

    ' Display printer information.
    MsgBox Prompt:=strMsg, Buttons:=vbOKOnly, Title:="Installed Printers"

ShowPrinters_End:
    Exit Sub

ShowPrinters_Err:
    MsgBox Prompt:=Err.Description, Buttons:=vbCritical & vbOKOnly, _
        Title:="Error Number " & Err.Number & " Occurred"
    Resume ShowPrinters_End

End Sub

您只需要调整该代码以填充您自己的打印对话框表单中的列表框。然后,当用户选择其中一台打印机时,您可以执行以下操作:

Dim oldPrinter As Printer

' Save original printer.
Set oldPrinter = Application.Printer

' x is the list index of the selected printer of your form.
Application.Printer = Application.Printers.Item(x)

' Print the report here using PrintOut.

' Restore the printer.
Application.Printer = oldPrinter
© www.soinside.com 2019 - 2024. All rights reserved.