Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-11-2024, 01:41 AM
WordVB WordVB is offline Bold certain parts of bookmarks text code help Windows 11 Bold certain parts of bookmarks text code help Office 2016
Novice
Bold certain parts of bookmarks text code help
 
Join Date: Jan 2024
Location: Ireland
Posts: 12
WordVB is on a distinguished road
Default Bold certain parts of bookmarks text code help

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.
Reply With Quote
  #2  
Old 01-11-2024, 05:15 PM
Guessed's Avatar
Guessed Guessed is offline Bold certain parts of bookmarks text code help Windows 10 Bold certain parts of bookmarks text code help Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 01-12-2024, 02:47 AM
WordVB WordVB is offline Bold certain parts of bookmarks text code help Windows 11 Bold certain parts of bookmarks text code help Office 2016
Novice
Bold certain parts of bookmarks text code help
 
Join Date: Jan 2024
Location: Ireland
Posts: 12
WordVB is on a distinguished road
Default

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?
Reply With Quote
  #4  
Old 01-12-2024, 03:03 AM
Italophile Italophile is online now Bold certain parts of bookmarks text code help Windows 11 Bold certain parts of bookmarks text code help Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Guessed has assumed that the checkboxes you are using are content controls, which they should be for a modern document.
Attached Images
File Type: png Screenshot 2024-01-12 095945.png (30.7 KB, 12 views)
Reply With Quote
  #5  
Old 01-12-2024, 03:31 AM
WordVB WordVB is offline Bold certain parts of bookmarks text code help Windows 11 Bold certain parts of bookmarks text code help Office 2016
Novice
Bold certain parts of bookmarks text code help
 
Join Date: Jan 2024
Location: Ireland
Posts: 12
WordVB is on a distinguished road
Default

Thank you. I think I have all linked, am I missing something? Appreciate the help!
Attached Images
File Type: jpg Capture1.JPG (53.7 KB, 11 views)
File Type: jpg Capture2.JPG (40.4 KB, 13 views)
File Type: jpg Capture3.JPG (114.0 KB, 11 views)
Reply With Quote
  #6  
Old 01-12-2024, 08:59 AM
Italophile Italophile is online now Bold certain parts of bookmarks text code help Windows 11 Bold certain parts of bookmarks text code help Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

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
Reply With Quote
  #7  
Old 01-12-2024, 09:18 AM
WordVB WordVB is offline Bold certain parts of bookmarks text code help Windows 11 Bold certain parts of bookmarks text code help Office 2016
Novice
Bold certain parts of bookmarks text code help
 
Join Date: Jan 2024
Location: Ireland
Posts: 12
WordVB is on a distinguished road
Default

Thank you, we are getting there. Still one error remains
Attached Images
File Type: jpg Capture4.JPG (86.2 KB, 12 views)
File Type: jpg Capture5.JPG (11.1 KB, 10 views)
Reply With Quote
  #8  
Old 01-12-2024, 11:41 AM
Italophile Italophile is online now Bold certain parts of bookmarks text code help Windows 11 Bold certain parts of bookmarks text code help Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Just add a check on the type of content control

Code:
If cc.type = wdContentControlCheckbox Then
DoMe cc.tag, cc.Checked
End If
Any errors a result of using my iPad
Reply With Quote
  #9  
Old 01-12-2024, 03:48 PM
Guessed's Avatar
Guessed Guessed is offline Bold certain parts of bookmarks text code help Windows 10 Bold certain parts of bookmarks text code help Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #10  
Old 01-15-2024, 03:00 AM
WordVB WordVB is offline Bold certain parts of bookmarks text code help Windows 11 Bold certain parts of bookmarks text code help Office 2016
Novice
Bold certain parts of bookmarks text code help
 
Join Date: Jan 2024
Location: Ireland
Posts: 12
WordVB is on a distinguished road
Default

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.
Reply With Quote
  #11  
Old 01-15-2024, 04:44 AM
WordVB WordVB is offline Bold certain parts of bookmarks text code help Windows 11 Bold certain parts of bookmarks text code help Office 2016
Novice
Bold certain parts of bookmarks text code help
 
Join Date: Jan 2024
Location: Ireland
Posts: 12
WordVB is on a distinguished road
Default

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
Reply With Quote
  #12  
Old 01-16-2024, 09:35 AM
WordVB WordVB is offline Bold certain parts of bookmarks text code help Windows 11 Bold certain parts of bookmarks text code help Office 2016
Novice
Bold certain parts of bookmarks text code help
 
Join Date: Jan 2024
Location: Ireland
Posts: 12
WordVB is on a distinguished road
Default

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
Reply With Quote
  #13  
Old 01-16-2024, 09:45 AM
Italophile Italophile is online now Bold certain parts of bookmarks text code help Windows 11 Bold certain parts of bookmarks text code help Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

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.
Reply With Quote
  #14  
Old 01-16-2024, 09:52 AM
WordVB WordVB is offline Bold certain parts of bookmarks text code help Windows 11 Bold certain parts of bookmarks text code help Office 2016
Novice
Bold certain parts of bookmarks text code help
 
Join Date: Jan 2024
Location: Ireland
Posts: 12
WordVB is on a distinguished road
Default

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
Reply With Quote
  #15  
Old 01-16-2024, 11:46 AM
Italophile Italophile is online now Bold certain parts of bookmarks text code help Windows 11 Bold certain parts of bookmarks text code help Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

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.
Reply With Quote
Reply

Tags
bold header



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bold certain parts of bookmarks text code help How to code a Find&Replace macro to remove a tab and bold the entire para Frogoogue Word VBA 4 05-30-2022 09:15 AM
Bold certain parts of bookmarks text code help bold (or unbold) section of text instead of bold toggling Burt Word 6 04-06-2019 09:09 AM
Bold certain parts of bookmarks text code help Text in #1 is made bold, rest of the document is edited, text in #1 is now not bold footer-assistance Word 1 06-29-2015 03:49 AM
Bold certain parts of bookmarks text code help Not Bold text but it comes up bold Pluviophile Word 7 10-22-2013 10:29 AM
Bold certain parts of bookmarks text code help VBA code to extract specific bookmarks from multiple word files Rattykins Word VBA 4 06-27-2012 10:02 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:23 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft