Consulting

Results 1 to 8 of 8

Thread: Check Content Control for No Text

  1. #1
    VBAX Regular
    Joined
    Aug 2018
    Posts
    16
    Location

    Check Content Control for No Text

    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!

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,706
    Location
    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
    Cheers
    Paul Edstein
    [MS MVP - Word]

  3. #3
    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.
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    VBAX Regular
    Joined
    Aug 2018
    Posts
    16
    Location
    Quote Originally Posted by gmayor View Post
    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!

  5. #5
    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
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    VBAX Regular
    Joined
    Aug 2018
    Posts
    16
    Location
    Quote Originally Posted by gmayor View Post
    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!

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,706
    Location
    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.
    Cheers
    Paul Edstein
    [MS MVP - Word]

  8. #8
    VBAX Regular
    Joined
    Aug 2018
    Posts
    16
    Location
    Quote Originally Posted by macropod View Post
    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
    Last edited by tgamekh; 10-06-2018 at 07:40 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •