Views:

Product: LABELVIEW Gold and CODESOFT Enterprise

Version: Any version

 

The purpose of this article is to assist with making a shape's color change based on the character either on or next to it. This functionality is widely used for filing and is meant to help replicate Smead ColorBar labels. To get access to some pre-made label designs you can download the Smead ColorBar Samples Pack.

NOTE: This functionality requires VB Script capabilities. 

 

The Visual Basic Script

1. Go to Tools > Visual Basic Scripts Manager

 

2. Go into {NAME OF DOCUMENT} > Document Events > OnBeforePrint

 

3. Check Activate event call

 

4. Paste in the following information from one of the sub steps into OnBeforePrint and any additional events mentioned below.

NOTE: Using OnBeforePrint will make sure labels will always update before printing. If you would like to see the design update in the software we also suggest pasting the code into OnBeforeShowDialog. This will make the design change anytime a dialog is opened. If you find that the label is not showing properly after opening or saving, you can also use OnBeforeLoad or OnBeforeSave. These will ensure that the label opens with the correct colors and saves with the correct colors.

4a. Paste in the following information if you need to only change the color of shapes based on a character:

Dim obj, pos, character, lookFor, colorValue, color
for each obj in Doc.DocObjects.Shapes
On Error Resume Next
character = Document.Variables(obj.Name).Value
lookFor = Right(UCase(character),1) & "-"
pos = 2 + InStr(Document.Variables("ColorCode").Value,lookFor)
colorValue = Mid(Document.Variables("ColorCode").Value,pos,6)
color = "&H" & Right(colorValue,2) & Mid(colorValue,3,2) & Left(colorValue,2)
If character = "" Then color = "&HFFFFFF" End If
If Err.Number <> 0 Then
Err.Clear
Else
obj.BackColor = color
End If
next

 

4b. Paste in the following information if you need to change the color of shapes and letters.

Dim obj, pos, character, lookFor, colorValue, color
for each obj in Doc.DocObjects.Shapes
On Error Resume Next
character = Document.Variables(obj.Name).Value
lookFor = Right(UCase(character),1) & "-"
pos = 2 + InStr(Document.Variables("ColorCode").Value,lookFor)
colorValue = Mid(Document.Variables("ColorCode").Value,pos,6)
color = "&H" & Right(colorValue,2) & Mid(colorValue,3,2) & Left(colorValue,2)
If character = "" Then color = "&HFFFFFF" End If
If Err.Number <> 0 Then
Err.Clear
Else
obj.BackColor = color
End If
next

for each obj in Doc.DocObjects.Texts
On Error Resume Next
lookFor = obj.Value & "-"
pos = 2 + InStr(Document.Variables("ColorCode").Value,lookFor)
colorValue = Mid(Document.Variables("ColorCode").Value,pos,6)
color = "&H" & Right(colorValue,2) & Mid(colorValue,3,2) & Left(colorValue,2)
If Err.Number <> 0 Then
Err.Clear
Else
obj.ForeColor = color
End If
next
Document.Refresh()

5. Save 

 

Color Control

In order to fully utilize this tool you will need an advanced variables called ColorCode. The steps below explain how to create this variable and what it does.

1. Go to Data source > Advanced > Add

 

2. Change Name to ColorCode

 

3. Add values in the following format Character-RRBBGG;

Character - Each letter or number that has an assigned color.

Dash - A Dash is required for the VB Formulas to find the correct character.

RR - The Red hex value.

BB - The Blue hex value.

GG - The Green hex value.

Semi-colon - The separator of different character.

If we wanted A to make red shapes, B to make blue shapes, and C to make green shapes we would construct the following ColorCode:

A-FF0000;B-00FF00;C-0000FF

To get access to some pre-made color codes you can download the Smead ColorBar Samples Pack.

NOTE: If a character is empty or not found in the list the VB Script will make the shape white.

 

Shape Color Control

After you have one of the VB Scripts from the first section added and you have your color codes defined you can associate shapes to data sources.

1. Create a data source.

NOTE: The color of the shape will always be based on the last character of the data source referenced. In this sample we use a Formula, but you can use any data source.

2. Name your data source.

NOTE: In this sample we use the formula "A" but you can have anything in here as long as the last character appears in your ColorCode variable.

 

3. Make a mental note of the chosen name, as we will be using it later. 

In this sample we called it Letter.

 

4. Go to Object > Shape > Select any shape

 

5. You can now draw the shape on the label.

 

6. Double click on the shape to enter the properties

NOTE: The VB Script only changes the background for those who need a black border around their shape. If you DO NOT need a black border uncheck Border.

7. Go to the General tab. 

 

8. Change the name to the name of the data source this will be based on.

NOTE: We are using Letter here, but you should use whatever name you chose in step 2.

9. Click OK.

Now that both Shape and Data Source have the same name they will be connected by the VB Script. Based on which events you chose in step 4 of The Visual Basic Script section you should see the shape update properly. In our sample we have the events OnBeforePrint and OnBeforeShowDialog, so anytime we print or open a dialog we will see the update happen.

 

10. Click Print. If you are using OnBeforeShowDialog you'll see the update here.

NOTE: If you update the Formula to contain B or C you'll see the colors Blue and Green respectively based on what we assigned in step 3 of the Color Code section. If you choose a character that does not appear in ColorCode it will not update the color. If you leave the character empty it will change the shape color to white. 

 

11. If you are only using OnBeforePrint, Click Print to see the update take place.

 

Letter Color Control

If you are using the VB Script from step 4b of The Visual Basic Script section you will also be able to make a single character in a text box update to the color assigned in ColorCode. 

1. Add a Fixed Text.

 

2. Click on your label to place the text. 

 

 

3. Type in a character that appears in your ColorCode.

As long as the character is the only thing in the text box it will be connected to your ColorCode data source. Based on which events you chose in step 4 of The Visual Basic Script section you should see the shape update properly. In our sample we have the events OnBeforePrint and OnBeforeShowDialog, so anytime we print or open a dialog we will see the update happen. 

 

4. Click Print. If you are using OnBeforeShowDialog you'll see the update here.

NOTE: If you update the Formula to contain B or C you'll see the colors Blue and Green respectively based on what we assigned in step 3 of the Color Code section. If you choose a character that does not appear in ColorCode it will not update the color. If you leave the character empty it will change the shape color to white. 

 

5. If you are only using OnBeforePrint, Click Print to see the update take place.