#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
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 |
#3
|
|||
|
|||
Quote:
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 |
#4
|
|||
|
|||
Quote:
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) 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 |
#5
|
||||
|
||||
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 |
#6
|
|||
|
|||
Quote:
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 |
#7
|
|||
|
|||
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. |
#8
|
|||
|
|||
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 |
#9
|
|||
|
|||
Quote:
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? |
#10
|
|||
|
|||
Quote:
|
#11
|
|||
|
|||
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 |
#12
|
|||
|
|||
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? |
#13
|
|||
|
|||
Quote:
|
#14
|
|||
|
|||
Quote:
|
#15
|
|||
|
|||
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?
|
|
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 |
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 |
Replace checkbox symbol with check box content control | canadansk | Word VBA | 5 | 04-01-2015 08:21 AM |