How to get all the ListBoxes in the current Word document?

N

Novice

Hi all, I'm in the process of creating a word document that contains a 10 column by 200 row table. I've inserted a listbox in one cell and then another in the cell adjacent to it (on the same row). I've written some code that will remember the selections in the boxes. In addition, the second list box will be populated with information specific to the selection the user made in the first list box. This all works and for illustration purposes - here is an example of how it could work

------------------
|cat |
|dog |
|bird |
------------------

When the user selects "cat", the listbox in the adjacent cell is populated with data specific to "cat" - for example

------------------
|cat |lion
|dog |cheetah
|bird |
------------------

The listboxes also support multi-select - and the data is simply added to the listbox in the adjacent cell - for example if the dog item was also selected
------------------
|cat |lion
|dog |cheetah
|bird |wolf
------------------

Anyway, the above all works fine - but I have created several procedures (Sub's) to achieve all of this - though the procedures are small, I don't want to have to repeat them 200 times for each row

But currently I don't know of anyway to iterate over all the listboxes in a document - I was hoping I could do something like
Dim aDoc As Word.Documen
Dim curListBox As ListBo
Set aDoc = ActiveDocumen
For Each curListBox In aDoc.ListBoxe
'Insert code her
Nex

But there is no such method or property

Does anyone know how to get all of the ListBoxes in the current document - so that someone could iterate over them

I'm guessing that I'm going to have to get some other set of objects like "FormItems" or something - but since I have no other "active content" in my word document that is also fine

Thanks
Novice
 
W

Word Heretic

G'day Novice <6tc1ATqlinkDOTqueensuDOTca>,

iterate the .FormFields collection, and only deal with Fields of the
right type...




Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Novice reckoned:
 
P

Peter Hewett

Hi Novice

You're using ActiveX controls, 200+ of them you're a much, much braver man than
I! The reason I say this is that ActiveX controls embedded in a document can be
problematic.

And we start with the first problem. Unlike a UserForm a document does not have
a controls collection. The only practical way with dealing with lots of
controls of the same sort is to create you own collection of them!! There is no
easy way of doing this. You need to create a procedure that adds each control
to a collection object. You needs to declare the collection as Private in the
ThisDocument module so that it persists. Since you're using ListBoxes for two
different purposes you may want to create more than one collection, that's up to
you.

Once you've built your ListBox collection you can uses the controls name as the
collection index.

Code snippet, add to the ThisDocument module:

Private mcolListBoxes As Collection

Private Sub Document_New()
BuildListBoxCollection
End Sub

Private Sub Document_Open()
BuildListBoxCollection
End Sub

Private Sub BuildListBoxCollection()

Set mcolListBoxes = New Collection

With mcolListBoxes
.Add ListBox1, "ListBox1"
.Add ListBox2, "ListBox2"
.Add ListBox3, "ListBox3"
'....
.Add ListBox200, "ListBox200"
End With
End Sub

Once you have the collection built you can use it in you other procedures.

HTH + Cheers - Peter
 
N

Novice

Is there a better way of adding ListBox type constructs to a word document

Word Heretic mentioned that he thought the listboxes would be in the FormField object of the document - is there another way to create listboxes without using ActiveX - i.e. so they occur within the FormField object

Thanks
Novice
 
W

Word Heretic

G'day Novice <6tc1ATqlinkDOTqueensuDOTca>,

You have three choices:

1) ActiveX controls - I know very little about these - what I've seen
of them I don't like much.


2) View > Toolbars > Forms - there's a dropdown you can insert as a
FormField

And

3) A custom style to use an AutoTextList field - details on
www.mvps.org/word/FAQs/index.htm somewhere I think, plus Word's Help
is OK.


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Novice reckoned:
 
P

Peter Hewett

Hi Novice

If you told us where you're code is crashing and what the error is we might be
able to help. Why don't you post all of the code in the ThisDOcument module
then we can see exactly what you've got.

Also check that you've not renamed the ListBox controls.

HTH + Cheers - Peter
 
N

Novice

:
Yeah I suppose that would have helped - but what I didn't make clear was that word was crashing with a memory out of range error - it wasn't even letting me open the document after I added your code, closed the word document and then tried to re-open it - i.e. I couldn't have changed the VBA code in that document after that point - luckily I had saved a version of it before adding your code - which I obviously was too ignorant to understand the code's true purpose

