Search code examples
excelvbazebra-printerszpl-ii

How do I permanently set the port to 9100 when printing from Excel VBA to the "ZPL Printer" Zebra label printer Chrome plug-in emulator


I installed the ZPL Printer emulator Chrome plugin yesterday and I'm able to send jobs to it just fine from NotePad. The VBA code below appears to be sending data to the emulator as well given the message that pops up momentarily on the bottom of the window saying that data was received. However, it's clear from the message that the port number is dynamic. On first pass, the message said "686 bytes received from Client 127.0.0.1 Port 52616." Additional runs list the port as 52635, 52649, and so on up the line (they do appear to be increasing). As this emulator is listening to port 9100, none of the jobs are printing on the screen...it's blank.

How do I modify the code below to set the port 9100 for all jobs?

Sub FourByTwo()
    Dim MyPrinter, NumLabels As Integer
    MyPrinter = "\\127.0.0.1\ZebraSim"
    NumLabels = 1
    Open MyPrinter For Output As #1
    Print #1, "^PQ" & NumLabels
    Print #1, "^LT0"
    Print #1, "^MD3"
    Print #1, "^POI"
    Print #1, "^CI34^FO0,730^A0,20,18,E:ARIALNB.FNT^FH^FDSample 1^FS"
    Print #1, "^CI34^FO0,670^A0,20,18,E:ARIALNB.FNT^FH^FDSample 2^FS"
    Print #1, "^CI34^FO0,610^A0,20,18,E:ARIALNB.FNT^FH^FDSample 3^FS"
    Print #1, "^CI34^FO0,550^A0,20,18,E:ARIALNB.FNT^FH^FDSample 4^FS"
    Print #1, "^CI34^FO0,490^A0,20,18,E:ARIALNB.FNT^FH^FDSample 5^FS"
    Print #1, "^CI34^FO0,430^A0,20,18,E:ARIALNB.FNT^FH^FDSample 6^FS"
    Print #1, "^CI34^FO0,370^A0,20,18,E:ARIALNB.FNT^FH^FDSample 7^FS"
    Print #1, "^CI34^FO0,310^A0,20,18,E:ARIALNB.FNT^FH^FDSample 8^FS"
    Print #1, "^CI34^FO0,250^A0,20,18,E:ARIALNB.FNT^FH^FDSample 9^FS"
    Print #1, "^CI34^FO0,190^A0,20,18,E:ARIALNB.FNT^FH^FDSample 10^FS"
    Print #1, "^CI34^FO0,130^A0,20,18,E:ARIALNB.FNT^FH^FDSample 11^FS"
    Print #1, "^CI34^FO0,70^A0,20,18,E:ARIALNB.FNT^FH^FDSample 12^FS"
    Close #1
End Sub

Note that when I try to determine the port with the GetPrinterPort2 function, the function returns a null string...

Public Function GetPrinterPort2(strPrinterName As String) As String
    Dim objReg As Object, strRegVal As String, strValue As String
    Const HKEY_CURRENT_USER = &H80000001
    Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    strRegVal = "Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts\"
    objReg.GetStringValue HKEY_CURRENT_USER, strRegVal, strPrinterName, strValue
    GetPrinterPort2 = Mid$(strValue, 10, 5)
End Function
Sub FourByTwo()
    Dim MyPrinter As String, NumLabels As Integer, ZebraSimPort
    MyPrinter = "\\127.0.0.1\ZebraSim"
    ZebraSimPort = GetPrinterPort2(MyPrinter)
    '...
End Sub

Setting ActivePrinter to ZebraSim on 9100 using the GetPrinterPort2 function doesn't cause any errors, but the simulator does not display the "bytes received" message with the following code, suggesting that the connection to the printer was not established:

Public Function GetPrinterPort2(strPrinterName As String) As String
    Dim objReg As Object, strRegVal As String, strValue As String
    Const HKEY_CURRENT_USER = &H80000001
    Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    strRegVal = "Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts\"
    objReg.GetStringValue HKEY_CURRENT_USER, strRegVal, strPrinterName, strValue
    GetPrinterPort2 = Replace(Mid$(strValue, 10, 5), ",", "")
End Function
Sub FourByTwo()
    Dim strPrinter As String, actPrinter As String, numLabels As Integer
    numLabels = 1
    With Application
        actPrinter = .ActivePrinter
        strPrinter = "ZebraSim"
        .ActivePrinter = strPrinter & " on " & GetPrinterPort2(strPrinter)
        Open .ActivePrinter For Output As #1
        Print #1, "^PQ" & numLabels
        Print #1, "^LT0"
        Print #1, "^MD3"
        Print #1, "^POI"
        Print #1, "^CI34^FO0,730^A0,20,18,E:ARIALNB.FNT^FH^FDSample 1^FS"
        Close #1
        .ActivePrinter = actPrinter
    End With
End Sub

Please note that as I will move back to printing to a real Zebra printer in the end, I don't want to have modify how the ZPLII code is sent to the printer just to get this simulator to work. In other words, I want to stick with the Open #1/Print #1/Close #1 technique to send commands to the printer. Basically, I just want to be able to easily toggle between simulator and real printer.

Using MyPrinter = "\\127.0.0.1\ZebraSim:9100" also results in the "bytes received" message, but again nothing prints out.

Sub FourByTwo()
    Dim MyPrinter As String, NumLabels As Integer
    MyPrinter = "\\127.0.0.1\ZebraSim:9100"
    NumLabels = 1
    Open MyPrinter For Output As #1
    Print #1, "^PQ" & NumLabels
    Print #1, "^LT0"
    Print #1, "^MD3"
    Print #1, "^POI"
    Print #1, "^FO100,100^A0,25,25^FDSample 1^FS"
    Close #1
End Sub

I also discovered when testing this that a PDF file is not generated in the destination folder when the Save labels checkbox is checked in the Settings dialog of the ZPL Printer interface. When printing from NotePad, this feature worked just fine (a file was made that contained a correctly generated label). Since the program reported that bytes were received, I would have expected an empty PDF to appear in the destination folder.

Also note that I simplified that line that prints "Sample 1" thinking that the character map and the font specification might be affecting things. The original code (with the font spec) worked fine from NotePad, but I thought I'd go ahead an rule it out.


Solution


  • I moved the ZPLII code back into Notepad to verify that I could still print from that app...I could not. It was because the the XA and XZ commands were missing all along. Somewhere along in the process, I got distracted by the whole port issue and forgot to include these crucial lines in the code.

    So now the following code prints to the next available port, which is not really what I wanted to do (I wanted to force it to print to 9100 every time), but it works for what I need it to do.

    Also, if I noticed that if ":9100" is not present in the line MyPrinter = "\\127.0.0.1\ZebraSim:9100" then the PDF file does not get generated. I still get a simulated print-out in the ZPL Printer window, but no file is generated. Thanks to Delphi Coder for that suggestion as it will be a good thing to have those PDFs on-hand if I need them.

    Sub FourByTwo()
        Dim MyPrinter As String, NumLabels As Integer
        MyPrinter = "\\127.0.0.1\ZebraSim:9100"
        NumLabels = 1
        Open MyPrinter For Output As #1
        Print #1, "^XA"
        Print #1, "^PQ" & NumLabels
        Print #1, "^LT0"
        Print #1, "^MD3"
        Print #1, "^POI"
        Print #1, "^FO100,100^A0,25,25^FDSample 1^FS"
        Print #1, "^XZ"
        Close #1
    End Sub