After creating the file using the Zebra designer software, I manually edited with Notepad. Attached is the file.
The first thing my code does is to read data from the PLC and put it in a local variable:
Dim RecipeName As String = ""
Try
RecipeName = EthernetIPforCLXCom1.Read("Batch1RecipeName")
Catch ex As Exception
MsgBox("Failed to read Batch1RecipeName from PLC. " & ex.Message)
Exit Sub
End Try
It then reads the file and stores it in a string:
Dim s As String
Using sw As New System.IO.StreamReader("BatchTicketLabelFormat.prn")
s = sw.ReadToEnd()
End Using
Using Regular Expression, the string is parsed looking for the brackets
Dim x As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(s, "\[(.*?)\]")
Dim Substitue As String
While Not String.IsNullOrEmpty(x.Value)
If x.Value.IndexOf("Batch1RecipeName") >= 0 Then
Substitue = RecipeName
Else
Substitue = "ZZZZZZ"
End If
s = s.Substring(0, x.Index) & Substitue & s.Substring(x.Index + x.Length)
x = System.Text.RegularExpressions.Regex.Match(s, "\[(.*?)\]")
End While
And finally send the string directly to the printr:
RawPrinterHelper.SendStringToPrinter(RawPrinterHelper.DefaultPrinterName(), s)