【问题标题】:Does VB6 have the ability to "cast" an object as type "Printer"?VB6 是否能够将对象“转换”为“打印机”类型?
【发布时间】:2020-10-09 18:08:17
【问题描述】:
Private Function SelectAPrinter(myName As String) As Printer

Dim strComputer As String
Dim objWMIService As Object
Dim myPrinter As Object

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set myPrinter = objWMIService.ExecQuery("Select * from Win32_Printer where Name = '" & myName & "'")

Set SelectAPrinter = myPrinter
  
End Function

当然,VB6 会以“类型不匹配”来攻击我,这并不奇怪。我正在尝试设置一个对象 (myPrinter) 输入打印机。

我不知道如何完成这个功能。 是否有一种将对象“强制转换”为打印机类型的方法?

【问题讨论】:

  • 让我回答为什么不使用vb​​6内置的PRINTERS集合的问题?答案是因为实际上有 500 多个重定向(许多似乎是重复的)填充客户端打印机集合。 WMI 解决了这个问题并允许我建立一个打印机列表 - 没有重定向或重复。

标签: vb6 wmi


【解决方案1】:

如果您不介意临时设置默认打印机,那么以下方法可能会起作用。


Option Explicit

Private Sub Command1_Click()
    Dim saveme As String
    Dim yourprinter As Printer
    
    saveme = Printer.DeviceName
    
    Set yourprinter = SelectAPrinter("Canon iR-ADV C5045/5051 PCL5c")
    
    Debug.Print "selected", yourprinter.DeviceName ' Microsoft Print to PDF
    
    Call SelectAPrinter(saveme) ' restore default
    
    Debug.Print "restored", Printer.DeviceName
End Sub

Private Function SelectAPrinter(myName As String) As Printer
    
    Dim strComputer As String
    Dim objWMIService As Object
    Dim colInstalledPrinters As Object
    Dim myPrinter As Object
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
    Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer where Name = '" & myName & "'")
    For Each myPrinter In colInstalledPrinters
        myPrinter.SetDefaultPrinter
        Exit For
    Next
    
    Set SelectAPrinter = Printer
      
End Function


【讨论】:

  • 非常感谢您提供的解决方案!是的,我可以忍受暂时设置默认打印机的轻微影响。我可能最终会负责追踪为什么所有这些重定向和多次发生(不是来自我们的代码)在 Windows 中,然后这个练习将没有实际意义。但是现在需要这个解决方案,您先生帮助我满足了需求! .
【解决方案2】:

您可以使用辅助方法将对象“投射”到打印机。但是,辅助方法确实使用了 Printers 集合:

Private Function SelectAPrinter(myName As String) As Printer
   Dim MyWMIService As Object
   Dim MyPrinters As Object
   Dim MyPrinter As Object
   
   Set MyWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
   Set MyPrinters = MyWMIService.ExecQuery("Select Name from Win32_Printer where Name = '" & myName & "'")
   
   For Each MyPrinter In MyPrinters
       Set SelectAPrinter = FindPrinter(MyPrinter.Name)
       Exit For
   Next
End Function

Private Function FindPrinter(ByVal DeviceName As String) As Printer
   Dim p As Printer
   
   For Each p In Printers
      If UCase(p.DeviceName) = UCase(DeviceName) Then
         Set FindPrinter = p
         Exit For
      End If
   Next
End Function

【讨论】:

  • 不幸的是,Printers 集合对我们来说对那个客户来说毫无用处。目前这个解决方案对我不起作用。或者,当然,我敢打赌,我的任务是解决数百个重定向和倍数问题。但那是另一个故事......谢谢你提供同样的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-10
  • 2016-10-19
  • 2018-11-27
  • 2020-07-29
  • 2022-01-19
  • 1970-01-01
相关资源
最近更新 更多