MS Office Forum / Word / Programming / November 2006
List box not working on my userform
|
|
Thread rating:  |
Sol Apache - 12 Nov 2006 13:12 GMT I am working on a userform which has a list box of about 50 locations. I intended that users would be able to select one location and my macro would then place other items of information in the document, depending upon the list item selected.
I have created many userforms before but I haven¹t used a list box, and I cannot get this to work. I have put the list information in a combo box and that works, so it is the list box which is causing the problem. It is much better to have a list than a combo box for this field data.
The properties I have set include:
Enabled: True ListStyle = 1 - FmListStyleOption Locked = False MatchEntry = 1 - fmMatchEntryComplete MultiSelect = 0 - fmMultiSelectSingle
I think these are all the relevant properties.
According to VB Help I should have a radio button showing on the left, but none are displaying when I test out the form, just my list. I click on the location I want and it highlights as selected, but the value does not get placed in the document.
I am using a Mac. Are there any issues with using listboxes on userforms on Macs?
If not, can someone help me with getting this listbox to work.
Thanks for any help
Sol
Jay Freedman - 12 Nov 2006 15:46 GMT Hi Sol,
Whether the list shows option buttons may be a Mac vs. PC issue, although I don't have a Mac to test with. The properties you listed are certainly sufficient to display them here (actually, MatchEntry doesn't have anything to do with it). In any case, I don't believe that the option buttons add anything useful -- the filled button is always on the same row as the highlighting.
I don't know what to make of your statement that "the value does not get placed in the document." Placing a selected list value into the document *always* requires explicit code in the userform, usually in the Click procedure of the OK button. It might look something like this:
Set rg = ActiveDocument.Bookmarks("bk1").Range rg.Text = ListBox1.Value ActiveDocument.Bookmarks.Add Name:="bk1", Range:=rg
Do you have code to do this, or something similar? If you put a breakpoint in the code, can you verify that the listbox's .Value property is the text of the selected row? If so, then the option button (or its absence) has nothing to do with the problem.
-- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>I am working on a userform which has a list box of about 50 locations. I >intended that users would be able to select one location and my macro would [quoted text clipped - 29 lines] > >Sol Sol Apache - 12 Nov 2006 16:03 GMT Thanks for this. I have been looking into this more, even getting a PC to try my macro out, and it isn¹t the properties, but the code because it would not work on a PC either.
My intention for this macro is a bit different from just placing the value at a bookmark. How I wanted it to work was to have the user select an item, then my code says:
List$ = ListAddresses.value
If List$ = ³a line of text in list selected² then ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Bookmarks( 2).Select Selection.TypeText Text:= "Company Name²
...Then the macro goes to several other bookmarks and places text which is hardwired into the macro (there are probably better ways of doing this too, but this works for me for the time being, using data from FileMaker fields to quickly create the code details)
So this all works if I am getting the value from a combo but not from a listbox.
Why is this?
Thanks for any (further) help.
On 12/11/06 15:46, in article 6pfel2119tadpthhpshmfsr5b77gn4eqao@4ax.com,
> Hi Sol, > [quoted text clipped - 60 lines] >> >> Sol Jay Freedman - 12 Nov 2006 17:28 GMT It looks like that *should* work. To find out what's really happening:
- Open the macro code in the VBA editor. - Click on the line "List$ = ListAddresses.value" and press F9 (or the menu item Debug > Toggle Breakpoint; or just click in the gray bar to the left of the code line) so a reddish dot and highlight appear on the line. - Run the macro. When it reaches that line, it will pause and open the VBA editor. - Press F8 to execute that line. - Hover the mouse pointer over the variable List$ to see its value (or click View > Locals Window to see all the variables). Does it match the quoted value in the If statement"? Note that it must match *exactly*, including case, presence/absence of spaces, straight vs. curly quotes, etc. - Continue to press F8 to step through the rest of the macro. Does it execute the remaining statements that you listed?
By the way, I'd advise strongly against using the Select and Selection.TypeText statements, especially when the location is in a header or footer. Instead, directly replace the text of the bookmark's range:
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range _ .Bookmarks(2).Text = "Company Name"
Word has a nasty habit of getting "lost" when you programmatically manipulate the Selection in headers/footers.
-- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>Thanks for this. I have been looking into this more, even getting a PC to >try my macro out, and it isn¹t the properties, but the code because it would [quoted text clipped - 89 lines] >>> >>> Sol Sol Apache - 12 Nov 2006 19:23 GMT Hi Jay
The text in the list is exactly the same as that for the combo box, except the field name is different:
CmbList.AddItem ³company name² ListBox2.AddItem ³company name²
Also I tried changing my code to your suggestion, but when I try to compile it, I get a message box saying:
Method or data member not found
and ³text =² in the code is highlighted. (My code is: ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Bookmarks( 1).Text = ³Company Name²) - the same as yours. I have looked in VB help and it seems that ³text =² is valid.
Looks like I¹ll just stick with the combo box since that works.
Thanks for your help
On 12/11/06 17:28, in article rglel25ff4kqof46smpor050krec2vmspg@4ax.com,
> It looks like that *should* work. To find out what's really happening: > [quoted text clipped - 125 lines] >>>> >>>> Sol Jay Freedman - 12 Nov 2006 21:42 GMT Sorry 'bout that. I missed a bit; that should be
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range _ .Bookmarks(2).Range.Text = "Company Name"
In any case, that's a completely separate issue from whether the list box would behave any differently than a combo box. They should either both work, or both not work, if all other circumstances are the same. Evidently *something* else is different.
-- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>Hi Jay > [quoted text clipped - 149 lines] >>>>> >>>>> Sol Sol Apache - 13 Nov 2006 01:38 GMT Thanks for your help. Have put your new code in.
I have another problem now - when running the compile or run userform I get a message that the ³procedure is too large². Does VB have limits on these things. If so, how can I break my code up so that VB can cope with it?
This is after I put in 28 of the 53 variations on the placing in the header/footer.
Thanks
On 12/11/06 21:42, in article 1t4fl2ph3v68597n7osg9h96b8v4234imm@4ax.com,
> Sorry 'bout that. I missed a bit; that should be > [quoted text clipped - 85 lines] >>>> >>>> If List$ = ³a line of text in list selected² then ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Bookmarks>>>> (
>>>> 2).Select >>>> Selection.TypeText Text:= "Company Name² [quoted text clipped - 83 lines] >>>>>> >>>>>> Sol Jay Freedman - 13 Nov 2006 03:51 GMT According to http://support.microsoft.com/?kbid=211489, macro size is "Limited only by available memory". I believe on a Mac you can specify how much memory to allocate -- on a PC it's generally as much as it can grab. I don't know of any other specific limit.
There are a couple of things you can do to reduce the size of the code. The most dramatic improvements are these:
- Don't repeat the same code over and over, with slight variations or with different strings. Instead, use a subroutine: http://www.word.mvps.org/FAQs/MacrosVBA/ProcArguments.htm.
- Don't store strings directly in the code. You can store them as AutoText entries with shorter names (http://www.word.mvps.org/FAQs/Customization/AutoText.htm) and call the ActiveDocument.AttachedTemplate.AutoTextEntries(name).Insert method; or you can store the strings in a table in a separate document and read them from there as needed.
-- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>Thanks for your help. Have put your new code in. > [quoted text clipped - 188 lines] >>>>>>> >>>>>>> Sol Sol Apache - 13 Nov 2006 05:13 GMT I have 2gb of RAM and on OSX memory works as UNIX does, which I think is the same as Windows, so I don¹t think most PCs (on which this template will be working) will be able to cope with this macro either.
Why didn¹t I think of an autotext before? I am dopey...
I think I even know how to put an autotext table into a header. The footer can still be in the main macro.
Many thanks for your help.
On 13/11/06 03:51, in article liqfl21e25pngpkjqoscfv4a3q6gl1cd4n@4ax.com,
> According to http://support.microsoft.com/?kbid=211489, macro size is > "Limited only by available memory". I believe on a Mac you can specify [quoted text clipped - 66 lines] >>>> >>>> and ³text =² in the code is highlighted. (My code is: ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Bookmarks>>>> (
>>>> 1).Text = ³Company Name²) - the same as yours. I have looked in VB help and >>>> it seems that ³text =² is valid. [quoted text clipped - 154 lines] >>>>>>>> >>>>>>>> Sol
|
|
|