【问题标题】:Changing printer in VBA for Access also change page layout在 VBA for Access 中更改打印机也会更改页面布局
【发布时间】:2016-03-31 16:45:20
【问题描述】:

我正在尝试为表单上的打印机选择提供控件。

名为cbxPrinterList 的组合框列出了打印机名称。当用户单击Command1 按钮时,应从选定的打印机打印CompanyHistory 报告。我怎样才能强制它在横向打印。

以下代码运行良好,但有一个例外。该报告采用横向设计。但它是纵向打印的。

Private Sub Command1_Click()

   Dim reportName As String
   reportName = "CompanyHistory"

   Dim vPrinter As Access.Printer

   Set vPrinter = Application.Printers(cbxPrinterList.ListIndex)
   vPrinter.Orientation = acPRORLandscape

   DoCmd.OpenReport reportName, View:=acViewPreview, WindowMode:=acHidden
   Set Reports(reportName).Printer = vPrinter

   DoCmd.OpenReport reportName, View:=acViewNormal
   Set Application.Printer = Nothing

   ' Close report without saving.
   DoCmd.Close ObjectType:=acReport, ObjectName:="Invoice", Save:=acSaveNo

End Sub

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    您最好采用稍微不同的方法来更改目标打印机以解决此问题。不要打开报表并更改报表打印机,而是尝试暂时更改 Microsoft Access 中的默认打印机,然后再将其更改回来。过去我从来没有遇到过这种方法的任何问题。

    首先,我不知道您的组合框是如何设置的,但我希望它提供打印机名称列表,以便选择的组合框返回打印机的windows DeviceName。下面是一些示例代码,用于用可用打印机列表填充组合框。你可以把它放在 Form_Open 事件中。

    Dim prt As Access.Printer
    
    'Populate the Printer Combo Box
    For Each prt In Application.Printers
        Me.cbxPrinterList.AddItem prt.DeviceName
    Next prt
    Set prt = Nothing
    

    您可能遇到的另一个问题是报告可能是针对特定打印机而不是默认打印机设置的。在报表的设计视图中,检查页面设置以确保将“公司历史打印机”设置为“默认打印机”(而不是“使用特定打印机”)。我发现当为特定打印机保存报表时,在运行时更改为不同的打印机时,可能会导致布局出现问题。

    然后,如下修改您的代码以在打印前更改默认打印机。

    Dim DefaultPrinter As String
    Dim ReportPrinter As String
    Dim prt As Access.Printer
    Dim reportName As String
    
    reportName = "CompanyHistory"
    ReportPrinter = cbxPrinterList
    
    'Get the current default printer so we can restore it after printing
    DefaultPrinter = Application.Printer.DeviceName
    'No need to change the printer if they chose the current default
    If ReportPrinter <> DefaultPrinter Then
        For Each prt In Application.Printers
            If prt.DeviceName = ReportPrinter Then
                Set Application.Printer = prt
                Exit For
            End If
        Next prt
    End If
    DoCmd.OpenReport reportName, acViewNormal
    If ReportPrinter <> DefaultPrinter Then
        Set Application.Printer = Application.Printers(DefaultPrinter)
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      相关资源
      最近更新 更多