VB6是否能够;铸造;“类型为”的对象;打印机;?

VB6是否能够;铸造;“类型为”的对象;打印机;?,vb6,wmi,Vb6,Wmi,所以VB6当然会用“类型不匹配”对我咆哮,这并不奇怪。我正在尝试设置一个对象 (myPrinter)以键入打印机 我不知道如何完成这个功能。 是否有一种将对象“强制转换”为类型打印机的方法?如果您不介意临时设置默认打印机,那么以下操作可能会起作用 Private Function SelectAPrinter(myName As String) As Printer Dim strComputer As String Dim objWMIService As Object Dim myPrin

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

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

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

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

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

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

可以使用助手方法将对象“强制转换”到打印机。但是,helper方法确实使用Printers集合:


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



可以使用助手方法将对象“强制转换”到打印机。但是,helper方法确实使用Printers集合:


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



让我来回答为什么不使用vb6内置的打印机集合的问题?答案是因为在clients-Printers集合中有500多个重定向(许多看起来是重复的)。WMI解决了这个问题,允许我建立一个打印机列表-没有重定向或重复。让我回答为什么不使用vb6内置的打印机集合的问题?答案是因为在clients-Printers集合中有500多个重定向(许多看起来是重复的)。WMI绕过了这一点,允许我建立一个打印机列表-没有重定向或重复。非常感谢您的解决方案!是的,我可以忍受临时设置默认打印机的痛苦。我可能最终会被指派去追踪为什么所有这些重定向和倍增都会在windows中发生(不是从我们的代码中发生),然后这个练习就没有意义了。但是现在需要的是这个解决方案,先生,您已经帮助我满足了这个需求。非常感谢您的解决方案!是的,我可以忍受临时设置默认打印机的痛苦。我可能最终会被指派去追踪为什么所有这些重定向和倍增都会在windows中发生(不是从我们的代码中发生),然后这个练习就没有意义了。但是现在需要的是这个解决方案,先生,您已经帮助我满足了这个需求。不幸的是,打印机集合已经变得对我们的客户毫无用处。目前这个解决方案对我不起作用。当然,我敢打赌,我的任务是解决数百个重定向和倍增问题。但那是另一个故事……谢谢你提供同样的东西。不幸的是,打印机收藏对我们的客户来说毫无用处。目前这个解决方案对我不起作用。当然,我敢打赌,我的任务是解决数百个重定向和倍增问题。但那是另一个故事……谢谢你提供了同样的东西。