View Single Post
 
Old 02-27-2024, 03:29 PM
viewtost viewtost is offline Windows 10 Office 2019
Advanced Beginner
 
Join Date: Jul 2021
Posts: 35
viewtost is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
You've got a few problems with your code. Here is the basics
You could have more than one content control with the same title so you would need to be specific about which one you are talking about. eg the first one might be
Set aCC = ActiveDocument.SelectContentControlsByTitle("Name" )(1)

However, if you assume that there is at least one CC with that name in the document and it doesn't exist, then you have to handle an error. To avoid that, I've set up a loop that handles none, one or more instances without crashing

Code:
Sub CheckContentControl()
  Dim aCC As ContentControl, bEmpty As Boolean
  For Each aCC In ActiveDocument.SelectContentControlsByTitle("Name")
    bEmpty = aCC.ShowingPlaceholderText
    If bEmpty Then
      MsgBox "The Content Control is Empty"
    Else
      MsgBox "The Content Control has content"
    End If
  Next
End Sub

Many thanks, it works. What if I have more content controls with different titles. For instance another one called "Department".

I am not great with macros...

Thanks in advance
Reply With Quote