【问题标题】:Choosing a default printer from a list instead of hard coding it in从列表中选择默认打印机,而不是硬编码
【发布时间】:2020-08-13 15:51:39
【问题描述】:

我目前有一个用于批量打印的宏。它完美地工作;但是,打印选项是通过使用以下代码设置默认打印机来硬编码的。

CreateObject("WScript.Network").SetDefaultPrinter "\\vs-dc.CCC.internal\RICOH MPC3503 - Office 2nd Flr WKRM"

有没有人知道一种方法可以打开一个包含网络上所有打印机选项的对话框,以便任何人都可以使用它?它不必将默认设置恢复为原始设置,但如果这样做,那将是一个绝对的优势。

我的想法是让表单以某种方式将您选择的打印机设置为 Selected printer = ChosenPrinter,然后使用它。

CreateObject("WScript.Network").SetDefaultPrinter ChosenPrinter

我还看到了如何使用以下方法将其设置回原始默认值:

ActivePrinter = OrigPrinter 完成后,您将其设置回原样。

如果有人知道如何将所有这些整合在一起,那就太棒了。如有必要,我将在下面包含完整的代码。

Sub GetFiles(StartFolder As String, Pattern As String, _
         DoSubfolders As Boolean, ByRef colFiles As Collection)

Dim f As String, sf As String, subF As New Collection, s

If Right(StartFolder, 1) <> "\" Then StartFolder = StartFolder & "\"

f = Dir(StartFolder & Pattern)
Do While Len(f) > 0
    colFiles.Add StartFolder & f
    f = Dir()
Loop

sf = Dir(StartFolder, vbDirectory)
Do While Len(sf) > 0
    If sf <> "." And sf <> ".." Then
        If (GetAttr(StartFolder & sf) And vbDirectory) <> 0 Then
                subF.Add StartFolder & sf
        End If
    End If
    sf = Dir()
Loop

For Each s In subF
    GetFiles CStr(s), Pattern, True, colFiles
Next s

End Sub

.

Sub BatchPrint()

Dim colFiles As New Collection
Dim CustRow, LastRow As Long

Set colFiles = New Collection

CreateObject("WScript.Network").SetDefaultPrinter "\\vs-dc.CCC.internal\RICOH MPC3503 - Office 2nd Flr WKRM"

LastRow = Sheet1.Range("B9999").End(xlUp).Row
Dim countFiles As Integer 'Storing the number of files found
With Sheet1

For CustRow = 3 To LastRow
countFiles = colFiles.Count
GetFiles "C:\Users\Desktop\Test\", Sheet1.Range("B" & CustRow) & ".pdf", True, colFiles
If countFiles = colFiles.Count Then
Sheet1.Range("B" & CustRow).Interior.ColorIndex = 3
End If
Next CustRow

End With

Dim i As Long
For i = 1 To colFiles.Count
Debug.Print colFiles(i)

PrintFile (colFiles(i))

Next i

Set colFiles = Nothing

End Sub

【问题讨论】:

  • Application.Dialogs(xlDialogPrinterSetup).Show 可能对你有用
  • 我尝试使用上面的解决方案,但由于我有一个集合,它会显示在集合中打印的每个项目。如果有一种方法只需按一次,那么它对我有用,但我想不通
  • 哪种解决方案?听起来@TimWilliams 提出的解决方案对您有用,因为xlDialogPrinterSetup 对话框将设置活动打印机,因此您可以让用户在开始时选择一次活动打印机。
  • xlDialogPrinterSetup 让我选择打印机;但是,该集合仍然打印到“默认”而不是“活动”打印机。至少这就是我正在发生的事情。

标签: excel vba printing


【解决方案1】:

如果您从 Excel 打印,您可以简单地使用

Application.Dialogs(xlDialogPrinterSetup).Show

让用户在宏的开头选择活动打印机(用于 Excel)(如 Tim Williams 所建议的那样)。

如您所述,这不会更改 Windows 的默认打印机,因为活动打印机只是 Excel 应用程序的本地设置。

由于您提到代码中的过程/函数PrintFile 使用默认打印机,因此您可以在设置ActivePrinter 后立即设置默认打印机:

CreateObject("WScript.Network").SetDefaultPrinter Left(ActivePrinter, Len(ActivePrinter) - 9)

请注意,我们需要从 ActivePrinter 字符串中删除最后 9 个字符,因为 ActivePrinter 包含有关打印机端口号的信息,类似于“on Ne##:”。

【讨论】:

  • 我实际上只是尝试过这个,当我按照那个顺序使用它时,它会为CreateObject("WScript.Network").SetDefaultPrinter ActivePrinter 行抛出一个错误,我不知道为什么这不起作用
  • @JoshL – 你是对的,你需要做一个小的调整来完成这项工作。我已经编辑了我的答案以反映这一点。
猜你喜欢
  • 2017-03-17
  • 2017-07-07
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-13
  • 1970-01-01
相关资源
最近更新 更多