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
|