Results 1 to 9 of 9

Thread: Populate paragraphs in document based on Userform Checkboxes

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Quote Originally Posted by gmaxey View Post
    There are a lot of options here. First, if you are using a version of Word that supports content controls then there is really no need to be populating bookmarks. You can populate content controls instead.

    As for the rich text, you can either define the rich text content as building blocks and insert it in a target rich text CC, or simply have it a boiler plate content in the template and delete it if not needed:

    here is some sample code:

    Sub Macro1()
    Dim bCheck As Boolean 'simulates your checkbox
    Dim oTmp As Template
    Dim oCC As ContentControl
    Dim oRng As Range
      bCheck = False
      Set oCC = ActiveDocument.SelectContentControlsByTitle("RTTarget").Item(1)
      If bCheck Then
        Set oTmp = Templates("D:\My Documents\Word\Templates\Normal.dotm") 'Or whatever template defines your buildingblock. I used my normal.dotm and a BB titled RT1
        'Use a target RichText contentcontrol titled e.g., RTTarget
        oTmp.BuildingBlockEntries("RT1").Insert Where:=oCC.Range, RichText:=True
      Else
        Set oRng = oCC.Range
        oCC.Delete True
        oRng.Paragraphs(1).Range.Delete
      End If
    
    End Sub
    Sub Macro2()
    Dim bCheck As Boolean 'simulates your checkbox
    Dim oTmp As Template
    Dim oCC As ContentControl
    Dim oRng As Range
      bCheck = False
      If Not bCheck Then
        Set oCC = ActiveDocument.SelectContentControlsByTitle("RTTarget").Item(1)
        Set oRng = oCC.Range
        oCC.Delete True
        oRng.Paragraphs(1).Range.Delete
      End If
    End Sub

    Hi Greg,

    Thanks for looking at this. I am still having trouble with the code though (as I said I am completely new to this so I think there may be some bits I'm not understanding)

    This is my version of your sub macro 1 code (I think I want to use the rich text content control way rather than the delete method):

    Dim chbTIFI As Boolean 'chbTIFI is the name of my checkbox
    Dim oTmp As Template
    Dim oCC As ContentControl
    Dim oRng As Range
    chbTIFI = False
    Set oCC = ActiveDocument.SelectContentControlsByTitle("ccTIFI").Item(1)
    If chbTIFI Then
    Set oTmp = Templates("C:\Users\sarahjane.munt\Documents\Custom Office Templates\Template Letter.dotm") 'I'm trying to build the content controls & building blocks in the template in which I am creating this userform - is that right or does it need to be in a separate template?
    'Use a target RichText contentcontrol titled e.g., RTTarget
    oTmp.BuildingBlockEntries("bbTIFI").Insert Where:=oCC.Range, RichText:=True 'bbTIFI is what I named my buildingblock when i added it to quickparts
    Else
    Set oRng = oCC.Range
    oCC.Delete True
    oRng.Paragraphs(1).Range.Delete
    End If

    So I created a RichText Content Control in the template and called it ccTIFI - I didn't put any content in it.

    I then created a building block called bbTIFI which I added to quickparts.

    Should the code above be telling the content control to populate with the building block?

    It doesn't seem to do that.

    Additionally, with the 'Else' section when I run the code it just deletes the content control altogether even though I have the checkbox ticked. I tried just removing that part of the code and the content control remains in the document but it is not populated. As the content control is saved into the template, I also can't tell if it is there because of the checkbox or just because it is there.

    Also, I notice that you have used Item(1) and Paragraph(1) but I'm not sure how I need to define these for my code. My assumption is:
    Item(1) - refers to the first contentcontrol with the specified title
    Paragraph(1) - refers to the first paragraph within the contentcontrol - or is it the first paragraph in the document?

    Sorry I realise I am a complete newb but I don't have an IT team to do this for me (plus I love to learn how to do this stuff!)

    TIA

    UPDATE: I changed the code to read chbTIFI = True and now the building block populates in the content control but the Else delete code doesn't seem to be working.
    Last edited by Sarahjane; 04-02-2019 at 12:56 AM.

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
  •