Office中国论坛/Access中国论坛

标题: 报表打印问题 [打印本页]

作者: ha0148    时间: 2017-6-3 11:23
标题: 报表打印问题
我找了好多ACCESS的论坛,也没有找到,望大家给我介绍在什么地方有这样的示例,谢谢!
在窗体上点打印按钮打印报表,点按钮后先打开打印预览,然后调出系统的打印设置如:
[attach]61360[/attach]
打印机、页码范围、份数等设置好后,按确定打印。

作者: tmtony    时间: 2017-6-3 11:36
试一下
DoCmd.OpenReport stDocName, acViewPreview
'指定打印机
    Set rpt =
     Reports(stDocName).Printer = Printers(打印机索引号)
    DoCmd.OpenReport stDocName, acNormal

用DoCmd.PrintOut acPrintAll
后面的参数可设置从第几页 打印到第几页
作者: tmtony    时间: 2017-6-3 11:39
切换打印机

    Dim rpt As Report
    DoCmd.OpenReport ReportName:="rptOrders", View:=acViewDesign, WindowMode:=acHidden
    Set rpt = Reports!rptOrders
    rpt.Printer = Application.Printers("myPrinterName")
    DoCmd.Close acReport, "rptOrders", acSaveYes
    Set rpt = Nothing



Dim strReport as String
Dim strPrinter as String
Dim rpt as Report
Dim strPrinterName as String

strReport = “MyReport”
strPrinter = “Printer2”
         
'open report in design view to set printer
DoCmd.OpenReport ReportName:=strReport, View:=acViewDesign, WindowMode:=acHidden
            
Set rpt = Reports(strReport)

'get the current printer device name
strPrinterName = rpt.printer.DeviceName
Debug.Print strPrinterName

‘change printer
rpt.printer = Application.Printers(strPrinter)

‘get printer name again
strPrinterName = rpt.printer.DeviceName
Debug.Print strPrinterName

'close and save
DoCmd.Close acReport, strReport, acSaveYes
            
'open report again to see if new printer was saved
DoCmd.OpenReport ReportName:=strReport, View:=acViewDesign, WindowMode:=acHidden

Set rpt = Reports(strReport)

'get the current printer device name
strPrinterName = rpt.printer.DeviceName
Debug.Print strPrinterName

DoCmd.Close acReport, strReport, acSaveNo
            
=====================
IMMEDIATE WINDOW

Printer1

Printer2

Printer1

或切换打印机
Sub SwitchPrinter()
     Dim prt As Printer
     ' Get current default printer
     Set prt = Application.Printer
     ' Set default printer
     Application.Printer = Application.Printers("OtherPrinter")
     ' Print something, e.g.
     DoCmd.PrintOut
     ' Restore original printer
     Set Application.Printer = prt
End Sub

或列出所有打印机
Sub ListPrinters()
     Dim prt As Printer
     For Each prt In Printers
         Debug.Print prt.DeviceName
     Next prt
End Sub

作者: tmtony    时间: 2017-6-3 11:44
如果你不想自己代码控制,可以直接调用 Access内置的打印设置窗口

DoCmd.OpenReport "报表1", acViewPreview   '先预览
DoCmd.RunCommand acCmdPrint                  '打印,打印前会先弹出系统的打印设置窗口,可设置打印机,开始页 结束页等


作者: ha0148    时间: 2017-6-3 15:11
谢谢您的回复!我看了您最后一个,出来的和我的一样,您的代码比我简练,我的是:
On Error Resume Next
DoCmd.OpenReport "报表1", acPreview
SendKeys "^" & "p"
DoCmd.RunCommand acCmdPrint
这有 一个问题,就是点击后,出来的预览是空白,只有取消或点打印机设置的后才能看到数据。
我想的是点击后预览不是空白,而是有数据的报表预览。
作者: tmtony    时间: 2017-6-4 07:49
DoCmd.OpenReport "报表1", acPreview
doevents
DoCmd.RunCommand acCmdPrint

试试
作者: ha0148    时间: 2017-6-4 11:10
谢谢tmtony !!试了一下,是我要求的。我觉得这种打印报表的方式比较好,谢谢您的回复。谢谢!




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3