PDA

View Full Version : Solved: Create checkboxes on UserForm based on array values



crowfan
10-04-2007, 11:22 AM
Hi all,

I'm just starting to work with UserForms and I have a question.

Here's the scenario: I have Word document (a manual), broken up into chapters. Each chapter is its own section. The Chapter heading is always contained in the first paragraph of the section.

I also have the need to create another document based on the first (sort of a quick reference guide), which takes the data out of the first document, and strips out the graphics and converts a bunch of styles.

So I thought it might be useful to create a custom UserForm. What I'd like to do, ideally, is to have the UserForm list all of the chapters from the main document as checkboxes. Then I could select the desired chapters, and the macro would then go through the manual and copy the selected chapters into the new document.

So I started with an array, to read the chapter titles into an array of strings. That's working well.

Dim strChapterArray() As String, i As Integer, intCount As Integer

intCount = 1

For i = 3 To ActiveDocument.Sections.Count
ReDim Preserve strChapterArray(intCount)
strChapterArray(intCount) = ActiveDocument.Sections(i).Range.Paragraphs(1).Range.Text
intCount = intCount + 1
Next i

Now I'd like to use the values in the array to create checkboxes on the UserForm. How would I go about doing that? I was thinking along these lines:

for each value in the array
create a checkbox on the user form, name it with the same name as the arry
next

Is this even possible? I am pretty good with Word VBA in general but very inexperienced with User Forms.

Thanks!

fumei
10-05-2007, 04:06 AM
You want to dynamically create controls on your userform? Do you fully understand what that involves? You will not only have to create the controls, but you will have to dynamically figure out where to place them!

I assume you want to do this because you have various documents, with various numbers of sections/chapters...yes? Why not a listbox that the user can select multiple items? That would be much easier.

fumei
10-05-2007, 04:17 AM
Dim j As Long
For j = 0 To Ubound(strChapterArray)
Listbox1.AddItem strChaperArray(j)
Next
Listbox1.ListIndex = 0would have a listbox of the chapter titles. Set with multiselect, you can get the items they select from the listbox and proceed.

It IS possible to dynamically create and place controls....but unless you really have to, I would not recommend going that route.

If you really, really, really, want to have checkboxes, it would still be easier to:

1. make a userform with MORE checkboxes than needed;
2. make them invisible
3. determine how many you actually need from the array count
4. make just those many visible
4b. resize the userform if needed....
5. use the array values to name the visible checkboxes.

crowfan
10-05-2007, 05:39 AM
You want to dynamically create controls on your userform? Do you fully understand what that involves?Not even close...hence the question. :) I didn't know if it was easy and I just didn't know how, or if it was hard to do. My research before I asked the qusetion here did not really get me anywhere.

Really the only reason I was thinking checkboxes was that I thought they'd be easier for the user. If a listbox is that much easier to do with VBA (which it obviously is), I'll just go that route.

Thanks!

Norie
10-05-2007, 06:11 AM
crowfan

As fumei is suggesting the way to go would be a listbox.

And you can still have your checkboxes using one.

Just set MultiSelect to fmMultiSelectMulti and ListStyle to fmListStyleOption.

crowfan
10-05-2007, 07:57 AM
crowfan

As fumei is suggesting the way to go would be a listbox.

And you can still have your checkboxes using one.

Just set MultiSelect to fmMultiSelectMulti and ListStyle to fmListStyleOption.Wow, that's excellent. Thanks!

fumei
10-08-2007, 01:27 AM
Thank you Norie.