Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read from file and print a Label on Zebra 6 M?

Status
Not open for further replies.

AFK1

IS-IT--Management
Aug 26, 2005
38
US
I need some help with printing a label from file. I have a code that writes to the Zebra printer.
What I need is to change my code to read from file and then send that to printer to print the label. As if i need to change something I will have to change only in my file not in the program.

My code
------------------------
sFolder = c\test\test.zpl
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(sFolder) Then
fso.CreateFolder (sFolder)
End If
sPort = "LPT1:"
lCount = 0
Dim i As Integer
For i = 1 To mxlabels
Set txtfile = fso.CreateTextFile(sFileName, True)
If Label = "test" Then
txtfile.WriteLine ("^XA")
txtfile.WriteLine ("^PRB^FS")
txtfile.WriteLine ("^FT516,173^A0R,118,93^FD" & sQTY & "^FS")
txtfile.WriteLine ("^FO607,0^GB0,1218,2,B^FS")
txtfile.WriteLine ("^FT785,25^A0R,31,18^FDPART NO. (P)^FS")
txtfile.WriteLine ("^FT176,25^A0R,31,18^FDSERIAL" & Sfield & "^FS")
txtfile.WriteLine ("^FO404,0^GB0,1218,2,B^FS")
txtfile.WriteLine ("^FO201,0^GB0,1218,2,B^FS")
txtfile.WriteLine ("^PF0^FS")
txtfile.WriteLine ("^XZ")
End If
txtfile.Close
fso.CopyFile sFileName, sPort
'Pause after sending print job
Call cmn_sDoEvents(200)
Next
------------------------

 
I print labels to a zebra label print where the zpl file variables change. I read the ZPL file from an ini file and then replace the variables with the text needed and print the label like this. Reading the ZPL allows me to change the label and use to same variables whith modifing code.

Code:
Sub Print_Labels()
       
    Dim szTemp As String
    Dim Loc1 As String 
    Dim Stencil_PN As String 
    Dim Combine As String
    
    Loc1 = Mid(Me![txtStorageLocation], 4) 
    Stencil_PN = Me![txtSPN] '& "." & Me![txtRevEdit]
    Combine = cboCustomer & ";" & Stencil_PN & ";" & Loc1
    
    Call ReplaceAll("~PN~", Stencil_PN, g_strZPL)
    Call ReplaceAll("~LOC~", Loc1, g_strZPL)
    Call ReplaceAll("~BC_ALL~", Combine, g_strZPL)
      
End Sub

Loc1, Stencil_PN, and Combine are all variables in the ZPL file. The ReplaceAll function is where all the variables get populated with the text that need to be printed.

Code:
Function ReplaceAll(ByVal Find As String, ByVal Replace As String, ByRef szInStr As String) As String
  Dim iLoc As Integer
  Dim leftstr As String
  Dim rightstr As String
 
  iLoc = InStr(1, szInStr, Find, 1)
  While iLoc > 0
    leftstr = Left(szInStr, iLoc - 1)
    rightstr = Mid(szInStr, iLoc + Len(Find))
    szInStr = leftstr & Replace & rightstr
    iLoc = InStr(1, szInStr, Find, 1)
  Wend
  ReplaceAll = szInStr

End Function
Then in a code module I have.
Code:
Sub PrintOneLabel(g_strZPL)
Dim intPrintFile As Integer
Dim strErrMsg    As String
Dim strPrinter   As String

    strPrinter = "LPT1:"
    intPrintFile = FreeFile
    Open strPrinter For Output As #intPrintFile
    Print #intPrintFile, g_strZPL
    Close #intPrintFile
    
    Exit Sub
ERRORHANDLER:
MsgBox "An error occured while printing the label. " & Err.Description, vbCritical, "Error"

End Sub

I call this from a command button.

Code:
Private Sub cmdPrintLabel_Click()
MousePointer = 11
Print_Labels
PrintOneLabel g_strZPL
MousePointer = 0
End Sub

Hope this helps.
 
Thanks a lot for replying and helping but I have a question and it is Why to use an ini file. Why can't we use a simple text file? Please answer. Thanks
 
An ini file is where the original ZPL file is stored. This allows you to modify the ZPL file format with recompiling your VB code. You aren't "printing" the ini file, just reading you unmodified ZPL file from it. Example:

