changing text colour based on another variable

Issues related to label design (working with databases, data processing, RFID encoding etc.) and printing (from NiceLabel Express, NiceLabel Pro, NiceForm and NicePrint)

Moderators: Georges, milos, NiceLabel Support Team

Post Reply
spincemaille
Enthusiast
Posts: 11
Joined: Sat Sep 17, 2011 11:02 am

changing text colour based on another variable

Post by spincemaille » Mon Nov 12, 2012 1:17 pm

It is my first question on this forum and I hope somebody could help me/
I have a text in a Textbox and i should like to change the colour of the text based on another variable:

Textbox11 uses the variable VsRefLang$T1
The color of the text should change based on the value of another variable: _Ref

If
_Ref = 1301 Colour RGB(128,0,128)
_Ref = 1301 Colour RGB(0,0,255)
_Ref = 1301 Colour RGB(255,,0,)
_Ref = 1301 Colour RGB(0,128,0,)
Following the Nicelabel guide, one should make a script in visual basic and the output variable should be used by the TextBox11

Is it possible?
How should it be programmed in Nicelabel pro 5.2

Greetings and thanks for all help

User avatar
Saso
NiceLabel
NiceLabel
Posts: 2980
Joined: Mon Sep 04, 2006 8:09 am
Contact:

Re: changing text colour based on another variable

Post by Saso » Mon Nov 12, 2012 1:34 pm

You can start in this thread and this thread.

Posts contain instructions how to do it and sample label with the VB Script code that changes color of the object.
Saso Fleiser
Senior Technical Product Specialist

spincemaille
Enthusiast
Posts: 11
Joined: Sat Sep 17, 2011 11:02 am

Re: changing text colour based on another variable

Post by spincemaille » Mon Nov 12, 2012 3:19 pm

Hello

I understand theoretically what should be done, but I can not see the practical implementation.

For example what should be the following in my case?
label.objects.item(counter).Name

Thanks in advance

User avatar
Saso
NiceLabel
NiceLabel
Posts: 2980
Joined: Mon Sep 04, 2006 8:09 am
Contact:

Re: changing text colour based on another variable

Post by Saso » Mon Nov 12, 2012 3:39 pm

Each object has an internal ID that you do not see and do not know about.

Before you can do anything with the object, you have to find it in this hidden internal list.
So, if have 10 objects on the label, you know the first one will have value for ID set to 1 and the last will have value 10. But you don't know which is first and which is last.

You have to iterate through the list of objects and find your object by name.

That's why the sample label contains a VBScript that looks for an object named "Counter":

Code: Select all

For i = 1 To MaxObjects

   ObjectName = label.objects.item(i).Name
   if (ObjectName = "Counter") Then
        ' Do your color change here for the object named "Counter"

        exit for
        ' You have found the object, so exit loop, no need to continue the search
   end if
Next
Where to define the object name?
In object properties -> Appearance tab -> Advanced -> Object name

Let me know if you need additional help on this.
Saso Fleiser
Senior Technical Product Specialist

spincemaille
Enthusiast
Posts: 11
Joined: Sat Sep 17, 2011 11:02 am

Re: changing text colour based on another variable

Post by spincemaille » Mon Nov 12, 2012 4:54 pm

Hello

Thank you very much your answer.
I did n't know there was an internal ID for objects and the way to look for it.
Has this been described in one of your guides or documentation?

Thanks in advance
W. Spincemaille

spincemaille
Enthusiast
Posts: 11
Joined: Sat Sep 17, 2011 11:02 am

Re: changing text colour based on another variable

Post by spincemaille » Tue Nov 13, 2012 1:20 pm

I did the following

I made a textbox2 who uses the variable Vbscript1
I did a print preview to see what should happen
As I understand this script should then change the color of the text in the Textbox11
But nothing happened
What's wrong?

------
dim i, MaxObjects, OjectName, ObjectColor
For i = 1 To MaxObjects
ObjectName = label.objects.item(i).Name
ObjectColor = label.objects.item(i).color
if (ObjectName = "TextBox11") Then
If [_Ref]="1301" Then
ObjectColor = RGB(128,0,128)
Else
ObjectColor = RGB(0,0,255)
End If
Exit for
End If
Next
Result = "1"
---------

