Consulting

Results 1 to 9 of 9

Thread: Populate paragraphs in document based on Userform Checkboxes

  1. #1

    Populate paragraphs in document based on Userform Checkboxes

    I am completely new to VBA (and in fact coding altogether) but I am trying and I'm kinda getting some of it.

    I am trying to create a template word document which is essentially a letter. The letter needs to be tailored to include or exclude various paragraphs depending on the scenario. I would like to be able to generate this tailoring with a UserForm. So far I have been able to do some of the simple stuff such as getting the UserForm to show on startup of the document and populating bookmarked parts of the document with content selected from a drop down box (CombBox) in the UserForm. How ever I am stuck with getting some RichText (I think this is what I need) to populate into the document by using the UserForm.

    Essentially, I want to have a checkbox on the UserForm, which, when the box is checked and the Command Button (labelled Apply) is clicked, will trigger a paragraph to populate in the document. I am not sure of the code to use; I have seen examples of various types of code but often followed with the comment 'you would be better off using a UserForm' which would indicate to me that, since I am already trying to use a UserForm, this is not the code that I need. Part of the problem is that I am not sure if the pre-set paragraphs that I want to use should be saved as Quick Parts, as ContentControlRichText objects in the template itself or as something else. And then I am not sure how to insert these into the code to tell it to insert.

    The only part that I think I know to do is that the code needs to go within the section: Private Sub cmdApply_Click() so as to make it happen when I click the CommandButton.

    Once I know how to do it for one it should be fairly easy to repeat for multiple sections, I just need some sample code if someone can help with that.

    TIA

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

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

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Sara
    My original Dim bCheck provided as an example to simulate your checkbox. bCheck = False (the box is not checked), bCheck = True (it is checked).

    You haven't shown in what context (or procedure) you are running your code. If it is in some execute event in your userform then you would adapt is as so:


    Dim oTmp As Template
    Dim oCC As ContentControl
    Dim oRng As Range
         Set oCC = ActiveDocument.SelectContentControlsByTitle("ccTIFI").Item(1)
         If chbTIFI Then  'The box is checked and True
             Set oTmp = Templates("C:\Users\sarahjane.munt\Documents\Custom  Office Templates\Template Letter.dotm") 
         oTmp.BuildingBlockEntries("bbTIFI").Insert Where:=oCC.Range,  RichText:=True
        Else 'The box is unchecked and false
             Set oRng = oCC.Range
             oCC.Delete True
             oRng.Paragraphs(1).Range.Delete
      End If
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Hi Greg,

    That works now thank you. Yes the code is running within the execute command of clicking the command button (Apply).

    One final question... If I have several checkboxes and I want the same thing to happen but as follows:

    Checkbox 1 & 2 ticked - then populate with building block 1
    Checkbox 1 & 3 ticked - then populate with building block 2
    Checkbox 2 & 3 ticked - then populate with building block 3

    Is there a way to modify the code to achieve this? I'm assuming there would be an IF xxx AND xxx type amendment that can be applied to the code?

    Thanks,
    SJ

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Private Sub CommandButton1_Click()
    Dim oTmp As Template
    Dim oCC As ContentControl
    Dim oRng As Range
      'Set oCC = ActiveDocument.SelectContentControlsByTitle("ccTIFI").Item(1)
      Select Case True
        Case CheckBox1 And CheckBox2
          
        Case CheckBox2 And CheckBox3
          
        Case CheckBox1 And CheckBox3
          
      End Select
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    Hi Greg, I was able to get that code to work sort of.

    Essentially the code is working but it doesn't like my building blocks and IDK why!

    Here is my code:

    Dim oCCA As ContentControl
    Set oCCA = ActiveDocument.SelectContentControlsByTitle("ccINTROACTION").Item(1)
    Select Case True
    Case chbTR And Not chbAccounts
    Set oTmp = Templates("C:\Users\sarahjane.munt\AppData\Roaming\Microsoft\Templates\Norm al.dotm")
    oTmp.BuildingBlockEntries("bbTRONLY").Insert Where:=oCCA.Range, RichText:=True

    Case chbTR And chbR185
    Set oTmp = Templates("C:\Users\sarahjane.munt\AppData\Roaming\Microsoft\Templates\Norm al.dotm")
    oTmp.BuildingBlockEntries("bbTRR185").Insert Where:=oCCA.Range, RichText:=True
    End Select

    Now if I try to run this it tells me there is an insert error on the building block line: Method 'Insert' of object 'BuildingBlock' failed

    But if I substitute either of my my building blocks (bbTRONLY or bbTRR185) with bbTIFI from my earlier code then the code works fine! I don't understand why it would like one buildingblock and not another. I have checked that all the buildingblock properties are all the same for all buildingblocks and they are all saved in my Normal.dotm file.

    Any ideas what the problem might be? Is it possible my building blocks are just corrupted?

    Thanks

  8. #8
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    With the exception of a space in your template name Templates\Norm al.dotm") which probably shouldn't be there, I don't see any reason why the code isn't working.
    Greg

    Visit my website: http://gregmaxey.com

  9. #9
    Quote Originally Posted by gmaxey View Post
    With the exception of a space in your template name Templates\Norm al.dotm") which probably shouldn't be there, I don't see any reason why the code isn't working.

    The space in the code isn't there in the actual code, not sure why it showed like that when copied.

    I've figured out that the problem with my building block is that it has multiple paragraphs (using trial and error with some test building blocks), but when I created the building block I included the formatting paragraph mark so it should have saved all the formatting including the multiple paragraphs. I thought the whole point of a rich text content control was to be able to include this kind of thing, is there something I'm missing? I could split the building block into multiple building blocks but this seems like a lot of work to do something that the rich text content control was seemingly designed to do.

    Any thoughts?

    TIA


    UPDATE - I think I fixed it
    Last edited by Sarahjane; 04-08-2019 at 03:47 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
  •