![]() |
#1
|
|||
|
|||
![]()
Hi All,
I am looking for some ideas please. This is the first time I am using VB so still am very fresh here. I am working on a simple word document, where the front page is used to check boxes and based on true false values it inserts texts in bookmark fields further down the document. Managed to get all with the help of this Forum but got stuck with one step. The text I am inserting looks like below: "Title First line" What I want to do is to make word "Title" bold and leave the rest non-bold. Option Explicit Dim oRng As Range Dim oBM As Bookmark Private Sub CheckBox1_Click() If Me.CheckBox1.Value = True Then Set oRng = ActiveDocument.Bookmarks("Text1").Range oRng.Text = "Title" & Chr(10) & "First line" ActiveDocument.Bookmarks.Add "Text1", oRng With oRng.Font .Bold = True End With Else: Set oRng = ActiveDocument.Bookmarks("Text1").Range oRng.Text = "Blank" ActiveDocument.Bookmarks.Add "Text1", oRng With oRng.Font .Bold = False End With End If End Sub It could do something with selecting the range but I am unsure how. Any thoughts would be greatly appreciated. |
#2
|
||||
|
||||
![]()
I'm not a fan of bookmarks for this task because of the fiddly nature of having to keep recreating the bookmark after changing the text inside it. I would recommend you using Rich Text Content Controls instead to streamline this aspect.
So first create some Rich Text Content Controls in your document and for each one, set a unique name in the Title field In your userform for each of the Checkboxes, set their Tag properties to match the Title property you applied to each of the Content Controls in the document. Then put this code in your userform code. Code:
Private Sub CheckBox1_Click() DoMe CheckBox1.Tag, CheckBox1.Value End Sub Private Sub CheckBox2_Click() DoMe CheckBox2.Tag, CheckBox2.Value End Sub Private Sub CheckBox3_Click() DoMe CheckBox3.Tag, CheckBox3.Value End Sub Private Sub DoMe(sTag As String, bVal As Boolean) Dim aCC As ContentControl Set aCC = ActiveDocument.SelectContentControlsByTitle(sTag)(1) With aCC If bVal Then .Range.Text = "Title" & vbTab & "First line" .Range.Words(1).Bold = True Else .SetPlaceholderText Text:="Blank" ' line doesn't need to be retained if you have already placeholder text once .Range.Text = "" .Range.Bold = False End If End With End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
![]()
Thanks very much for the above. I see the logic, seems to be much better approach.
One question please, how do you set Tag properties to match the Title property to each of the Checkboxes? Is it in properties somewhere? |
#4
|
|||
|
|||
![]()
Guessed has assumed that the checkboxes you are using are content controls, which they should be for a modern document.
|
#5
|
|||
|
|||
![]()
Thank you. I think I have all linked, am I missing something? Appreciate the help!
|
#6
|
|||
|
|||
![]()
The content control properties are correct, but your code is written for legacy checkboxes. You need to replace your Click events with just the one routine below:
Code:
Private Sub Document_ContentControlOnExit(ByVal cc As ContentControl, Cancel As Boolean) DoMe cc.tag, cc.Checked End Sub |
#7
|
|||
|
|||
![]()
Thank you, we are getting there. Still one error remains
|
#8
|
|||
|
|||
![]()
Just add a check on the type of content control
Code:
If cc.type = wdContentControlCheckbox Then DoMe cc.tag, cc.Checked End If |
#9
|
||||
|
||||
![]()
The initial post included this line
If Me.CheckBox1.Value This made me think that you had the checkboxes on a VBA Userform since 'Me' is a reference to a VBA userform. If you ACTUALLY have Content Control Checkboxes in the body of the document then the coding can be simpler. Put this code into the ThisDocument module. You won't need any other code if you have set up your Tag/Title combinations with the Content Controls. Code:
Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean) Dim aCC2 As ContentControl, sTag As String If aCC.Type = wdContentControlCheckBox Then sTag = aCC.Tag Set aCC2 = ActiveDocument.SelectContentControlsByTitle(sTag)(1) With aCC2 If aCC.Checked Then aCC2.Range.Text = "Title" & vbTab & "First line" aCC2.Range.Words(1).Bold = True Else aCC2.SetPlaceholderText Text:="Blank" aCC2.Range.Text = "" End If End With End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#10
|
|||
|
|||
![]()
Thanks very much everyone.
I don't think I got to the level where I can use tags at this stage, but the last code works very well with Tag Title combinations. Can you help me with the last bit of the code please, what do I need to change in the code if I want to add additional checkboxes and text? Additional checkbox would have additional corresponding text. Appreciate your help, this project is almost finished. |
#11
|
|||
|
|||
![]()
Think I have it sorted. Works so far. Thanks for all your help everyone.
Private Sub Document_ContentControlOnexit(ByVal aCC As ContentControl, Cancel As Boolean) Dim aCC2 As ContentControl, sTag As String Select Case aCC.Title Case "C1" If aCC.Type = wdContentControlCheckBox Then sTag = aCC.Tag Set aCC2 = ActiveDocument.SelectContentControlsByTitle(sTag)( 1) With aCC2 If aCC.Checked Then aCC2.Range.Text = "Title" & Chr(10) & "First line" aCC2.Range.Words(1).Bold = True Else aCC2.SetPlaceholderText Text:="Blank" aCC2.Range.Text = "" End If End With End If End Select Select Case aCC.Title Case "C2" If aCC.Type = wdContentControlCheckBox Then sTag = aCC.Tag Set aCC2 = ActiveDocument.SelectContentControlsByTitle(sTag)( 1) With aCC2 If aCC.Checked Then aCC2.Range.Text = "Title1" & Chr(10) & "First line1" aCC2.Range.Words(1).Bold = True Else aCC2.SetPlaceholderText Text:="Blank" aCC2.Range.Text = "" End If End With End If End Select End Sub |
#12
|
|||
|
|||
![]()
The project is almost complete now, thank you for all your suggestions, using content control definitely streamlined the process.
I have spent much time on research but hit a brick wall again. I am sure there were much efficient ways around it but the word document works well and checkboxes populate text in rich text areas so I would be reluctant to make any significant changes to the code, however the text needs formatting. Unfortunately, one paragraph is over two pages long and should have different levels of lists formatting (Bullet points (chr(149), sub a) level and sub-sub i) level. Is there a way to select a range within the text and apply the list format? I am able to apply one list format to a whole range but not for parts of range and different levels of lists (as per above). Any help would be greatly appreciated again. Code:
Private Sub Document_ContentControlOnexit(ByVal aCC As ContentControl, Cancel As Boolean) Dim aCC2 As ContentControl, sTag As String Select Case aCC.Title Case "Title1" If aCC.Type = wdContentControlCheckBox Then sTag = aCC.Tag Set aCC2 = ActiveDocument.SelectContentControlsByTitle("Title")(1) With aCC2 If aCC.Checked Then aCC2.Range.Text = "HEADING BOLD" & Chr(10) & Chr(10) & "Text text text text text." & Chr(10) & Chr(10) & "Text text text text text" _ & Chr(10) & Chr(10) & "Bullet point text" _ & Chr(10) & Chr(10) & "a) List text" & Chr(10) & "b) List text" _ & Chr(10) & Chr(10) & "i) List text" & Chr(10) & "ii) List text" aCC2.Range.Words(1).Bold = True aCC2.Range.Words(2).Bold = True aCC2.Range.Font.Name = "Calibri" aCC2.Range.Font.Size = 11 aCC2.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify aCC2.Range.ParagraphFormat.SpaceAfter = 0 Else aCC2.SetPlaceholderText Text:="Blank" aCC2.Range.Text = "" End If End With End If End Select |
#13
|
|||
|
|||
![]()
You will save yourself a lot of grief if you use building-blocks for the text. Save your pre-formatted text as a building-block then you can insert it into the content-controls as required.
|
#14
|
|||
|
|||
![]()
A lot of grief is what I want to save, spending way too much time on this project. Last time I was writing scripts was in late 90s on Turbo Pascal, haven't done it ever since up until now.
Thanks Italophile, I'll research building blocks now. Appreciate the help |
#15
|
|||
|
|||
![]()
You’ll need to do all of this in a template (.dotx), not a document, though as your goal seems to be to build a document from the choices on the first page you should be using a template anyway.
|
![]() |
Tags |
bold header |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Frogoogue | Word VBA | 4 | 05-30-2022 09:15 AM |
![]() |
Burt | Word | 6 | 04-06-2019 09:09 AM |
![]() |
footer-assistance | Word | 1 | 06-29-2015 03:49 AM |
![]() |
Pluviophile | Word | 7 | 10-22-2013 10:29 AM |
![]() |
Rattykins | Word VBA | 4 | 06-27-2012 10:02 PM |