Consulting

Results 1 to 11 of 11

Thread: Solved: URGENT COMBOBOX HELP

  1. #1
    VBAX Regular
    Joined
    Nov 2004
    Posts
    74
    Location

    Solved: URGENT COMBOBOX HELP

    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

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Regular
    Joined
    Nov 2004
    Posts
    74
    Location
    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.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  6. #6
    VBAX Regular
    Joined
    Nov 2004
    Posts
    74
    Location
    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.

  7. #7
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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:[VBA]Selection.Range.Text = ComboBox2.Value[/VBA]
    K :-)

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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:
    [vba]Selection.TypeText Text:=Combobox2.Text[/vba]
    Also, using the properties of objects, you can do this:
    [vba]ActiveDocument.Bookmarks("Repdivision") _
    .Range.InsertAfter ComboBox2.Text[/vba]
    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

  9. #9
    VBAX Regular
    Joined
    Nov 2004
    Posts
    74
    Location
    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.

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  11. #11
    VBAX Regular
    Joined
    Nov 2004
    Posts
    74
    Location
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •