Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-26-2024, 02:34 PM
viewtost viewtost is offline Macro to check the data in a content control box Windows 10 Macro to check the data in a content control box Office 2019
Advanced Beginner
Macro to check the data in a content control box
 
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
  #2  
Old 02-26-2024, 03:35 PM
Guessed's Avatar
Guessed Guessed is offline Macro to check the data in a content control box Windows 10 Macro to check the data in a content control box Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 02-27-2024, 03:29 PM
viewtost viewtost is offline Macro to check the data in a content control box Windows 10 Macro to check the data in a content control box Office 2019
Advanced Beginner
Macro to check the data in a content control box
 
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
  #4  
Old 02-27-2024, 04:07 PM
Charles Kenyon Charles Kenyon is offline Macro to check the data in a content control box Windows 11 Macro to check the data in a content control box Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,140
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Quote:
Many thanks, it works. What if I have more content controls with different titles. For instance another one called "Department".
In a separate procedure have an Array that holds the names.
Cycle through those names calling the check control macro with each name. In the procedure that has the names, assign the variable strName to each name in turn and use:
Code:
CheckContentControl(strName)
Change the macro to something like:
Code:
Sub CheckContentControl(strName)
   ' Andrew Lockton
  ' https://www.msofficeforums.com/word-vba/52145-macro-check-data-content-control-box.html
'

   Dim aCC As ContentControl, bEmpty As Boolean
  For Each aCC In ActiveDocument.SelectContentControlsByTitle(strName)
    bEmpty = aCC.ShowingPlaceholderText
    If bEmpty Then
      MsgBox "The Content Control " & strName & " is Empty"
    Else
      MsgBox "The Content Control  " & strName & " has content"
    End If
  Next
End Sub
Reply With Quote
  #5  
Old 02-27-2024, 04:25 PM
Guessed's Avatar
Guessed Guessed is offline Macro to check the data in a content control box Windows 10 Macro to check the data in a content control box Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

It doesn't matter if you aren't great with macros - there is plenty of resources available and threads here to help you learn.

I can't give you a direct answer because you haven't been clear on what you 'want'. This is a good thing because it shows you are trying to learn but it also means we can't jump to a complete solution without considering the larger picture.

Food for Thought: If you are going to validate a 'completed' form then you need to work out what constitutes completed. Does EVERY CC need to be completed or just some of them? If your code identifies a CC that needs completing, how does it inform the user where that CC is? When should the code run?

Here are some threads which are worth studying:
https://www.msofficeforums.com/word/...completed.html
https://www.msofficeforums.com/word-...-vba-code.html
https://www.msofficeforums.com/word-...ault-text.html
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 02-28-2024, 02:35 AM
viewtost viewtost is offline Macro to check the data in a content control box Windows 10 Macro to check the data in a content control box Office 2019
Advanced Beginner
Macro to check the data in a content control box
 
Join Date: Jul 2021
Posts: 35
viewtost is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
It doesn't matter if you aren't great with macros - there is plenty of resources available and threads here to help you learn.

I can't give you a direct answer because you haven't been clear on what you 'want'. This is a good thing because it shows you are trying to learn but it also means we can't jump to a complete solution without considering the larger picture.

Food for Thought: If you are going to validate a 'completed' form then you need to work out what constitutes completed. Does EVERY CC need to be completed or just some of them? If your code identifies a CC that needs completing, how does it inform the user where that CC is? When should the code run?

Here are some threads which are worth studying:
https://www.msofficeforums.com/word/...completed.html
https://www.msofficeforums.com/word-...-vba-code.html
https://www.msofficeforums.com/word-...ault-text.html

Hi, thanks for your help.

Sorry if I haven't been clear on what I want to achieve with this macro. Basically, I have a form, and I would like the person filling it out not to leave any section blank. That's why I am inserting a text content control for each section of the form. If the user leaves any section blank, a message will pop up warning them. If they are not blank, I want the user to receive a message indicating that there is content in the fields.

I have tried using an array, and made sure the names are the same as the ones I have given to the content controls, but I am getting the error "Run time error 13 Type mismatch," so I'm not sure what's causing it.


Code:
Sub CheckAllContentControls()

  Dim aCC As ContentControl
  Dim ctrlTitle() As String  ' Array to store control titles

  ' Define the list of control titles
  ctrlTitle = Array("Name", "Position", "Store", "Date of Lateness", _
                   "Scheduled Time Lost", "Number of occurrences in 13 weeks", _
                   "Number of occurrences in 52 weeks", "Stage", "Details", _
                   "Is there any pattern", "Preventative measures", "Employee signature", _
                   "Employee signature date", "Employee name", "Managers signature", _
                   "Managers signature date", "Managers name")

  ' Loop through all control titles
  For i = 0 To UBound(ctrlTitle)
    ' Check each content control with the current title
    For Each aCC In ActiveDocument.SelectContentControlsByTitle(ctrlTitle(i))
      If aCC.ShowingPlaceholderText Then
        MsgBox "The Content Control '" & ctrlTitle(i) & "' is Empty"
      Else
        MsgBox "The Content Control '" & ctrlTitle(i) & "' has content"
      End If
    Next aCC
  Next i

End Sub
Thanks!
Reply With Quote
  #7  
Old 02-28-2024, 08:05 AM
gmaxey gmaxey is offline Macro to check the data in a content control box Windows 10 Macro to check the data in a content control box Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

You are getting a runtime error because your are trying to set a string variable to a variant variable.


Change ctrlTitle() as String to just ctrlTitle
or
Change Array("Name", "Position" , "Etc") to Split("Name,Position,Etc", ",")


Now sure you don't want a msgbox popping up every time your code finds a CC with content.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 02-28-2024, 08:15 AM
gmaxey gmaxey is offline Macro to check the data in a content control box Windows 10 Macro to check the data in a content control box Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Btw if every variable field in your form is a content control and every one needs to be filled in, you don't need to list the titles. Something like:


Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oCC As ContentControl
Dim oRng As Range
Dim strMsg As String
  For Each oCC In ActiveDocument.ContentControls
    If oCC.ShowingPlaceholderText Then
      If oRng Is Nothing Then
        Set oRng = oCC.Range
        oRng.Select
        strMsg = "One or more content controls is blank. Please enter data in the following CCs:" & vbCr & vbCr & oCC.Title & vbCr
      Else
        strMsg = strMsg & oCC.Title & vbCr
      End If
    End If
  Next oCC
  If strMsg = vbNullString Then strMsg = "Form complete.  Thank you."
  MsgBox strMsg, vbOKOnly, "REPORT"
lbl_Exit:
  Exit Sub
End Sub
should do.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 02-29-2024, 04:21 AM
viewtost viewtost is offline Macro to check the data in a content control box Windows 10 Macro to check the data in a content control box Office 2019
Advanced Beginner
Macro to check the data in a content control box
 
Join Date: Jul 2021
Posts: 35
viewtost is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
Btw if every variable field in your form is a content control and every one needs to be filled in, you don't need to list the titles. Something like:


Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oCC As ContentControl
Dim oRng As Range
Dim strMsg As String
  For Each oCC In ActiveDocument.ContentControls
    If oCC.ShowingPlaceholderText Then
      If oRng Is Nothing Then
        Set oRng = oCC.Range
        oRng.Select
        strMsg = "One or more content controls is blank. Please enter data in the following CCs:" & vbCr & vbCr & oCC.Title & vbCr
      Else
        strMsg = strMsg & oCC.Title & vbCr
      End If
    End If
  Next oCC
  If strMsg = vbNullString Then strMsg = "Form complete.  Thank you."
  MsgBox strMsg, vbOKOnly, "REPORT"
lbl_Exit:
  Exit Sub
End Sub
should do.
Hi Greg,

Many thanks. It works!

However, my question about listing the titles. Do you mean that the macro captures the content control titles without the need to list them here? Ie. I just have to give them a name in the Word file but I don't have to add those names to the code?
Reply With Quote
  #10  
Old 02-29-2024, 04:22 AM
viewtost viewtost is offline Macro to check the data in a content control box Windows 10 Macro to check the data in a content control box Office 2019
Advanced Beginner
Macro to check the data in a content control box
 
Join Date: Jul 2021
Posts: 35
viewtost is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
You are getting a runtime error because your are trying to set a string variable to a variant variable.


Change ctrlTitle() as String to just ctrlTitle
or
Change Array("Name", "Position" , "Etc") to Split("Name,Position,Etc", ",")


Now sure you don't want a msgbox popping up every time your code finds a CC with content.
I thought that variant variable could hold strings?
Reply With Quote
  #11  
Old 02-29-2024, 07:10 AM
gmaxey gmaxey is offline Macro to check the data in a content control box Windows 10 Macro to check the data in a content control box Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

A variant variable can hold strings. An array of strings e.g., Dim ctrlTitle() as String cannot resolve a variant array.


Consider:
Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim strVar() As String 'Here we have declared a string variable as an array of strings
Dim strVar2 As String
Dim varVar 'Here we have declared a variant variable
Dim lngNum As Long
  strVar = Split("A,B,C", ",")
  'A variant variable can be a string
  varVar = strVar
  'Here we set our variant variable to the variant returned by the Array funciton
  varVar = Array("A", 1, "C", 2.4)
  'While some other variable types are automatically resolved e.g., ...
  lngNum = 100
  strVar2 = lngNum
  '... a variant array cannot be resolved to a string
  On Error GoTo Err_Handler
  strVar = varVar
lbl_Exit:
  Exit Sub
Err_Handler:
  MsgBox "Do you see?"
  Resume lbl_Exit
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #12  
Old 02-29-2024, 07:16 AM
gmaxey gmaxey is offline Macro to check the data in a content control box Windows 10 Macro to check the data in a content control box Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

If you want to validate "all" CCs in the main text story range of the document then all you need is:


Dim oCC as ContentControl
For Each oCC In ActiveDocument.ContentControls
'Your code here
Next oCC


It doesn't matter if they have a title or not.


Note: That only applies if "all" of the CCs are in the main text storyrange. If you have CCs in the headers, footers, textboxes, etc. then you would have to process each of those storyranges as well.


If you only want to process some of the CCs in the document then you would list and process them in your code as you have done so far, or you could apply a unique tag to the ones you want to process e.g., "Validate" and use


Dim oCC as ContentControl
Dim oCCs as ContentControls
Set oCCs = ActiveDocument.SelectContentControlsbyTag("Validat e")
For Each oCC in oCCs
'Your code here
Next oCC


Get it?
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #13  
Old 03-05-2024, 10:06 AM
viewtost viewtost is offline Macro to check the data in a content control box Windows 10 Macro to check the data in a content control box Office 2019
Advanced Beginner
Macro to check the data in a content control box
 
Join Date: Jul 2021
Posts: 35
viewtost is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
A variant variable can hold strings. An array of strings e.g., Dim ctrlTitle() as String cannot resolve a variant array.


Consider:
Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim strVar() As String 'Here we have declared a string variable as an array of strings
Dim strVar2 As String
Dim varVar 'Here we have declared a variant variable
Dim lngNum As Long
  strVar = Split("A,B,C", ",")
  'A variant variable can be a string
  varVar = strVar
  'Here we set our variant variable to the variant returned by the Array funciton
  varVar = Array("A", 1, "C", 2.4)
  'While some other variable types are automatically resolved e.g., ...
  lngNum = 100
  strVar2 = lngNum
  '... a variant array cannot be resolved to a string
  On Error GoTo Err_Handler
  strVar = varVar
lbl_Exit:
  Exit Sub
Err_Handler:
  MsgBox "Do you see?"
  Resume lbl_Exit
End Sub
Understood, many thanks!
Reply With Quote
  #14  
Old 03-05-2024, 10:08 AM
viewtost viewtost is offline Macro to check the data in a content control box Windows 10 Macro to check the data in a content control box Office 2019
Advanced Beginner
Macro to check the data in a content control box
 
Join Date: Jul 2021
Posts: 35
viewtost is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
If you want to validate "all" CCs in the main text story range of the document then all you need is:


Dim oCC as ContentControl
For Each oCC In ActiveDocument.ContentControls
'Your code here
Next oCC


It doesn't matter if they have a title or not.


Note: That only applies if "all" of the CCs are in the main text storyrange. If you have CCs in the headers, footers, textboxes, etc. then you would have to process each of those storyranges as well.


If you only want to process some of the CCs in the document then you would list and process them in your code as you have done so far, or you could apply a unique tag to the ones you want to process e.g., "Validate" and use


Dim oCC as ContentControl
Dim oCCs as ContentControls
Set oCCs = ActiveDocument.SelectContentControlsbyTag("Validat e")
For Each oCC in oCCs
'Your code here
Next oCC


Get it?
The original macro that you had provided was working perfectly. Will stick to that one, many thanks again.
Reply With Quote
  #15  
Old 03-05-2024, 10:29 AM
viewtost viewtost is offline Macro to check the data in a content control box Windows 10 Macro to check the data in a content control box Office 2019
Advanced Beginner
Macro to check the data in a content control box
 
Join Date: Jul 2021
Posts: 35
viewtost is on a distinguished road
Default

I would like to ask an additional question, please. Is there any way to restrict the user so that users can only print, save, or share the file if all the content controls are not blank?
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Use VBA to show text when content control check box is checked kvrieze Word VBA 1 08-19-2023 03:11 AM
MsgBox launch when a Check Box Content Control is clicked wolfgrrl Word VBA 1 06-21-2019 12:13 PM
Macro to check the data in a content control box Check box content control to show text when checked Strogg Word 3 02-10-2019 11:45 AM
Check Box Content Control when checked users are presented with an option cryder Word 0 01-07-2016 05:11 AM
Macro to check the data in a content control box Replace checkbox symbol with check box content control canadansk Word VBA 5 04-01-2015 08:21 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:21 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft