【问题标题】:How to select a tray on a printer thorugh code when printing from a word document in VB (VS2019)VB中从word文档打印时如何通过代码选择打印机上的托盘(VS2019)
【发布时间】:2019-09-09 10:36:52
【问题描述】:

您好,我正在尝试创建一个 word 文档,然后在 Visual Studio 2019 中使用 VB 打印它。 创建工作正常,文档保存良好,并且可以正常打印到普通托盘,但我无法让应用程序将打印作业发送到指定托盘。打印件将来自默认纸张尝试

客户有一系列不同的打印机品牌和型号

我尝试通过word打印文档,我也尝试在计算机上更改打印机本身以设置托盘然后将其更改回来

试一试

将 intTray 调暗为 Integer = varibleNumber

  If intTray = 1 Then

     oWord.ActiveDocument.PageSetup.FirstPageTray = Word.WdPaperTray.wdPrinterUpperBin
     oWord.ActiveDocument.PageSetup.OtherPagesTray = Word.WdPaperTray.wdPrinterUpperBin

ElseIf intTray = 2 Then

     oWord.ActiveDocument.PageSetup.FirstPageTray = Word.WdPaperTray.wdPrinterMiddleBin
     oWord.ActiveDocument.PageSetup.OtherPagesTray = Word.WdPaperTray.wdPrinterMiddleBin

ElseIf intTray = 3 Then

     oWord.ActiveDocument.PageSetup.FirstPageTray = Word.WdPaperTray.wdPrinterLowerBin
     oWord.ActiveDocument.PageSetup.OtherPagesTray = Word.WdPaperTray.wdPrinterLowerBin

Else
    'else print default tray
     oWord.ActiveDocument.p.PageSetup.FirstPageTray = Word.WdPaperTray.wdPrinterDefaultBin
     oWord.ActiveDocument.PageSetup.OtherPagesTray = Word.WdPaperTray.wdPrinterDefaultBin
End If

尝试 2

Dim intTray As Integer = varibleNumber

Dim oPS As New System.Drawing.Printing.PrinterSettings


If intTray = 1 Then

   oPS.DefaultPageSettings.PaperSource = oPS.DefaultPageSettings.PrinterSettings.PaperSources.Item("Tray 1")
   oWord.ActiveDocument.PageSetup.FirstPageTray = "Tray 1"
   oWord.ActiveDocument.PageSetup.OtherPagesTray = "Tray 1"

ElseIf intTray = 2 Then

   oPS.DefaultPageSettings.PaperSource = oPS.DefaultPageSettings.PrinterSettings.PaperSources.Item("Tray 2")
   oWord.ActiveDocument.PageSetup.FirstPageTray = "Tray 2"
   oWord.ActiveDocument.PageSetup.OtherPagesTray = "Tray 2"

ElseIf intTray = 3 Then

   oPS.DefaultPageSettings.PaperSource = oPS.DefaultPageSettings.PrinterSettings.PaperSources.Item("Tray 3")
    oWord.ActiveDocument.PageSetup.FirstPageTray = "Tray 3"
    oWord.ActiveDocument.PageSetup.OtherPagesTray = "Tray 3"

Else

   'else print default tray
    oPS.DefaultPageSettings.PaperSource = oPS.DefaultPageSettings.PrinterSettings.PaperSources.Item("Automatically Select")

End If


modPrint.printWordDoc(oWord)

页面刚从打印机主托盘中出来。

非常感谢任何帮助

【问题讨论】:

    标签: vba printing


    【解决方案1】:

    我想到的一个快速解决方案是使用 VBA Printer Setup Dialog 更改打印机设置上的 Tray。尝试执行这行代码并更改您正在使用的打印机上的选项:

    Application.Dialogs(xlDialogPrinterSetup).Show
    

    编辑:

    尝试使用此特定于VB 框架的代码。这是一个有效的起点,因为它允许您选择要使用的打印:

    Public Sub Printing(printer As String)
        Try
            streamToPrint = New StreamReader(filePath)
            Try
                printFont = New Font("Arial", 10)
                Dim pd As New PrintDocument()
                AddHandler pd.PrintPage, AddressOf pd_PrintPage
                ' Specify the printer to use.
                pd.PrinterSettings.PrinterName = printer
    
                If pd.PrinterSettings.IsValid then
                   pd.Print()
                Else
                   MessageBox.Show("Printer is invalid.")
                End If
            Finally
                streamToPrint.Close()
            End Try
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    

    您可以在Microsoft Documentation 上找到更多信息。

    希望这会有所帮助。

    【讨论】:

    • 嘿,路易斯,谢谢您的回复。这确实是理想的,我忘了包括客户端使用这个软件一次踢出数百个,所以希望它“只是这样做”而不是让用户在每次打印之间点击
    • 您好,根据我在 Excel 上看到的内容,它会记住您所做的选择。所以应该是“一次性配置”。
    • 啊,太棒了,我会看看这个,我无能为力:) 非常感谢,如果它有效,请告诉你
    • 嘿 louis 害怕那行不通 VB 似乎没有那个类,也许这只是 VBA
    • 很遗憾听到它不起作用。检查更新答案,如果这是朝着正确方向迈出的一步,请告诉我。
    【解决方案2】:

    终于设法解决了这个问题 :) 每台打印机的每个托盘都有自己的托盘编号,即使它们被标记为 Tray1 Tray2 等等。

    所以必须查看 papersources 找到源名称何时等于我想要的托盘编号,然后获取该来源的 rawkind 值并将其用作分配给单词 firstpagetray 时的托盘编号

    Dim intTray As Integer = <tray number i'm looking for>
    Dim oPS As New System.Drawing.Printing.PrinterSettings
    Dim paper_Source As PaperSource = New PaperSource
    Dim i As Integer = 0
    
    For Each ps As PaperSource In oPS.PaperSources
    
        If ps.SourceName.Contains(intTray.ToString) Then
            i = ps.RawKind
            Exit For
        End If
    
    Next
    
    oWord.ActiveDocument.PageSetup.FirstPageTray = i
    

    希望这可以帮助其他有同样问题的人

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2023-03-30
      • 2023-04-09
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多