Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
I have some very simple data and code (culled from a larger application) where I'm trying to select multiple values of a field in a macro.
Here's the sample version:
LOAD * INLINE [
release, revisions
20.5, 140
20.6, 132
20.7, 129
20.8, 145
];
and two code snippets:
rem Using SelectValues, doesn't work
set releaseField = ActiveDocument.Fields("release")
set fieldValues = releaseField.GetNoValues
fieldValues.Add
fieldValues(0).Text = "20.5"
fieldValues.Add
fieldValues(0).Text = "20.6"
releaseField.SelectValues(fieldValues)
rem Using ToggleSelect *does* work
ActiveDocument.Fields("release").Select("20.5")
ActiveDocument.Fields("release").ToggleSelect("20.6")
ToggleSelect works to allow me to select multiple values of the field, but the seemingly more flexible and powerful SelectValues fails (returns false).
What subtlety about SelectValues am I missing? In other words, why is SelectValues failing?
This is using v8.5.
Thanks!
You're setting both values to the same array spot. You're overwritting the first value with the second value. Also, they set the IsNumeric properties of the array elements. Try this:
set releaseField = ActiveDocument.Fields("release")
set fieldValues = releaseField.GetNoValues
fieldValues.Add
fieldValues(0).Text = "20.5"
fieldValues(0).IsNumeric = false
fieldValues.Add
fieldValues(1).Text = "20.6"
fieldValues(1).IsNumeric = false
releaseField.SelectValues fieldValues
You're using text values for your field in your example, but they look like numbers. You may want to try this as well:
set releaseField = ActiveDocument.Fields("release")
set fieldValues = releaseField.GetNoValues
fieldValues.Add
fieldValues(0).Text = 20.5
fieldValues(0).IsNumeric = true
fieldValues.Add
fieldValues(1).Text = 20.6
fieldValues(1).IsNumeric = true
releaseField.SelectValues fieldValues
Thanks, the overwriting of values came from copying and pasting my code (always a mistake )
I tried what you suggested, but that doesn't work either. I get the feeling that there's something else I need to do to the field first, but I can't see an obvious method, or property to set.
EDIT: See below...
I've attached a doc with similar data to yours. I could not get it to select numeric values. It selects text values just fine.
There were some changes I needed to make to the code I gave you above. Here's what works for selecting text:
Sub Testing
set releaseField = ActiveDocument.Fields("letters")
set fieldValues = releaseField.GetNoValues
fieldValues.Add
fieldValues.Add
fieldValues(0).Text = "AAA"
fieldValues(0).IsNumeric = false
fieldValues(1).Text = "BBB"
fieldValues(1).IsNumeric = false
releaseField.SelectValues fieldValues
End Sub
This is a duplicate, sorry for the triple post (it's Friday )
Ah, I figured it out, you need to use .Number not .Text.
Sub TestingNum
set releaseField = ActiveDocument.Fields("release")
set fieldValues = releaseField.GetNoValues
fieldValues.Add
fieldValues.Add
fieldValues(0).Number = 20.5
fieldValues(0).IsNumeric = true
fieldValues(1).Number = 20.6
fieldValues(1).IsNumeric = true
releaseField.SelectValues fieldValues
End Sub