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).
How can I programmatically get a preview of a label from NiceLabel?
Getting a preview of a label from NiceLabel is quite easy. All you have to do is call GetLabelPreviewEx method of the INiceLabel interface. GetLabelPreviewEx has 6 parameters:
- file name: full path to the file where NiceLabel will generate image of the label. Extension part of the file name defines which graphics format will be used. You can use .bmp, .jpg, .png or .gif as the file name extension.
- width: width of image (in pixels)
- height: height of image (in pixels)
- label kind: there are 3 possible values that define the kind of label you want preview for; 0 – header label, main label, tail label
- label side: define whether you want to preview front side (use 0 as parameter value) or back side of the label (use 1 as value). Using back side of the label makes sense only if you have label designed as double sided.
- flag that defines whether to show border around preview or not (true or false)
Function will return true if graphics file was successfully created or false it file could not be created.
bool GetLabelPreviewEx(fileName, width, height, labelKind, labelSide, showBorder)
The following code will run NiceEngine, open label and get a preview of the label in a file called “preview.jpg” that is located in “c:\tmp”. Then you can load the image file in a component of your choice.
// Open NiceEngine NiceEngine5WR.NiceApp engine = new NiceEngine5WR.NiceApp(); // Open label NiceEngine5WR.NiceLabel label = engine.LabelOpenEx(“file.lbl”); string previewFileName = @"c:\tmp\preview.jpg"; int width = 640; int height = 480; previewStatus = label.GetLabelPreviewEx(previewFileName, width, height, 1, 0, false);
How can I programmatically get a list of variables from NiceLabel?
The following piece of code shows how you can programmatically get a list of variables from a label (.lbl) file. For this sample to work you will have to add reference to “niceengine5wr.dll” that is usually installed in the "c:\Program Files\Common Files\EuroPlus Shared\NiceEngine 5" folder. You will also have to compile your application as 32-bit (x86) application even if you want to run it on a 64-bit system, because NiceEngine and NiceLabel are 32-bit applications.
// Open NiceEngine
NiceEngine5WR.NiceApp engine = new NiceEngine5WR.NiceApp();
// Open label
NiceEngine5WR.NiceLabel label = engine.LabelOpenEx(“file.lbl”);
// List of variables where basic information about label variable will be stored.
// For this sample name, length and prompt order will be stored.
IList<Variable> variables = new List<Variable>();
if (label != null)
{
try
{
// Get a list of variables
NiceEngine5WR.IVariableList varList = label.Variables;
try
{
for (int i = 1; i <= varList.Count; i++)
{
NiceEngine5WR.IVar varIntf = varList.Item(i);
if (varIntf != null)
{
try
{
// Use only prompt variables that require prompt.
// Using other types of variables usually does not make sense.
if (varIntf.VarType == 1 && varIntf.DefType != 2)
{
Variable v = new Variable();
v.Name = varIntf.Name;
v.Length = varIntf.Length;
v.PromptOrder = varIntf.PromptOrder;
variables.Add(v);
}
}
finally
{
// Make sure that interfaces are correctly released otherwise you may get errors.
varIntf.Free();
varIntf = null;
}
}
else
{
// Show error
}
}
}
finally
{
varList.Free();
varList = null;
}
}
finally
{
// Close label by releasing the interface.
label.Free();
label = null;
}
}
Variable class definition looks like this:
public class Variable
{
public string Name
{
get;
set;
}
public string Value
{
get;
set;
}
public int Length
{
get;
set;
}
public int PromptOrder
{
get;
set;
}
}