Consulting

Results 1 to 7 of 7

Thread: Inserting Multiple Building Blocks at One Bookmark

  1. #1
    VBAX Newbie
    Joined
    Oct 2016
    Posts
    5
    Location

    Inserting Multiple Building Blocks at One Bookmark

    Hello everyone,

    I am working with a document that has a User Form with several texboxes on it that simply populates the information typed in to them at bookmark locations set in the document. I have this working for a drop-down box as well without any issues.

    The issue I am having is in reference to a Multi-select list box.
    I have a multi-select listbox with the options being populated using code list this:

    With ListBox1
    .AddItem "Option 1"
    .AddItem "Option 2"
    .AddItem "Option 3"
    I have logical expressions setup to determine which building blocks should be used based of the selections using gmayor's code that I found on here for filling bookmarks with building blocks (fcnFillBMwithBB) which look like this:

    If Listbox1.Value = "Example 1" Then
        fcnFillBMwithBB "bmContent", "Example.dotm", 9, "General", "BB1 Name"
    ElseIf Listbox1.Value = "Example 2" Then
        fcnFillBMwithBB "bmContent", "Example.dotm", 9, "General", "BB2 Name"
    ElseIf Listbox1.Value = "Example 3" Then
        fcnFillBMwithBB "bmContent", "Example.dotm", 9, "General", "BB3 Name"
    End If
    The question I have is, what code would I use to look through all the options selected in the listbox and populate the different building blocks associated with each item at a bookmark on different lines. Any help would be greatly appreciated!

  2. #2
    It is easy enough to establish which items are selected in a list box e.g.

        With ListBox1
            For i = 0 To .ListCount - 1
                If .Selected(i) Then
                    MsgBox .List(i)    'do something with the selected item
                End If
            Next i
        End With
    The problem you present does not indicate which bookmarks to write the selected items to. Is there a bookmark and a building block for every item in the list, selected or not?

    What about the list items not selected. What happens to their bookmarks (if anything)? You will need to explain what exactly you expect to achieve.

    The code you quoted matches the style of my old friend Greg Maxey with whom my name is often confused, no doubt because we share the same initials and frequently cooperate.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    If you want to wrap multiple building blocks in the same bookmark, you could use something like this:

    Sub ScratchMacro()
    Dim oRng As Range, oRng2 As Range
    Dim i As Long
      Set oRng = ActiveDocument.Bookmarks("bmTarget").Range
      'Insert the first BB
      Set oRng = ThisDocument.AttachedTemplate.BuildingBlockTypes(9).Categories("General").BuildingBlocks(1).Insert(oRng, True)
      For i = 2 To 3 'or each additional item you want to insert
      Set oRng2 = oRng.Duplicate
      oRng2.Collapse wdCollapseEnd
      oRng2.InsertBefore vbCr
      oRng2.Collapse wdCollapseEnd
      Set oRng2 = ThisDocument.AttachedTemplate.BuildingBlockTypes(9).Categories("General").BuildingBlocks(i).Insert(oRng2, True)
      oRng.End = oRng2.End
      ActiveDocument.Bookmarks.Add "bmTarget", oRng
      Next i
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Newbie
    Joined
    Oct 2016
    Posts
    5
    Location
    Thank you both for you help and Apologies for the name confusion!

    I think what I am trying to do is a combination of both suggestions from the two of you.

    I have a list of 10 items in a listbox named "lbDocuments", and depending which combination of those 10 items are selected will dictate which building blocks(I have 10 of these as well) are wrapped into a single bookmark which I have named "bmContent".

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Ok, if that is the case then you just need to combine the two to achieve that result. We can give you a fish and you eat for a day ....

    Post your combined code (or attempt) if you need additional help.
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    VBAX Newbie
    Joined
    Oct 2016
    Posts
    5
    Location
    Thanks for the help so far.

    I am struggling with bridging the gap between the code identifying what items are selected in the listbox and inserting the building blocks that are associated with those listbox items.

    Here is what I have so far.

     Dim lngIndex As Long
     Dim strSelected As String
        For lngIndex = 0 To lbDocuments.ListCount - 1
            If lbDocuments.Selected(lngIndex) Then
                strSelected = strSelected & lbDocuments.List(lngIndex) & vbCr
            End If
        Next
    Dim oRng As Range, oRng2 As Range
    Dim i As Long
        Set oRng = ActiveDocument.Bookmarks("bmContent").Range
         'Insert the first BB
        Set oRng = ThisDocument.AttachedTemplate.BuildingBlockTypes(9).Categories("General").BuildingBlocks(1).Insert(oRng, True)
        For i = 2 To 10 'or each additional item you want to insert
            Set oRng2 = oRng.Duplicate
            oRng2.Collapse wdCollapseEnd
            oRng2.InsertBefore vbCr
            oRng2.Collapse wdCollapseEnd
            Set oRng2 = ThisDocument.AttachedTemplate.BuildingBlockTypes(9).Categories("General").BuildingBlocks(i).Insert(oRng2, True)
            oRng.End = oRng2.End
            ActiveDocument.Bookmarks.Add "bmContent", oRng
        Next i
    I have my building blocks named exactly like the listbox items they should be associated with to try to simplify things.

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    I didn't test this but it should work or be close:

    Private Sub CommandButton1_Click()
    Dim lngIndex As Long
    Dim bFirst As Boolean, bSetBM As Boolean
    Dim oRng As Range, oRng2 As Range
      bFirst = True
      bSetBM = False
      For lngIndex = 0 To lbDocuments.ListCount - 1
        If lbDocuments.Selected(lngIndex) Then
          If bFirst Then
            Set oRng = ActiveDocument.Bookmarks("bmContent").Range
            'Insert the first BB
            Set oRng = ThisDocument.AttachedTemplate.BuildingBlockTypes(9).Categories("General").BuildingBlocks(lbDocuments.List(lngIndex)).Insert(oRng, True)
            bFirst = False
            bSetBM = True
          Else
            Set oRng2 = oRng.Duplicate
            oRng2.Collapse wdCollapseEnd
            oRng2.InsertBefore vbCr
            oRng2.Collapse wdCollapseEnd
            Set oRng2 = ThisDocument.AttachedTemplate.BuildingBlockTypes(9).Categories("General").BuildingBlocks(lbDocuments.List(lngIndex)).Insert(oRng2, True)
            oRng.End = oRng2.End
        End If
        If bSetBM Then ActiveDocument.Bookmarks.Add "bmContent", oRng
      Next lngIndex
    End Sub
    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
  •