View Full Version : Inserting Multiple Building Blocks at One Bookmark
Jhender2
04-24-2017, 10:59 AM
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!
gmayor
04-25-2017, 10:55 PM
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.
gmaxey
04-26-2017, 05:27 AM
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
Jhender2
04-26-2017, 05:46 AM
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".
gmaxey
04-26-2017, 06:00 AM
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.
Jhender2
04-28-2017, 07:41 AM
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.
gmaxey
04-28-2017, 04:20 PM
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
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.