How can I automate NiceLabel from a scripting language like VBScript or Python?

You can find two examples of automating NiceLabel from a scripting language. The first one is in VBScript and the other one in Python. Both samples do the same thing: run NiceLabel, open label file, iterate through a list of variables, set their values and print the label. At the end label is closed and NiceLabel shuts down.

Set nice = CreateObject("NiceLabel5.Application")
If (nice is Nothing) Then
  MsgBox("Cannot open NiceLabel!")
Else
  fileName =  "c:\Labels\Sample.lbl"
  Set lbl = nice.LabelOpenEx(fileName)

  If (lbl is Nothing) Then
      MsgBox("Cannot open '" & fileName & "'!")
  Else
     Set variables = lbl.Variables
     
     For i = 1 to variables.Count
       Set variable = variables.Item(i)
       ' Use only prompt variables that require prompt.
       If (variable.VarType = 1) and (variable.DefType <> 2) Then
         MsgBox(variable.Name)
         variable.SetValue("Value" & CStr(i))
       End if
       
       ' Release interface to variable.
       Set variable = Nothing    
     Next
     
     'Set printer
     lbl.PrinterName = "Altec TTP-343 Plus"
     
     ' Print 1 label
     quantity = "1"
     lbl.Print(quantity)
     
     ' Release interface to variables list.
     Set variables = Nothing
          
     ' Release interface to label to close it
     Set lbl = Nothing
  End If

  ' Close NiceLabel
  nice.Quit
  Set nice = Nothing
End if

And the same thing in Python.

import win32com.client
import os.path

# Run NiceLabel
nice = win32com.client.Dispatch("NiceLabel5.Application")
if (nice == None):
    error = "Cannot open NiceLabel";
else:
    fileName = os.path.normpath("c:/Users/alesp/Documents/My Labels/Labels/Sample.lbl");
    lbl = nice.LabelOpenEx(fileName);
    if (lbl == None):
        error = "Cannot open '{0}'".format(fileName);
    else:
        variables = lbl.Variables;
        for i in range (1, variables.Count + 1):
            variable = variables.Item(i);
            if (variable.VarType == 1) and (variable.DefType <> 2):
                variable.SetValue("Value" + str(i));
            # Release interface to variable.
            variable = None;

        # Set printer
        #lbl.PrinterName = "Altec TTP-343 Plus";

        # Print 1 label
        quantity = "1";
        lbl.Print(quantity);

        # Release interface to variables list.
        variables = None;

        # Release interface to variables list and close label.
        lbl = None;

    nice.Quit();
    nice = None;

Please note that these samples will only work on a desktop and not in NiceLabel Portal in a browser. You will also need to have NiceLabel 5 installed.

For more information about automating NiceLabel see the NiceLabel Programming Guide (PDF).

Posted in NiceLabel, SDK Tagged with: , , , , ,