Results 1 to 8 of 8
  1. #1
    Lounger
    Join Date
    Sep 2007
    Posts
    45
    Thanks
    6
    Thanked 1 Time in 1 Post

    Word 2010: Can you populate a listbox from building blocks?

    I'm wondering if I can populate a Listbox from certain types of building blocks? I would like to give the user a chance to select a bunch of building block text specific to a template and then build a document from their selection.

    Would it be possible to give me some starter VBA code to do this? I know how to set the userbox to allow for multiple options, but I would love some VBA code to help me get started?


    Thank you so much.

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,514
    Thanks
    3
    Thanked 143 Times in 136 Posts
    Yes you can do this. The following snippet should get you started although you will need to declare a bunch of variables used in it.
    Code:
    With Me.cbCover
        'Empty the list box first
        .Text = ""
        Do While .ListCount > 0
          .RemoveItem 0
        Loop
      
        'Fill the document type list
        Set aTpl = ActiveDocument.AttachedTemplate
        Set objBBT = aTpl.BuildingBlockTypes(wdTypeCustomCoverPage)
        If objBBT.Categories.Count > 0 Then
          For intCountCat = 1 To objBBT.Categories.Count
            Set objCat = objBBT.Categories(intCountCat)
            If objCat.Name = sDocType Then
              For i = 1 To objBBT.Categories(intCountCat).BuildingBlocks.Count
                Set aBB = objBBT.Categories(intCountCat).BuildingBlocks(i)
          '      Debug.Print aBB.Name
                .AddItem aBB.Name
              Next
            End If
          Next
        End If
        'Now set the first entry as the default selection
        If .ListCount > 0 Then
          .ListIndex = 0
        End If
      End With
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    maverik (2013-01-17)

  4. #3
    Lounger
    Join Date
    Sep 2007
    Posts
    45
    Thanks
    6
    Thanked 1 Time in 1 Post
    I wish people would make up their minds!!

    Now they want the list to be sectioned and use an expansion list to create their document.

    So for example:

    +Spousal Clauses
    Spousal 1
    Spousal 2
    +Trust Clauses
    Trust 1

    I have never seen this done in a user box, never mind attempted to do it myself!!

    I'm assuming I will have to manually type all the additem entries in the proper order, but is this even possible, and if so, would someone mind posting some code to get me started, please?

    Thanks again.

  5. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,192
    Thanks
    201
    Thanked 781 Times in 715 Posts
    Maverick,

    Attached is a sample document that will open a dialog and allow you to select a class then on the making a selection will populate the Items box with the appropriate entries and make it visible. Note if your document parts are named consistently like the arrays I used for the sample you could populate the Items box by adapting Andrews code above then you would only have to create the values for the Categories array. HTH
    Attached Files Attached Files
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  6. #5
    Lounger
    Join Date
    Sep 2007
    Posts
    45
    Thanks
    6
    Thanked 1 Time in 1 Post
    Thank you for that. Is that the closest thing that I can get in VBA to a "tree branch" format?

  7. #6
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,514
    Thanks
    3
    Thanked 143 Times in 136 Posts
    Now it sounds like you are looking for a treeview control. Have a search on google for this but you might be able to get started here
    http://www.excelguru.ca/content.php?...eeview-Control
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  8. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    maverik (2013-01-17)

  9. #7
    Lounger
    Join Date
    Sep 2007
    Posts
    45
    Thanks
    6
    Thanked 1 Time in 1 Post
    I had no idea that was even possible! Thank you for that!! Hopefully this will make them happy.

  10. #8
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,192
    Thanks
    201
    Thanked 781 Times in 715 Posts
    Maverik,

    I tried using the link Andrew provided and got it to work using the data in my previous post see attached file.
    Please note that I had a problem getting the TreeView control to work I had to do two things to get it to work.
    First I had to register the .ocx control as follows:

    Using a Elevated Command prompt enter the command:

    regsvr32 C:\windows\syswow64\mscomctl.ocx

    Next I needed to add a reference in my VBA project to: Microsoft Windows Common Controls 6.0 (SP6)

    To allow multiple selection per your OP you need to set the Checkboxes property to True.

    HTH
    Attached Files Attached Files
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


Posting Permissions

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