• Word 2007 – populate combobox with autotext entries

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Word 2007 – populate combobox with autotext entries

    • This topic has 2 replies, 2 voices, and was last updated 16 years ago.
    Author
    Topic
    #459954

    I have several autotext entries saved in a template add-in. In another template’s userform, I have a combobox that populates from the specified autotext entries in the add-in template (i.e. based on category). This works fine, except I want them to sort alphabetically and to display the first item on the list in the combobox. Currently, using the following code, the combobox displays blank and I have to click the down-arrow to see the entries. Also, they are not sorted, rather, they are in the order in which I created them.

    This is the code I’m using:
    Set MyTemplate = Templates(CorpStartTemp)

    Dim objDistList As BuildingBlock

    For i = 1 To MyTemplate.BuildingBlockTypes(wdTypeAutoText).Categories(“DistLists”).BuildingBlocks.Count
    Set objDistList = MyTemplate.BuildingBlockTypes(wdTypeAutoText).Categories(“DistLists”).BuildingBlocks(i)
    ToDlg.cboDistLists.AddItem objDistList.Name
    Next i

    CorpStartTemp – is my startup template
    ToDlg – is the form name in the other template
    cboDistList – is the name of the combobox control

    Can anyone help with (1) sorting them alphabetically, and (2) defaulting to display the first name in the list?

    Thanks…
    Adri

    Viewing 1 reply thread
    Author
    Replies
    • #1161280

      You can use code like this:

      Code:
      Dim arr() As String
      Dim i As Integer
      Dim n As Integer
      
      ...
      
      n = MyTemplate.BuildingBlockTypes(wdTypeAutoText).Categories("DistLists").BuildingBlocks.Count
      ' Fill array
      ReDim arr(1 To n)
      For i = 1 To n
        Set objDistList = MyTemplate.BuildingBlockTypes(wdTypeAutoText).Categories("DistLists").BuildingBlocks(i)
        arr(i) = objDistList.Name
      Next i
      ' Sort array
      WordBasic.SortArray arr
      With ToDlg.cboDistLists
        ' Populate combo box
        .List = arr
        ' And select first item
        .Value = .List(0)
      End With
    • #1161282

      Thanks Hans, I’ll give it a try.
      Adri

    Viewing 1 reply thread
    Reply To: Word 2007 – populate combobox with autotext entries

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: