Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-02-2024, 11:01 AM
WordExperto WordExperto is offline Highlight Word formtext next to formcheckbox when selected Windows 10 Highlight Word formtext next to formcheckbox when selected Office 2021
Novice
Highlight Word formtext next to formcheckbox when selected
 
Join Date: Feb 2024
Posts: 4
WordExperto is on a distinguished road
Default Highlight Word formtext next to formcheckbox when selected

Hello everyone and thank you very much in advance.


I have formcheckbox next to formtext and I need the last ones to be highlighted when the first ones are checked.
I have obtained this code but it gives me an error in a protected area and the forms need protection.
How can I do it?

Sub CheckBox_Click()
Dim ff As FormField
Dim rng As Range
Set ff = Selection.FormFields(1) 'Assuming the check box is the only form field in the selection
Set rng = ff.Range 'The range of the check box
rng.MoveEnd wdWord, 1 'Extend the range to include the next word
If ff.CheckBox.Value = True Then 'If the check box is checked
rng.HighlightColorIndex = wdYellow 'Highlight the range with yellow color
Else 'If the check box is unchecked
rng.HighlightColorIndex = wdNoHighlight 'Remove the highlight from the range
End If
End Sub
Reply With Quote
  #2  
Old 02-04-2024, 10:09 AM
WordExperto WordExperto is offline Highlight Word formtext next to formcheckbox when selected Windows 10 Highlight Word formtext next to formcheckbox when selected Office 2021
Novice
Highlight Word formtext next to formcheckbox when selected
 
Join Date: Feb 2024
Posts: 4
WordExperto is on a distinguished road
Default

I share the solution that I have achieved with the help of Copilot.
Since I had the data with legacy forms, I have converted them into content controls:

Sub TransformCheckBoxes()
Dim ff As FormField
Dim cc As ContentControl
Dim rng As Range

' Recorre todos los controles de formulario en el documento
For Each ff In ActiveDocument.FormFields
' Comprueba si el control de formulario es una casilla de verificación
If ff.Type = wdFieldFormCheckBox Then
' Guarda el rango del control de formulario
Set rng = ff.Range
' Elimina el control de formulario
ff.Delete
' Añade un control de contenido de casilla de verificación en el mismo rango
Set cc = ActiveDocument.ContentControls.Add(wdContentContro lCheckBox, rng)
End If
Next ff
End Sub

Next, I have added a title from cc1 to ccn

Sub AddTitlesToCheckBoxes()
Dim cc As ContentControl
Dim i As Integer

' Inicializa el contador
i = 1

' Recorre todos los controles de contenido en el documento
For Each cc In ActiveDocument.ContentControls
' Comprueba si el control de contenido es una casilla de verificación
If cc.Type = wdContentControlCheckBox Then
' Añade el título al control de contenido
cc.Title = "cc" & i
' Incrementa el contador
i = i + 1
End If
Next cc
End Sub


Now I convert the formtext fields to plain text and add a bookmark with the same name as the content control to the left of them

Sub TransformFormTextFields()
Dim ff As FormField
Dim cc As ContentControl
Dim rng As Range
Dim i As Integer

' Recorre todos los controles de contenido en el documento
For Each cc In ActiveDocument.ContentControls
' Comprueba si el control de contenido es una casilla de verificación
If cc.Type = wdContentControlCheckBox Then
' Recorre todos los campos de formulario en el mismo párrafo
For Each ff In cc.Range.Paragraphs(1).Range.FormFields
' Comprueba si el campo de formulario es un campo de texto
If ff.Type = wdFieldFormTextInput Then
' Guarda el rango y el texto del campo de formulario
Set rng = ff.Range
Dim texto As String
texto = ff.Result
' Elimina el campo de formulario
ff.Delete
' Inserta el texto en el mismo rango
rng.Text = texto
' Añade un marcador con el mismo nombre que el título del control de contenido
ActiveDocument.Bookmarks.Add cc.Title, rng
End If
Next ff
End If
Next cc
End Sub


This way, instead of form controls, I have content controls, with title cc1 to n, and plain text with bookmarks cc1 to n. Now I run this macro to highlight the text to the right of the checkbox in yellow when it is checked.

Public Sub StartChecking()
' Ejecuta la comprobación cada segundo
Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="CheckCheckBoxes"
End Sub

Public Sub CheckCheckBoxes()
Dim cc As ContentControl
For Each cc In ActiveDocument.ContentControls
' Comprueba si el título del control de contenido comienza con "cc"
If Left(cc.Title, 2) = "cc" Then
' Define el rango del texto a resaltar usando el marcador
Dim rango As Range
If ActiveDocument.Bookmarks.Exists(cc.Title) Then
Set rango = ActiveDocument.Bookmarks(cc.Title).Range

' Comprueba si la casilla de verificación está marcada
If cc.Checked Then
' Si está marcada, resalta el texto en amarillo
rango.HighlightColorIndex = wdYellow
Else
' Si no está marcada, quita el resaltado
rango.HighlightColorIndex = wdNoHighlight
End If
End If
End If
Next cc

' Continúa la comprobación
StartChecking
End Sub
Reply With Quote
Reply

Tags
form controls, formcheckbox, formtext



Similar Threads
Thread Thread Starter Forum Replies Last Post
Highlight Word formtext next to formcheckbox when selected VBA word macro to highlight a selected text just like a spell checker Pankaj Word VBA 1 10-14-2016 03:48 AM
Highlight Word formtext next to formcheckbox when selected Word 2010 FormText appears in doc instead of text mp04279 Word 1 02-17-2016 08:16 PM
Highlight Word formtext next to formcheckbox when selected FORMTEXT field contained in INCLUDETEXT bookmark emwinschuh Word 1 08-17-2015 08:55 AM
Macro to find coloured text and replace with form-field/formtext containing that text tarktran Word VBA 1 11-26-2014 08:12 AM
find - reading highlight - highlight all / highlight doesn't stick when saved bobk544 Word 3 04-15-2009 03:31 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:26 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