After some additional work I realized what you had intended for the code. You had intended for all those ListBox objects to already exist in the document. I know I said that I already have the 200 ListBoxes, but I started playing with FormFields in a new test document with only a couple of Listboxes. Besides which there were some problems with the creation of my ListBoxes that resulted in some indices being skipped (in the naming of the ListBox objects) - i.e. there was a ListBox1 and a ListBox3 object, but no ListBox2 object. Anyway, the short and sweet of it was that when I put your code in (that added ListBox objects to the Collection object) they didn't actually exist.

Part of my original problem (the other part Peter solved) was that I didn't know how to programmatically make reference to the ActiveX ListBoxes - but I found I could do it by just using the Me class - i.e.
Me.ListBox

I didn't realize that you could access them in that manner - I guess that also means that you could even access them without prefixing the ListBox object name with the Me class, correct? I just use the Me because then the code completion feature comes up

Anyway, I think I'm well on my way thanks mostly to you - but also to Word Heretic - I'll keep trying to get things working. But as a final couple of questions before I lose your attention
1. is the best way to
a) populate the first listbox (in each pair) with its data - i.e the 100 ListBoxes (not the other 100
b) try to retrieve the previous selections - if the document was previously opened and saved with selection
c) construct the collection I'll use for referencing the 200 ListBoxe

to do it in the Document_Open() Sub? That will likely mean it will take a long time for the user to be able to open the document - since the VBA will have to populate all those ListBoxes

2. Is the best way to save the selections by the user in the ListBoxes (all 200 of them) before they close the document to do it in the
Document_Close Su

For example (the code to do it for just one of them - the real code would have to iterate over all of them in my ListBoxCollection private member variable)
Private Sub Document_Close(
Dim selectionsInFirstListBox As Collectio

Dim iCtr As Intege
For iCtr = 0 To RoleListBox.ListCount -
With selectionsInFirstListBo
.add iCtr, Me.ListBox1.Valu
End Wit
Me.CustomDocumentProperties("choicesInListBox1") = selectionsInFirstListBo
Nex
End Su

Thanks to both Word Heretic and Peter for schooling a novice
Novice
 
P

Peter Hewett

Hi Novice

You don't really have any choice. You have to initialise the controls in both
the Document_New and Document_Open event procedures. However, you also need to
reselect previously selected values in the Document_Open event.

Likewise, when you close (maybe save might be better?) you need to store
sufficient context for you to restore the controls when you next open the
document - Ah the joys of ActiveX controls in a Word document!!

I'd retain the "me.control" syntax. It not only gives you
intellisense/autocomplete but it makes the code more readable. It's no less
efficient either as VBA has to generate the same code anyway, it's just that it
conceals a lot of what its doing (thank goodness!!).

HTH + Cheers - Peter
 
N

Novice

Thanks very much and here I thought I was a VBA whiz - there is more to this than I thought

Oh and I've already posted this in its own thread in this group - but do you know of any way to create a variable of type ListForm - or stated more specifically what is the syntax to declare a variable of type ListForm in VBA 6 for Word 2000

I need to make the ListForm (that I'm adding to the document using VBA) a multi-select ListForm

Thanks
Novice
 
P

Peter Hewett

Hi Novice

There's not a "ListForm" object in the Word Object model.

Not sure what you mean, you'll need to post with more info!

Cheers - Peter

Thanks very much and here I thought I was a VBA whiz - there is more to this than I thought.

Oh and I've already posted this in its own thread in this group - but do you know of any way to create a variable of type ListForm - or stated more specifically what is the syntax to declare a variable of type ListForm in VBA 6 for Word 2000?

I need to make the ListForm (that I'm adding to the document using VBA) a multi-select ListForm.

Thanks,
Novice

HTH + Cheers - Peter
 
P

Peter Hewett

Hi Novice

OK I've seen you're other post. You meant an MSForms.ListBox control!

Cheers - Peter

Hi Novice

There's not a "ListForm" object in the Word Object model.

Not sure what you mean, you'll need to post with more info!

Cheers - Peter



HTH + Cheers - Peter

HTH + Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top