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
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.
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
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()
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.
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.
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.
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.
In this sample we called it Letter.
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.
NOTE: We are using Letter here, but you should use whatever name you chose in step 2.
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.
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.
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.
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.
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.
|