PDA

View Full Version : [SOLVED:] Updating bookmark with multiple buildings blocks based on checkbox status



gingerstu
04-27-2021, 09:05 AM
Hi,
I'm trying to update a single bookmark with multiple building blocks based on checkboxes on a user form.

This is the code I have so far:


Dim fi As Word.Range
If cbObjectivesFamilyIncome.Value Then
Set fi = ThisDocument.AttachedTemplate.BuildingBlockTypes(wdTypeQuickParts). _
Categories("SL-Objectives").BuildingBlocks("FamilyIncome").Insert(ActiveDocument.Bookmarks("bmObjectives").Range, True)
ActiveDocument.Bookmarks.Add "bmObjectives", fi
End If


Dim pom As Word.Range
If cbObjectivesBuyHouse.Value Then
Set pom = ThisDocument.AttachedTemplate.BuildingBlockTypes(wdTypeQuickParts). _
Categories("SL-Objectives").BuildingBlocks("payOffMortgage").Insert(ActiveDocument.Bookmarks("bmObjectives").Range, True)
ActiveDocument.Bookmarks.Add "bmObjectives", pom
End If


I'm only getting the 2nd building block coming through to the bookmark if both checkboxes are selected. I'm assuming that the first building block gets written into the bookmark and the second overwrites it.

After much searching, I can't find a way to append a building block to a bookmark when there's content already in there.

Can you help?

Thanks,
Stu

Chas Kenyon
04-27-2021, 11:41 AM
You need to collapse the range before inserting the building block.
You may also want to use two bookmarks.
See: Working with bookmarks https://wordmvp.com/FAQs/MacrosVBA/WorkWithBookmarks.htm

gingerstu
04-27-2021, 12:03 PM
You need to collapse the range before inserting the building block.
You may also want to use two bookmarks.
See: Working with bookmarks https://wordmvp.com/FAQs/MacrosVBA/WorkWithBookmarks.htm

Thanks Chas, I’ll look into collapsing the range.
I haven’t used more bookmarks as this code is only 2 of 12 unique objectives. My assumption is that if a bookmark remains unused, on print it would looks like a random carriage return was left over. My assume is that By using one bookmark and ‘injecting’ the building blocks needed based on the checkboxes I would have a tidier print.

