I have a Word document, written by someone else in my company, with a macro. The purpose is to dynamically populate one list of choices based on a choice in a previous list. As you can see below, the macro creates some static arrays and then checks the value from the first list 'process' and fills 'subprocess' with the associated list.
An abbreviated sample of the code is at the bottom.
This code is not very dynamic and cumbersome to allow for new values. The person that adds and updates the arrays doesn't understand the code enough to change it properly. A new choice was added to 'process' and then all the 'subprocess' choices no longer matched. That is why I was called to help. I figured out how to get it working, but realized how painful it was to change.
The number of arrays is around 20 in the full document. The array with the most elements has UBound = 27. When a new choice was added to the first dropdown list it created the need to change most of the hard-coded values used in the Select Case.
I thought I would be able to use a dynamic string to supply the correct array to the list, but was unable to figure out how to do so.
Should the code remain as it is or be changed to be more dynamic, and hopefully easier to manipulate when new lists are added? Does anyone know a better way?
This code is for Microsoft Word 2010 using Windows 7 OS.
Thanks
An abbreviated sample of the code is at the bottom.
This code is not very dynamic and cumbersome to allow for new values. The person that adds and updates the arrays doesn't understand the code enough to change it properly. A new choice was added to 'process' and then all the 'subprocess' choices no longer matched. That is why I was called to help. I figured out how to get it working, but realized how painful it was to change.
The number of arrays is around 20 in the full document. The array with the most elements has UBound = 27. When a new choice was added to the first dropdown list it created the need to change most of the hard-coded values used in the Select Case.
I thought I would be able to use a dynamic string to supply the correct array to the list, but was unable to figure out how to do so.
Should the code remain as it is or be changed to be more dynamic, and hopefully easier to manipulate when new lists are added? Does anyone know a better way?
Code:
Sub FieldExit()
Dim P9221(20) As String
Dim P9385(20) As String
Dim var
P9221(0) = "0010 IT budget"
P9221(1) = "0050 IT management"
P9385(0) = "0100 Management"
P9385(1) = "0150 Research"
P9385(2) = "0200 Markets"
P9385(3) = "0800 Safety"
P9385(4) = "0850 Projects"
' use the value of the dropdown to case select condition
ActiveDocument.FormFields("subprocess").DropDown.ListEntries.Clear
Select Case ActiveDocument.FormFields("process").DropDown.Value
Case 2
For var = 1 To 2
ActiveDocument.FormFields("subprocess").DropDown.ListEntries.Add Name:=P9221(var - 1)
Next
Case 3
For var = 1 To 5
ActiveDocument.FormFields("subprocess").DropDown.ListEntries.Add Name:=P9385(var - 1)
Next
End Select
End Sub
This code is for Microsoft Word 2010 using Windows 7 OS.
Thanks