User avatar
Saso
NiceLabel
NiceLabel
Posts: 2980
Joined: Mon Sep 04, 2006 8:09 am
Contact:

Re: changing text colour based on another variable

Post by Saso » Thu Nov 15, 2012 2:41 pm

A few problems:
  • You didn't define value for MaxObjects. This isn't defined anywhere, so you have to put in some reasonable number.
  • If you use syntax:

    Code: Select all

    ObjectColor = RGB(128,0,128)
    you define value to variable ObjectColor, you don't send the value to the label object.
    To do so you have to use:

    Code: Select all

    label.objects.item(i).color = RGB(128,0,128)
  • You have to use VBScript resulting variable on the label, or the script will not execute. You can define the object as non-printable.
The final script would look like this:

Code: Select all

MaxObjects = 20
dim i, MaxObjects, OjectName, ObjectColor
For i = 1 To MaxObjects
    ObjectName = label.objects.item(i).Name
    ObjectColor = label.objects.item(i).color
    if (ObjectName = "Text Box1") Then
        If [_Ref]="1301" Then
            label.objects.item(i).color = RGB(128,0,128)
        Else
            label.objects.item(i).color = RGB(0,0,255)
        End If
    Exit for
    End If
Next
Result = "1"
See the attached label.
Attachments
label.lbl
(138 KiB) Downloaded 204 times
Saso Fleiser
Senior Technical Product Specialist

User avatar
Saso
NiceLabel
NiceLabel
Posts: 2980
Joined: Mon Sep 04, 2006 8:09 am
Contact:

Re: changing text colour based on another variable

Post by Saso » Thu Nov 15, 2012 2:46 pm

One update to the script.
Instead of manually defining the MaxObjects and estimating the number, you can get the exact number of label objects.

Replace this:

Code: Select all

MaxObjects = 20
dim i, MaxObjects, ObjectName, ObjectColor
For i = 1 To MaxObjects
with this:

Code: Select all

dim i, ObjectName, ObjectColor
For i = 1 To label.objects.count
Saso Fleiser
Senior Technical Product Specialist

spincemaille
Enthusiast
Posts: 11
Joined: Sat Sep 17, 2011 11:02 am

Re: changing text colour based on another variable

Post by spincemaille » Thu Nov 15, 2012 11:06 pm

It works fine now

The only small problem was that when checking the syntax i got an error :
1.Error ID: 92003
Error description: 'Script error:'
Detailed information: 'Object required: 'label'
Line number:2

That was for the line: For i = 1 To label.objects.count
But it did not harm the functioning of the script.
Maybe a problem coming from the version I have: 5.4.1 (build 6941)

Thanks very much for your intervention
W. Spincemaille

User avatar
Saso
NiceLabel
NiceLabel
Posts: 2980
Joined: Mon Sep 04, 2006 8:09 am
Contact:

Re: changing text colour based on another variable

Post by Saso » Wed Nov 21, 2012 9:43 am

Sometimes such warnings in VB Script are caused by label not being previewed at least once. It might sound a bit strange, but previewing the label actually helps, because at that time all variables are assigned with values. ;-)

Also see the Knowledge Base article KB186: Tips and Tricks Using VB Scripting in NiceLabel software.

The latest version of NiceLabel software in generation 5 is 5.4.4. It might not be bad idea to upgrade to the latest in V5 or even to V6.
Saso Fleiser
Senior Technical Product Specialist

cscollen
Newcomer
Posts: 1
Joined: Thu Jun 25, 2020 1:19 am

Re: changing text colour based on another variable

Post by cscollen » Thu Jun 25, 2020 9:54 am

Hi,
I know this is a very old post but i am after the same thing but i am just a lowly coffee roaster so have no knowledge of vsbasics or python...
I have a visual of how i want my labels to look with the text colour matching the colour of an image (i have four different colours needed)

is somebody able to point me in the right direction for a very comprehensive how to guide... or are there any platforms i can pay to have this design done?

Cheers

Chris

Post Reply