strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True Install "192.168.5.150" sub Install(strIP) InstallPrinterPort strIP end Sub strBasePrinter = "Copier" strPrinterName = "RICOH IM C6000 PCL 6" strINFPath = "C:\Printers\IMC6000\disk1\OEMSETUP.INF" strIPPort = "IP_192.168.5.150" Set objShell = CreateObject("WScript.Shell") strCommand = "cmd /c rundll32 printui.dll,PrintUIEntry /if /b """ & strBasePrinter & """ /f " & strINFPath & " /r """ & strIPPort & """ /m """ & strPrinterName & """ & /Z" objShell.Run strCommand, 1, True Sub InstallPrinterPort(strIP) Set colInstalledPorts = objWMIService.ExecQuery _ ("Select Name from Win32_TCPIPPrinterPort") For each objPort in colInstalledPorts If objPort.Name="IP_" & strIP then exit sub Next Set objNewPort = objWMIService.Get _ ("Win32_TCPIPPrinterPort").SpawnInstance_ objNewPort.Name = "IP_" & strIP objNewPort.Protocol = 1 objNewPort.HostAddress = strIP objNewPort.PortNumber = "9100" objNewPort.SNMPEnabled = False objNewPort.Put_ end SubI'm trying to do the same now in vb.NET, with these two functions:
Private Function CreatePrinterIP(ByVal IPPort As String, ByVal IPPortName As String) Try Dim searcher As New ManagementObjectSearcher( "root\CIMV2", "SELECT * FROM Win32_TCPIPPrinterPort") For Each queryObj As ManagementObject In searcher.Get() If queryObj("Name") = IPPortName Then Return 1 Exit Function End If Next Dim objNewPort As ManagementObject = searcher.Get("Win32_TCPIPPrinterPort").SpawnInstance() objNewPort("Name") = IPPortName objNewPort("Protocol") = 1 objNewPort("HostAddress") = IPPort objNewPort("PortNumber") = "9100" objNewPort("SNMPEnabled") = False objNewPort.Put() Return 1 Catch err As ManagementException MessageBox.Show("An error occurred while querying for WMI data: " & err.Message) Return 0 End Try End Function Private Sub CreatePrinter(ByVal IPPortName As String, ByVal PrinterName As String, ByVal Location As String, ByVal Drivername As String) Dim cmdToSend As String = "rundll32 printui.dll,PrintUIEntry /if /b """ & Printername & """ /f " & Location & " /r """ & IPPortName & """ /m """ & Drivername & """ & /Z" Shell(cmdToSend) End SubThe CreatePrinterIP function worked once.
The conversion of string Win32_TCPIPPrinterPort to type Integer is invalid.on line
Dim objNewPort As ManagementObject = searcher.Get("Win32_TCPIPPrinterPort").SpawnInstance()Does anyone have an idea how to solve this?
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE