Getting the Printer Port

Do you know how windows appends that “on Ne01? to your ActivePrinter. In the old days we had to loop through all the possible digits to find which one didn’t error out. Well no more!

Holger uses the registry to find the printer port. Very clever. However, if you have back slashes in your regestry key name, the scripting shell object won’t work for retrieving them. To the shell, back slashes are path separators, so it’s trying to navigate down some path that doesn’t exist.

To overcome that problem, you can use Registration Manipulation Classes.

If your you’re a late binding kind of a guy, use CreateObject("RegObj.Registry") in your code. With this dll, we can loop through all of the keys in a folder, like so:

Function GetPrinterPort(sPrinterName As String) As String
    Dim objReg As RegObj.Registry
    Dim objRootKey As RegObj.RegKey
    Dim sKey As String
    Dim objVal As RegObj.RegValue
    Dim sData As String
    Dim vData As Variant
    sKey = “HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionDevices”
    Set objReg = New RegObj.Registry
    Set objRootKey = objReg.RegKeyFromString(sKey)
    For Each objVal In objRootKey.Values
        If objVal.Name = sPrinterName Then
            sData = objVal.Value
            Exit For
        End If
    Next objVal
    If Len(sData) > 0 Then
        vData = Split(sData, “,”)
        GetPrinterPort = vData(UBound(vData))
        GetPrinterPort = “”
    End If
    Set objReg = Nothing
End Function

I haven’t tested that extensively, so use caution.

Posted in Uncategorized

3 thoughts on “Getting the Printer Port

  1. Simple point of grammar – I believe you meant to write, “If you’re a late binding kind of a guy…” in the text just below the first image. You typed “your” when “you’re” or “you are” seems more appropriate.

  2. OK, a message I posted yesterday didn’t come through, so I’ll express it differently.

    This is simple from the command line. Either

    rundll32 printui.dll,PrintUIEntry /Xg /n “\servernameprintername”

    which displays the information in a dialog, or adding another option to it,

    rundll32 printui.dll,PrintUIEntry /Xg /n “\servernameprintername” /f “filename.ext”

    which stores the information AS UNICODE TEXT in the file named filename.ext, or by querying the registry directly and parsing the result,

    for /F “skip=3 tokens=2 delims=,” %a in (‘reg query
    “HKCUSoftwareMicrosoftWindows NTCurrentVersionDevices”
    /v “\servernameprintername”‘) do echo %a > filename.ext

    which also stores the result in the file named filename.ext.

    All it takes is running one of these commands from VBA using Shell and reading filename.ext.

    Depressing how few presumably sophisticated Windows users still know how to use the command line.

  3. @fzz
    First command doesn’t help. There is no entry containing the “Ne0x:” info.
    Your second command works perfect.
    Thanks a lot:-)

Leave a Reply

Your email address will not be published. Required fields are marked *