Chas Kenyon
04-27-2021, 12:12 PM
There is a macro for inserting a building block at a range on my page on AutoText (http://www.addbalance.com/usersguide/autotextautocorrect.htm#UsingVBABuildingBlock). It is Situation #3. Before getting into the series of IF's looking for the building block, you would want to collapse the range oRange.Collapse possibly with a direction.
This is based on Graham Mayor's macro to insert a building block regardless of which template holds the building block.

Chas Kenyon
04-27-2021, 12:15 PM
Thanks Chas, I’ll look into collapsing the range.
I haven’t used more bookmarks as this code is only 2 of 12 unique objectives. My assumption is that if a bookmark remains unused, on print it would looks like a random carriage return was left over. My assume is that By using one bookmark and ‘injecting’ the building blocks needed based on the checkboxes I would have a tidier print.

If working with empty bookmarks, definitely check out this MVP Page (https://wordmvp.com/FAQs/MacrosVBA/InsertingTextAtBookmark.htm).
It would also be possible to include a paragraph mark in your Building Block. Then use multiple bookmarks. (Bookmarks do not require a separate line.)

gingerstu
04-27-2021, 01:29 PM
There is a macro for inserting a building block at a range on my. It is Situation #3. Before getting into the series of IF's looking for the building block, you would want to collapse the range oRange.Collapse possibly with a direction.
This is based on Graham Mayor's macro to insert a building block regardless of which template holds the building block.

I had a go at collapsing the range, but still had the same outcome. I've probably got the syntax wrong?


Dim updateObjective As Word.Range
Set updateObjective = ActiveDocument.Bookmarks("bmObjectives").Range
updateObjective.Collapse Direction:=wdCollapseEnd

If cbObjectivesFamilyIncome.Value Then
Set updateObjective = ThisDocument.AttachedTemplate.BuildingBlockTypes(wdTypeQuickParts). _
Categories("SL-Objectives").BuildingBlocks("FamilyIncome").Insert(ActiveDocument.Bookmarks("bmObjectives").Range, True)
ActiveDocument.Bookmarks.Add "bmObjectives", updateObjective
End If

If cbObjectivesBuyHouse.Value Then
Set updateObjective = ThisDocument.AttachedTemplate.BuildingBlockTypes(wdTypeQuickParts). _
Categories("SL-Objectives").BuildingBlocks("payOffMortgage").Insert(ActiveDocument.Bookmarks("bmObjectives").Range, True)
ActiveDocument.Bookmarks.Add "bmObjectives", updateObjective
End If

macropod
04-27-2021, 02:14 PM
For example:

Dim BkMkRng As Word.Range, RngTmp As Word.Range, StrBkMk As String, i As Long
i = (cbObjectivesFamilyIncome.Value * 1 + cbObjectivesBuyHouse.Value * 2)
StrBkMk = "bmObjectives"
With ActiveDocument
Set BkMkRng = .Bookmarks(StrBkMk).Range
Select Case i
Case 0
BkMkRng.Text = vbNullString
Case 1
Set BkMkRng = .AttachedTemplate.BuildingBlockTypes(wdTypeQuickParts). _
Categories("SL-Objectives").BuildingBlocks("FamilyIncome").Insert(BkMkRng, True)
.Bookmarks.Add StrBkMk, BkMkRng
Case 2
Set BkMkRng = .AttachedTemplate.BuildingBlockTypes(wdTypeQuickParts). _
Categories("SL-Objectives").BuildingBlocks("FamilyIncome").Insert(BkMkRng, True)
.Bookmarks.Add StrBkMk, BkMkRng
Case 3
Set BkMkRng = .AttachedTemplate.BuildingBlockTypes(wdTypeQuickParts). _
Categories("SL-Objectives").BuildingBlocks("FamilyIncome").Insert(BkMkRng, True)
Set RngTmp = BkMkRng.Duplicate
RngTmp.Collapse wdCollapseEnd
Set RngTmp = .AttachedTemplate.BuildingBlockTypes(wdTypeQuickParts). _
Categories("SL-Objectives").BuildingBlocks("FamilyIncome").Insert(RngTmp, True)
BkMkRng.End = RngTmp.End
.Bookmarks.Add StrBkMk, BkMkRng
End Select
End With

gmayor
04-27-2021, 08:48 PM
You may find the following useful:

Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
'Graham Mayor - https://www.gmayor.com - Last updated - 28 Apr 2021
'strBMName is the name of the bookmark to fill
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
Dim oRng As Range
On Error GoTo lbl_Exit
With ActiveDocument
If .Bookmarks.Exists(strbmName) = True Then
Set oRng = .Bookmarks(strbmName).Range
Set oRng = oTemplate.AutoTextEntries(strAutotext).Insert _
(Where:=oRng, RichText:=True)
.Bookmarks.Add Name:=strbmName, Range:=oRng
End If
End With
lbl_Exit:
Exit Sub
End Sub

gingerstu
05-01-2021, 03:13 PM
After a fair amount of fighting with the code and interpreting various posts online, I've come up with this and it works for my purposes. I'm posting here so that other who are searching for this sort of thing can stumble upon the solution I came up with.

Thanks to those who helped with this, much appreciated.


Dim updateObjective1 As Word.Range, updateObjective2 As Word.Range, updateObjective3 As Word.Range, updateObjective4 As Word.Range
Set updateObjective1 = ActiveDocument.Bookmarks("bmObjectives").Range


If cbPayOffMortgage.Value Then
Set updateObjective1 = ThisDocument.AttachedTemplate.BuildingBlockTypes(wdTypeQuickParts). _
Categories("SL-Objectives").BuildingBlocks("Pay off mortgage").Insert(updateObjective1, True)
End If


If cbStandardOfLiving.Value Then
Set updateObjective2 = updateObjective1.Duplicate
updateObjective2.Collapse wdCollapseEnd
'updateObjective2.InsertBefore vbCr
updateObjective2.Collapse wdCollapseEnd
Set updateObjective2 = ThisDocument.AttachedTemplate.BuildingBlockTypes(wdTypeQuickParts). _
Categories("SL-Objectives").BuildingBlocks("Standard of Living").Insert(ActiveDocument.Bookmarks("bmObjectives").Range, True)
updateObjective1.End = updateObjective2.End
End If

If cbInheritanceLumpSum.Value Then
Set updateObjective3 = updateObjective1.Duplicate
updateObjective3.Collapse wdCollapseEnd
'updateObjective2.InsertBefore vbCr
updateObjective3.Collapse wdCollapseEnd
Set updateObjective3 = ThisDocument.AttachedTemplate.BuildingBlockTypes(wdTypeQuickParts). _
Categories("SL-Objectives").BuildingBlocks("Inheritance or Lump Sum").Insert(ActiveDocument.Bookmarks("bmObjectives").Range, True)
updateObjective1.End = updateObjective2.End
End If

If cbReviewExistingPolicies.Value Then
Set updateObjective4 = updateObjective1.Duplicate
updateObjective4.Collapse wdCollapseEnd
'updateObjective2.InsertBefore vbCr
updateObjective4.Collapse wdCollapseEnd
Set updateObjective4 = ThisDocument.AttachedTemplate.BuildingBlockTypes(wdTypeQuickParts). _
Categories("SL-Objectives").BuildingBlocks("Review existing policies").Insert(ActiveDocument.Bookmarks("bmObjectives").Range, True)
updateObjective1.End = updateObjective4.End
End If
ActiveDocument.Bookmarks.Add "bmObjectives", updateObjective1