Consulting

Results 1 to 9 of 9

Thread: Updating bookmark with multiple buildings blocks based on checkbox status

  1. #1

    Updating bookmark with multiple buildings blocks based on checkbox status

    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

  2. #2
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    119
    Location
    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/W...hBookmarks.htm

  3. #3
    Quote Originally Posted by Chas Kenyon View Post
    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/W...hBookmarks.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.

  4. #4
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    119
    Location
    There is a macro for inserting a building block at a range on my page on AutoText. 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.

  5. #5
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    119
    Location
    Quote Originally Posted by gingerstu View Post
    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.
    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.)

  6. #6
    Quote Originally Posted by Chas Kenyon View Post
    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

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    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

Tags for this Thread

Posting Permissions

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