Consulting

Results 1 to 13 of 13

Thread: Word 2010 Macro - Populating ComboBox on Open Event

  1. #1

    Word 2010 Macro - Populating ComboBox on Open Event

    Hi,

    I've searched in the entire internet about this and I didn't find any solution for my problem.

    I have a form containing several comboboxes and they have to be populate with 4 items. I want to populate them in the Document_Open Event, so I started with:
    Private Sub Document_Open ()
    And now begins my problem, How can I loop for all combobox in the document to add items? I thought in use a For Each / Next structure, but I can't define what kind of object a combobox is. I read in some forums that in Word 2010 they are located inside de InLineShapes collection, but I couldn't make any code to work.

    Is there anybody that can help me??

    Thanks a lot!!!

    Eddie

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "I've searched in the entire internet about this"

    The ENTIRE Internet? I seriously doubt that. I know I have posted on this on a couple of occasions.

    IF the comboboxes are the only InlineShapes, and IF each gets the SAME values, the following populates all of them with the same values on document open.[vba]Option Explicit

    Private Sub Document_Open()
    Call PopulateCB
    End Sub

    Sub PopulateCB()
    Dim objCombo As InlineShape
    For Each objCombo In ActiveDocument.InlineShapes
    objCombo.OLEFormat.Object.AddItem "blah"
    objCombo.OLEFormat.Object.AddItem "yadda"
    objCombo.OLEFormat.Object.AddItem "whatever"
    objCombo.OLEFormat.Object.AddItem "ho-hum"
    objCombo.OLEFormat.Object.ListIndex = 0
    Next
    End Sub[/vba]Note that it also makes the first item the visible one (ListIndex = 0).

  3. #3
    Worked Perfectly!!!!

    Thank you!!!!

    I removed the "ListIndex = 0", because I want to maintain the last saved selection.

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Oh really. Do tell. How are you going to do that? This is on document open. There will be no saved selection. You can select whatever you want...this will not be what shows on document open.

  5. #5
    Hi,

    In the document open procedure we are going to add items in the listbox.

    the Combobox.Text is kept when you save the file. If you don't populate then when you open the file, you have the last saved selection but you can't select any other option.

    Try it.

  6. #6
    Quote Originally Posted by fumei View Post
    "I've searched in the entire internet about this"

    The ENTIRE Internet? I seriously doubt that. I know I have posted on this on a couple of occasions.

    IF the comboboxes are the only InlineShapes, and IF each gets the SAME values, the following populates all of them with the same values on document open.[vba]Option Explicit

    Private Sub Document_Open()
    Call PopulateCB
    End Sub

    Sub PopulateCB()
    Dim objCombo As InlineShape
    For Each objCombo In ActiveDocument.InlineShapes
    objCombo.OLEFormat.Object.AddItem "blah"
    objCombo.OLEFormat.Object.AddItem "yadda"
    objCombo.OLEFormat.Object.AddItem "whatever"
    objCombo.OLEFormat.Object.AddItem "ho-hum"
    objCombo.OLEFormat.Object.ListIndex = 0
    Next
    End Sub[/vba]Note that it also makes the first item the visible one (ListIndex = 0).

    Hello,
    i know its perhaps an old post but i realy nead help in this macro.Well my problem is that i need to open a word document containing a table.i have a combobox in the first column and another one in the third. the probleme is that they have different items.do you have an idea about this? with my advanced thanks.

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Unfortunately at this point I don't have any idea what you want to do with either combobox. What king of comboboxes are they?
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    Quote Originally Posted by gmaxey View Post
    Unfortunately at this point I don't have any idea what you want to do with either combobox. What king of comboboxes are they?
    Hello, i want comboboxes of the type: ClassType = "Forms.ComboBox.1" and i want that on each row of the column 1 and threee will be a combobox.this is the general code:
    'Private Sub Document_Open()
    'Dim cbx As ComboBox, ishp As InlineShape
    'ThisDocument.Tables(1).Columns(1).Select
    'For Each ishp In Selection.InlineShapes
    'If ishp.Type = wdInlineShapeOLEControlObject Then
    'If ishp.OLEFormat.ClassType = "Forms.ComboBox.1" Then
    'Set cbx = ishp.OLEFormat.Object
    'cbx.AddItem "Saturday"
    'cbx.AddItem "Sunday"
    'cbx.AddItem "monday"
    ....etc
    'End If
    'End If
    'Next
    'ThisDocument.Tables(1).Columns(3).Select
    'For Each ishp In Selection.InlineShapes
    'If ishp.Type = wdInlineShapeOLEControlObject Then
    'If ishp.OLEFormat.ClassType = "Forms.ComboBox.1" Then
    'Set cbx = ishp.OLEFormat.Object
    ''cbx.Clear
    'cbx.AddItem "January"
    'cbx.AddItem "Febuary"
    ...etc
    'End If
    'End If
    'Next
    'End Sub
    Unfortunately i do not get the good result.i hope this will give you an idea about what i want.thank you

  9. #9
    i just want to add that the comboboxes are inserted in the document.i just want the items to be loaded.

  10. #10
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Private Sub Document_Open()
    Dim cbx As ComboBox, ishp As InlineShape
      Application.ScreenUpdating = False
      For Each ishp In ActiveDocument.Tables(1).Range.InlineShapes
       ishp.Select
       If ishp.Type = wdInlineShapeOLEControlObject Then
         If ishp.OLEFormat.ClassType = "Forms.ComboBox.1" Then
           Set cbx = ishp.OLEFormat.Object
           cbx.Clear
           Select Case ishp.Range.Information(wdEndOfRangeColumnNumber)
             Case 1:
               cbx.AddItem "Sunday"
               cbx.AddItem "Monday"
             Case 3:
               cbx.AddItem "January"
               cbx.AddItem "February"
            End Select
          End If
        End If
      Next
      Application.ScreenUpdating = True
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  11. #11
    Quote Originally Posted by gmaxey View Post
    Private Sub Document_Open()
    Dim cbx As ComboBox, ishp As InlineShape
      Application.ScreenUpdating = False
      For Each ishp In ActiveDocument.Tables(1).Range.InlineShapes
       ishp.Select
       If ishp.Type = wdInlineShapeOLEControlObject Then
         If ishp.OLEFormat.ClassType = "Forms.ComboBox.1" Then
           Set cbx = ishp.OLEFormat.Object
           cbx.Clear
           Select Case ishp.Range.Information(wdEndOfRangeColumnNumber)
             Case 1:
               cbx.AddItem "Sunday"
               cbx.AddItem "Monday"
             Case 3:
               cbx.AddItem "January"
               cbx.AddItem "February"
            End Select
          End If
        End If
      Next
      Application.ScreenUpdating = True
    End Sub
    I thank you very much for the answer,it's very interesting!!!
    i tried it in a simple table and it worked perfectly,but when i tried it in my particular table where there are some fusion of the above lines of the table,the comboboxes seems to be blocked until i click on a line then i activate and desactivate the creation mode.
    do you have an idea about how to get around this special case? thank you again for the precedent code you are a genius!!!!

  12. #12
    oh! very nice i tried the code in other computer and all thing is fine.the probleme is perhaps in my computer.Thank you very much Greg!!!!!!!!!!

  13. #13
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    No, but I also experience something that I could quite figure out. If the shape is not selected i.g., ishp.Select then the code would not correctly determine the column index.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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