PDA

View Full Version : Solved: Count / Cycle / Auto Populate List Boxes



nzeser
05-04-2012, 06:39 AM
I'm new to using VBA in the MS Word environment. What I have are some MS Word documents that are protected (formatting and editing are restricted). I can disable that feature, but don't know if that will have to be considered when running a macro or not. Within the documents are a sets of instructions for users to follow and most of the user feedback involves a list box with three values ("", "PASS", or "FAIL") defaulted to the blank option. Since the length of each document can vary and the number of list boxes can vary as well, I'd like to approach my task with a generic macro that can apply to all documents. At the bottom of the document I'd like to insert a button that does the following:
1) Count the total number of list boxes within the document
2) Cycle through those list boxes and check their value
3) If the value = "" then change it to "PASS", else if the value = "PASS" or "FAIL" then skip.

The documents can be 100 to 200 pages long and it can take too long to fill in while trying pay attention to events going on around you...this would provide the user a quick way to only document what they determine to be "FAIL" and at the end populate the unpopulated list boxes instead of having to go back through and manually update them.

My thoughts are to use a for loop to count the number of list boxes and populate an integer with that value (int_ListBoxCount), then another for loop to cycle through 1 to int_ListBoxCount with a If statement comparing and looking for the three values. First, I don't know how to count list boxes and second I don't know how to jump from list box to list box updating their values. Additionally, will the macro have to disable to document protection in order to run properly? Any advice/direction would be greatly appreciated, thanks!

fumei
05-04-2012, 10:04 AM
Assuming these are formfields dropdowns...

1. you probably do not need to change protection

2. you do not need to count, you can use a For Each...statement (to action each formfield), and an IF statement to test if the result is " ".Sub UpdateFF()
Dim oFF As FormField
For Each oFF In ActiveDocument.FormFields
If oFF.Type = wdFieldFormDropDown Then
If oFF.Result = " " Then
oFF.Result = "Pass"
End If
End If
Next
End SubIf these are not formfields, then it is a different process.

nzeser
05-04-2012, 01:52 PM
Fumei,

Thank you very much.

I was able to try out the code you provided and it does exactly what I want it to do. Additionally, thank you for the explanation, it makes perfect sense and really helped me to understand what was going on and why!

fumei
05-04-2012, 02:47 PM
You are welcome. Please mark the thread as solved.