更改报告中的打印机设置
Change printer settings in reports
当我更新我的 Access 应用程序时,我需要更改每台 PC 上的打印机设置 - 匹配附加的标签打印机名称,在每台打印机上使用特定的预定义标签,然后调整页边距。
我想使该过程自动化。但是我在获取报告以保存设置时遇到问题。
想法是将打印机设置导出到 table(完成),更新应用程序,然后读取每个报告的 "old" 设置并将其保存为更新的一部分。
问题是,我无法获取 Access 2010 来保存设置。
有什么想法吗?
Dim rpt As Access.Report
Dim rstUpdRptSettings As ADODB.Recordset
Dim NameOfPrinter As String
Set rstUpdRptSettings = New ADODB.Recordset
rstUpdRptSettings.Open "Select * From tblUpdRptSettings", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
Do While Not rstUpdRptSettings.EOF
DoCmd.OpenReport ReportName:=rstUpdRptSettings!ReportName, View:=acViewDesign, WindowMode:=acHidden
Set rpt = Reports(rstUpdRptSettings!ReportName)
NameOfPrinter = rstUpdRptSettings!PrinterName
With rpt
.Printer = Application.Printers(NameOfPrinter)
.Printer.TopMargin = rstUpdRptSettings!TopMargin
.Printer.BottomMargin = rstUpdRptSettings!BottomMargin
.Printer.LeftMargin = rstUpdRptSettings!LeftMargin
.Printer.RightMargin = rstUpdRptSettings!RightMargin
.Printer.PaperSize = rstUpdRptSettings!PaperSize
End With
DoCmd.Close acReport, rpt.Name, acSaveYes
rstUpdRptSettings.MoveNext
Loop
运行 代码不会更改任何设置 - 也不会更改打印机名称、页边距或纸张大小。
感谢您的评论。
我已经通过将此添加到代码中解决了这个问题:
If .DefaultView = 0 Then
.DefaultView = 1
Else
.DefaultView = 0
End If
'Set it back
If .DefaultView = 0 Then
.DefaultView = 1
Else
.DefaultView = 0
End If
我在这里找到了解释和代码(感谢大牛):
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/8061fb4a-2e5f-4abe-a1ab-493c3d733e82/set-report-margins-using-vba?forum=accessdev
回答者:
Daniel Pineault,2010-2016 微软 MVP
专业支持:http://www.cardaconsultants.com
MS Access 提示和代码示例:http://www.devhut.net
当我更新我的 Access 应用程序时,我需要更改每台 PC 上的打印机设置 - 匹配附加的标签打印机名称,在每台打印机上使用特定的预定义标签,然后调整页边距。
我想使该过程自动化。但是我在获取报告以保存设置时遇到问题。
想法是将打印机设置导出到 table(完成),更新应用程序,然后读取每个报告的 "old" 设置并将其保存为更新的一部分。
问题是,我无法获取 Access 2010 来保存设置。
有什么想法吗?
Dim rpt As Access.Report
Dim rstUpdRptSettings As ADODB.Recordset
Dim NameOfPrinter As String
Set rstUpdRptSettings = New ADODB.Recordset
rstUpdRptSettings.Open "Select * From tblUpdRptSettings", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
Do While Not rstUpdRptSettings.EOF
DoCmd.OpenReport ReportName:=rstUpdRptSettings!ReportName, View:=acViewDesign, WindowMode:=acHidden
Set rpt = Reports(rstUpdRptSettings!ReportName)
NameOfPrinter = rstUpdRptSettings!PrinterName
With rpt
.Printer = Application.Printers(NameOfPrinter)
.Printer.TopMargin = rstUpdRptSettings!TopMargin
.Printer.BottomMargin = rstUpdRptSettings!BottomMargin
.Printer.LeftMargin = rstUpdRptSettings!LeftMargin
.Printer.RightMargin = rstUpdRptSettings!RightMargin
.Printer.PaperSize = rstUpdRptSettings!PaperSize
End With
DoCmd.Close acReport, rpt.Name, acSaveYes
rstUpdRptSettings.MoveNext
Loop
运行 代码不会更改任何设置 - 也不会更改打印机名称、页边距或纸张大小。
感谢您的评论。
我已经通过将此添加到代码中解决了这个问题:
If .DefaultView = 0 Then
.DefaultView = 1
Else
.DefaultView = 0
End If
'Set it back
If .DefaultView = 0 Then
.DefaultView = 1
Else
.DefaultView = 0
End If
我在这里找到了解释和代码(感谢大牛): https://social.msdn.microsoft.com/Forums/sqlserver/en-US/8061fb4a-2e5f-4abe-a1ab-493c3d733e82/set-report-margins-using-vba?forum=accessdev
回答者: Daniel Pineault,2010-2016 微软 MVP 专业支持:http://www.cardaconsultants.com MS Access 提示和代码示例:http://www.devhut.net