我创建了一个带有1个组合框的表单,该表单填充了本地网络上已安装的打印机。加载表单时选择默认打印机。当我选择另一台打印机时,我想通过一个按钮进行验证,成为默认打印机。无论如何,我搜索网络,我建立的是不起作用(可能是较旧的答案,而不是视觉基础2015)。所选的打印机未设置为默认值。 “prndf”是新的默认打印机。我将在这里发布代码:
Dim strComputer
Dim objWMIService
Dim col
strComputer = "."
objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
col = objWMIService.ExecQuery _
("Select * from Win32_Printer Where Name = 'prndf'")
For Each objPrinter In col
objPrinter.SetDefaultPrinter()
Next
答案 0 :(得分:0)
试试这个:
将此功能放入您的班级,并使用SetDefaultPrinter(PrinterName)
进行调用。
<DllImport("winspool.drv", CharSet:=CharSet.Auto, SetLastError:=True)>
Public Shared Function SetDefaultPrinter(Name As String) As Boolean
End Function
答案 1 :(得分:0)
我也遇到过这个问题。经过多次搜索后,我找到了一个解决方案的帖子:
Public defaultPrinterNameHardcoded As String = "\\Printserver01\P20451"
DefaultPrinter.DefaultPrinterName = defaultPrinterNameHardcoded
Public Class DefaultPrinter
#Region "GetDefaultPrinter"
<DllImport("winspool.drv", EntryPoint:="GetDefaultPrinter", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=False, _
CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function GetDefaultPrinter(ByVal pszBuffer As System.Text.StringBuilder, _
ByRef BufferSize As Int32) As Boolean
End Function
#End Region
#Region "SetDefaultPrinter"
<DllImport("winspool.drv", EntryPoint:="SetDefaultPrinter", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=False, _
CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function SetDefaultPrinter(ByVal PrinterName As String) As Boolean
End Function
#End Region
Public Shared Property DefaultPrinterName() As String
Get
'\\ Go through the list of printers and return the default one
Dim lpsRet As New System.Text.StringBuilder(256), chars As Integer = 256
If GetDefaultPrinter(lpsRet, chars) Then
End If
Return lpsRet.ToString
End Get
Set(ByVal value As String)
'\\ Go through the list of printers and if you find the one named as above make it the default
If Not SetDefaultPrinter(value) Then
Trace.WriteLine("Failed to set printer to : " & value)
End If
End Set
End Property
End Class