PDA

View Full Version : [SOLVED:] Check Content Control for No Text



tgamekh
09-10-2018, 04:36 PM
Hi,

Been searching for an answer on this for some time now. I have content controls that cannot be deleted, but their contents (text) can be removed/cleared. In some cases if the text is not removed a specific way it is possible that the content control can contain spaces, carriage returns, tabs, all of the above, a combination of the above, and in some cases I have found multiple any of the above. I want to be able to run a check for "blank" content controls that contain no actual text. Since vbCR, vbTab, vbLF, etc. seem to be picked up as text, in the sense that the presence of any of these seem to prevent the use of checking for nullstring or defaultplaceholder text. Is there a way to check for any content controls which do not contain text/number/special characters?

Thank you!

macropod
09-10-2018, 10:14 PM
For example:

Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
With CCtrl
If .ShowingPlaceholderText = True Then MsgBox "Nothing has been input into " & .Title
If Trim(Replace(Replace(Replace(.Range.Text, vbCr, ""), Chr(11), ""), vbTab, "")) = "" _
Then MsgBox "Nothing meaningful has been input into " & .Title
End With
End Sub

gmayor
09-10-2018, 10:52 PM
Usually if the contents of a content control are deleted the placeholder text is shown. If there is no placeholder text, the placeholder text is still 'shown' if the control is empty, so you can check whether the control is showing placeholder text e.g.


Function OCC_HasText(oCC As ContentControl) As Boolean
If Selection.InRange(oCC.Range) Then Selection.HomeKey wdStory
If oCC.ShowingPlaceholderText = False Then
OCC_HasText = True
End If
lbl_Exit:
Exit Function
End Function

Frankly it is simpler to have placeholder text associated with the control, which is the default. You may find https://www.gmayor.com/insert_content_control_addin.htm useful.

tgamekh
09-11-2018, 06:41 AM
Usually if the contents of a content control are deleted the placeholder text is shown. If there is no placeholder text, the placeholder text is still 'shown' if the control is empty, so you can check whether the control is showing placeholder text e.g.


Function OCC_HasText(oCC As ContentControl) As Boolean
If Selection.InRange(oCC.Range) Then Selection.HomeKey wdStory
If oCC.ShowingPlaceholderText = False Then
OCC_HasText = True
End If
lbl_Exit:
Exit Function
End Function

Frankly it is simpler to have placeholder text associated with the control, which is the default. You may find https://www.gmayor.com/insert_content_control_addin.htm useful.

Thank you both. I will do some more investigating. My experience so far is that any space, tab, or carriage return removes the placeholder text. There could be another component impacting this though and will retrace my steps to be sure. I appreciate both of your replies as well as the link for reference. Have a great day!

gmayor
09-11-2018, 08:36 PM
OK In that case you can combine the function I posted with the code that Paul posted to produce a function that checks for both apparently empty and actually empty controls e.g.


Function OCC_HasText(oCC As ContentControl) As Boolean
If Selection.InRange(oCC.Range) Then Selection.HomeKey wdStory
If oCC.ShowingPlaceholderText = False Then
If Not Trim(Replace(Replace(Replace(oCC.Range.Text, vbCr, ""), Chr(11), ""), vbTab, "")) = "" Then
OCC_HasText = True
End If
End If
lbl_Exit:
Exit Function
End Function

tgamekh
09-28-2018, 12:34 PM
OK In that case you can combine the function I posted with the code that Paul posted to produce a function that checks for both apparently empty and actually empty controls e.g.


Function OCC_HasText(oCC As ContentControl) As Boolean
If Selection.InRange(oCC.Range) Then Selection.HomeKey wdStory
If oCC.ShowingPlaceholderText = False Then
If Not Trim(Replace(Replace(Replace(oCC.Range.Text, vbCr, ""), Chr(11), ""), vbTab, "")) = "" Then
OCC_HasText = True
End If
End If
lbl_Exit:
Exit Function
End Function

Meant to circle back to this after testing and say thank you. That said, thank you!

macropod
09-28-2018, 03:14 PM
It's not apparent to me why you'd need a separate function, when a ContentControlOnExit macro such as I posted can do the test without one. Even if you want to limit the test to particular content controls, you'd ordinarily build that logic into the self-same ContentControlOnExit macro.

tgamekh
10-06-2018, 06:34 AM
It's not apparent to me why you'd need a separate function, when a ContentControlOnExit macro such as I posted can do the test without one. Even if you want to limit the test to particular content controls, you'd ordinarily build that logic into the self-same ContentControlOnExit macro.

Essentially, I wanted to create a macro button which did the check for specifically titled empty content controls, then display a message box given the user the count for them to review and correct. I know this was not clear in my original question, but I was wanting more of an idea how to accomplish 1 piece of the puzzle without having you solve the whole problem. The hurdle was test boxes that contained no text, but the presence spaces, tabs, returns, were interpreted for populated text. Your replies did just that and I was able to incorporate the trim aspects into my checks and calculations. The result below works for my purpose and I was able to replicate into multiple different "quality" checks, but this is the foundation:

Again, I thank you both!



Private Sub CommandButton1311_Click()

Dim empty_controls As Integer
empty_controls = 0

For Each content_control In ActiveDocument.ContentControls
If content_control.Title = "Finding" And (LTrim(Replace(Replace(Replace(content_control.Range.Text, vbCr, ""), Chr(11), ""), vbTab, "")) = "" Or ShowingPlaceholderText Or content_control.Range.Text = "Click or tap here to enter text.") Then
empty_controls = empty_controls + 1
End If
Next

MsgBox "Please review and correct any issues below:" & vbCr & vbCr & "Blank Findings: " & empty_controls, 0, "Document Integrity Check"

End Sub