View Single Post
 
Old 02-26-2024, 02:34 PM
viewtost viewtost is offline Windows 10 Office 2019
Advanced Beginner
 
Join Date: Jul 2021
Posts: 35
viewtost is on a distinguished road
Default Macro to check the data in a content control box

Hi all,

I hope you are well.

I have a word file with a Rich text content control whose title is "Name", I tried to create a macro to tell whether or not there is data in the Rich text content control.
However, it doesn´t work as it always says that the Content control has data.

Anyone can help me set up this macro so it says “Content control name is empty” when there is no data in it? Or “Content control name has data”

Thanks


Code:
Sub CheckContentControl()
 
    Dim ctnControl As ContentControl
    Dim isEmpty As Boolean
 
    Set ctnControl = ActiveDocument.ContentControls("Name")
 
    If ctnControl Is Nothing Then
        MsgBox "Content control 'Name' not found.", vbExclamation
        Exit Sub
    End If
 
    isEmpty = Len(ctnControl.Range.Text) = 0
 
    If isEmpty Then
        MsgBox "Content control 'Name' is empty.", vbInformation
    Else
        MsgBox "Content control 'Name' has data.", vbI
Nformation
    End If
 
End Sub
Reply With Quote