Consulting

Results 1 to 5 of 5

Thread: Check Content Control for No Text

  1. #1
    VBAX Regular
    Joined
    Aug 2018
    Posts
    10
    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,585
    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
    10
    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

Posting Permissions

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