I am very new to programming and I am writing some code to work with Nicelabel 5.0 to display small pictures of allergens on a product label when the allergen is present within a string. I thought I really nailed it by using inStr to see if certain strings were present. I was even able to make the program distinguish between "FISH" and "SHELLFISH"
if inStr (1, Allergens1, "FISH") > 0 then
if inStr (1, Allergens1, "SHELLFISH") > 0 then
a = label.SetObjectVisible("SHELLFISH", true)
else
a = label.SetObjectVisible("FISH", true)
end if
end if
Where I'm stuck is what do I do if a product has BOTH shellfish and fish in it? The string "FISH" will always appear in the string "SHELLFISH" so how can I distinguish between the two?
Please let me know if more information is needed. The variable allergens1 is just a simple string ex.
"CONTAINS: EGG, FISH(SALMON), SHELLFISH(OYSTERS)"
I need to work within the confines of the labeling software which actually uses VBScript. I'm sorry for any confusion.
THANK YOU TO EVERYONE WHO HELPED WITH THIS! The final working code is below and should be noted will work with older version of Nicelabel that still use VBScript for label functions:
'''
Private Function GetWords(sText)
GetWords =
Split(Trim(Replace(Replace(Replace(Replace(Replace(Replace(sText, ":", " "), ",", " "), "(", " "), ")", " "), " ", " "), " ", " ")))
End Function
Dim vElem
dim a
a = label.SetObjectVisible("TREENUT",false)
a = label.SetObjectVisible("EGG",false)
a = label.SetObjectVisible("SOY", false)
a = label.SetObjectVisible("WHEAT", false)
a = label.SetObjectVisible("NUT", false)
a = label.SetObjectVisible("SHELLFISH", false)
a = label.SetObjectVisible("FISH", false)
a = label.SetObjectVisible("MILK", false)
a = label.SetObjectVisible("SESAME", false)
a = label.SetObjectVisible("SULFITE", false)
For Each vElem In GetWords(Allergens1)
a = label.SetObjectVisible(vElem,True)
Next
'''
Here is a one-liner which splits your string into words:
Private Function GetWords(sText)
GetWords = Split(Trim(Replace(Replace(Replace(Replace(Replace(Replace(sText, ":", " "), ",", " "), "(", " "), ")", " "), " ", " "), " ", " ")))
End Function
You can use this with For Each
like this:
Dim Allergens1
Dim vElem
Allergens1 = "CONTAINS: EGG, FISH(SALMON), SHELLFISH(OYSTERS)"
For Each vElem In GetWords(Allergens1)
WScript.echo vElem & " picture"
Next
. . . to get this:
CONTAINS picture
EGG picture
FISH picture
SALMON picture
SHELLFISH picture
OYSTERS picture