Code:
[Label]
Zpl=^XA^MCY^XZ^XA^LRN^FWN^CFD,24^LH0,0^CI0^PR3^MNY^MMT^MD0^PON^PMN^XZ^XA^A0N,110,110^FO373,13^CI0^FD[b]~LOC~[/b]^FS^A0N,60,60^FO390,138^CI0^FD[b]~PN~[/b]^FS^FO62,31^BXN,9,0,,,5,_^FD[b]~BC_ALL~[/b]^FS^XZ

ToolZPL=^XA^MCY^XZ^XA^LRN^FWN^CFD,24^LH0,0^CI0^PR3^MNY^MD0^PON^PMN^XZ^XA^A0N,54,52^FO172,21^CI0^FD[b]~PN~[/b]^FS^FO76,21^BXN,4,0,,,5,_^FD[b]~BC_ALL~[/b]^FS^A0N,42,42^FO179,74^CI0^FD[b]~LOC~[/b]^FS^XZ

I read these to ZPL's from the ini file and assign them to a global variable. I have the same 3 variables in both files but depending on the tool size I use 2 different labels. If I needed to change the label for whatever reason I could layout my new label, generate the new ZPL using the same variables and place it in the ini file and never have to touch my code.

Using
Code:
    intPrintFile = FreeFile
    Open strPrinter For Output As #intPrintFile
    Print #intPrintFile, g_strZPL
you don't have to actually make a txt file in order to print the ZPL file. HTH
 
Thanks for replying..
The reason I am asking is I have never worked with ini fiels. I don't know how to write the ini file and how to read from. Can you post sample code for ini and how to read from it. That will help me and others. Thanks
 
An ini file is a text file that can be written with notepad. The ini file has a section and a key name like this:

Code:
 [Label]
Zpl=^XA^MCY^XZ^XA^LRN^FWN^CFD,24^LH0,0^CI0^PR3^MNY^MMT^MD0^PON^PMN^XZ^XA^A0N,110,110^FO373,13^CI0^FD~LOC~^FS^A0N,60,60^FO390,138^CI0^FD~PN~^FS^FO62,31^BXN,9,0,,,5,_^FD~BC_ALL~^FS^XZ

[PrintServer]
PServerStencil=ZBR2424237
PServerTool=ZBR2248402

[DBName]
Production=\production.mdb
NonProduction=\test.mdb

Once you have the ini file as you need it in Notepad click File->Save As and in the Save As Type dropdown box chose ALL FILES then name it (myfile.ini) and save. If you don't ALL FILES then it will be saved with the txt extension (I think). It's a good idea to save the ini file in the same place as the vb applications exe file that you are coding so when you reference the file you can use App.Path.

To read from the ini file put this in a module.
Code:
Option Explicit
'******This module can be copied to other projects without modification*******

#If Win32 Then
    Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
#Else
    Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
#End If


Function StrTrimNulls(sMsg As Variant) As String
    'This strips all the spaces and extra characters
    'from the string
    '
    '

    Dim iPos As Integer, bContinue As Integer
    Dim sStr As String

    bContinue = True
    sStr = sMsg
    iPos = InStr(sStr, Chr$(0))
    Do Until (iPos = 0)
        Mid$(sStr, iPos, 1) = " "
        iPos = InStr(iPos, sStr, Chr$(0))
    Loop
    StrTrimNulls = Trim$(sStr)
End Function

Function WinPrivateProfileGet(sSection As String, KeyName As String, sDefault As String, sFName As String) As String
    
    ' Gets information from "ini" file
    ' Then run "StrTrimNulls" on string to
    ' eliminate the NULLs, then "Trims" off extra spaces
    
    Dim x As Integer
    Dim sRetString As String
    
    sRetString = Space$(500)
    x = GetPrivateProfileString(ByVal sSection, ByVal KeyName, ByVal sDefault, ByVal sRetString, 500, ByVal sFName)
    sRetString = StrTrimNulls(sRetString)
    WinPrivateProfileGet = Trim$(sRetString)



End Function

Then in you startup sub or startup form call the above function to read from the ini file like this:

Code:
Option Explicit
Global sZPL  As String
'___________________________________
Sub Main()
sZPL = WinPrivateProfileGet("Label", "ZPL", "", App.Path & "\yourfile.INI")
End Sub

Hope that helps.
 
I am trying both the codes and I got errors on both codes.

1. Code 1
When I click on Print and the code goes to "Print_Labels", I get an error
"By ref argumnet type mismatch" and g_strZPL is highlighted.


2. Code 2
Get error about constants on this line
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String,


Any ideas.
I exactly copied and pasted the code above.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top