Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
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: 4,185
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
  #2  
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
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 09:00 PM.


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