PDA

View Full Version : Solved: URGENT COMBOBOX HELP



newk
11-08-2005, 03:30 PM
Hi guys I need some urgent help with a combobox on the attached template.

Basically the choices are linked to the userform but the userform isn't writing to document.

I've done comboboxes on previous templates successfully and I have retraced my steps on this one but can't find what I've done wrong.

During sanitation of personal material for VBAX I now have another problem but please don?t worry about that as I haven't looked at it and can probably figure it out.

Any help would be greatly appreciated.

Thank you

fumei
11-08-2005, 03:34 PM
Could you describe your problem? It is good to submit a file/document, but I am not going to look at it until you state what the issue is. I have no interest in exploring through a file, trying to find code that may not be working. Tell us what is not working, tell us what you want to happen.

During sanitation of personal material for VBAX I now have another problem but please don?t worry about that as I haven't looked at it and can probably figure it out.
That is a bizarre sentence. Not sure how to take that.

mdmackillop
11-08-2005, 03:38 PM
Hi Newk,
I'm running Word 2003 and get an error message saying I'm writing to a protected area of the document. I don't see any unprotect routine in your code.
Regards
MD

newk
11-09-2005, 03:44 AM
Thanks Fumei & Mdmackillop for responding so quickly and I apologise for not being clear in my post.
I will deal with Mdmackillop's response first.
On the original there is a 'form document protection'. I think I have disabled this on the attachment, obviously I didn't want anyone to have to mess around with that. I have just tried removing all references to passwords on the original and it still crashes at:

Selection.GoTo What:=wdGoToBookmark, Name:="Repdivision"
Selection.Range.Text = NameNo1(ComboBox2, 1) [Yellow]

and i get the error mesg:
Selection.Range.Text = ""

and the definition says
Property Selection As Selection
read-only
Member of Word.Global

Can I provide anymore useful information to you?

Fumei - Basically when you open the document a userform appears which the user completes and then the information is placed into the relevant bookmarks on the document. It all works fine with the exception of the combobox.

The way I see it is there are two sections to getting a combobox working.
Part1: The script has to be assigned to the combobox so that the choices appear in the combobox when the userform loads.
Part2 the choice is then written to the appropriate bookmark in the document.
At the moment the choices are showing in the userform but when it comes to write them to the document it crashes at the lines:

Selection.GoTo What:=wdGoToBookmark, Name:="Repdivision"
Selection.Range.Text = NameNo1(ComboBox2, 1) [Yellow]

I need the user's choice selected from the combobox to write to the document.

Thanks again for your help on this it really is needed.

PS. Whilst I was removing sensitive information from the template so I could post it on VBAX i created another error. Please don't concern yourself with this as I can probably work it out.

fumei
11-10-2005, 09:15 AM
Could you explain "Selection.Range.Text = NameNo1(ComboBox2, 1) [Yellow]"? This is meaningless to me. Text has to be a string. This does not look like a string. "Combobox2" by itself is also meaningless. You need a property.

newk
11-10-2005, 09:34 AM
Thanks for a swift response fumei,

You really need to look at the code. I have declared the variables as strings. The choices are text and therefore strings as you have quite rightly mentioned. Combobox2 is a combobox- what property do you want? It is a combobox on the userform which is named 'combobox2'

I'm very sorry and am very grateful for your input but please can you be more specific on what information you would like me to give you or alternatively please open up my code and have a look.

Killian
11-10-2005, 09:47 AM
I think the error at that point is coming from the fact that you refer to the array's item with:
NameNo1(ComboBox2, 1)
which when you have something selected, gives:
NameNo1("Choice 3", 1)
"Choice 3" isn't an valid array index, it's a string.

Maybe you wanted the position of the item in the list to refer to the array with? That would be:
NameNo1(ComboBox2.ListIndex + 1, 1)
(N.B. the ListIndex count starts at 0)
But I'm not sure why you need to re-initialize that array on the submit code at all... the info is in the combobox, and its the combobox's selected value you want, isn't it?
so this would do it:Selection.Range.Text = ComboBox2.Value

fumei
11-11-2005, 12:53 AM
Combobox2 is a combobox- what property do you want?
Killian describes it.

Combobox2 is a pointer to the object. It gives absolutely NO information about that object. Nothing. Information about an object is contained in its properties....like:

Combobox2.Value
Combobox2.Text
Combobox2.ListIndex
Combobox2.Height

I also agree with K's question regarding the need for the array. If you want to use the current selected item in the combobox, then you can use it - the "it" being a property. However, of course that property must be a string if you are going to use it at the Selection.

BTW: you could also use:
Selection.TypeText Text:=Combobox2.Text
Also, using the properties of objects, you can do this:
ActiveDocument.Bookmarks("Repdivision") _
.Range.InsertAfter ComboBox2.Text
which uses one instruction instead of two. The bookmark object has a property (Range), which has a method (InsertAfter) that takes a parameter (Combobox2.Text).

I must point out that it is a very good idea to get into the habit of NOT using the default name for control objects like Combobox2. It makes it much, MUCH, easier to debug and really see the flow of code. If it is just one or two...sure, but if the form (or document as this applies to formfields especially) has more than a couple of controls...well....

cboDivisionManagers.Text

can be much easier to understand than

Combobox2.Text

"cbo" is the traditional prefix for comboboxes. Using Intellisense makes it fairly easy to get the longer names. You could get "cboDivisionManagers.Text" by typing:

cbodiv-Ctrl-Spacerbar-.te-Enter

newk
11-11-2005, 02:55 AM
Killian, Thank you. That works perfectly, I am very grateful.

Fumei, Thanks for your help on this and thank you for the ideas on naming conventions. I did purposefully rename the controls as I thought it would be easier for people to understand my code, but I will bare it in mind for the future.

fumei
11-11-2005, 02:01 PM
Essentially K gave the same thing I did. Which was to point out that NameNo1(Combobox2, 1) does not do anything - because Combobox2 by itself does not mean anything.

Glad you got it working.

newk
11-14-2005, 03:23 AM
Please don't think I was not grateful Fumei. I just didn't understand your response. No sorry I didn't know how to deal with your response. I think you overestimated my VBA skills :)

Thanks to you all for helping me out, it truely saved me from a brain bubble!