Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-14-2021, 07:43 AM
gsfousteris gsfousteris is offline VB Script using Content Control Windows 10 VB Script using Content Control Office 2019
Novice
VB Script using Content Control
 
Join Date: Dec 2021
Posts: 5
gsfousteris is on a distinguished road
Cool VB Script using Content Control


I'm am to VBA coding in Microsoft Word. I have a Word document and there is a Plain text content control field (CCtext). If this CCtext gets the value "Test", I want the Checkbox Content Control (CCbox1) and Checkbox Content Control (CCbox4) to be checked. If this CCtext gets the value "Actual", I want the Checkbox Content Control (CCbox2) and Checkbox Content Control (CCbox3) to be checked.

Any idea, how to do that?
Reply With Quote
  #2  
Old 12-14-2021, 10:23 PM
gmayor's Avatar
gmayor gmayor is offline VB Script using Content Control Windows 10 VB Script using Content Control Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

In the ThisDocument module of the document enter the following. Note that content controls and their contents are case sensitive so the code refers to those lower case versions of those values. The check boxes will update when you click out of the test cc.

To reduce the potential for misspelling errors, if possible, you might consider replacing the plain text CC with a listbox CC - see
https://www.gmayor.com/insert_content_control_addin.htm



Code:
Option Explicit

Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
    Select Case LCase(ContentControl.Title)
        Case Is = "cctext"
            If ContentControl.ShowingPlaceholderText = True Then
                FillCB "CCbox1", False
                FillCB "CCbox2", False
                FillCB "CCbox3", False
                FillCB "CCbox4", False
            End If
    End Select
End Sub

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    Select Case LCase(ContentControl.Title)
        Case Is = "cctext"
            FillCB "CCbox1", False
            FillCB "CCbox2", False
            FillCB "CCbox3", False
            FillCB "CCbox4", False
            If ContentControl.ShowingPlaceholderText = False Then
                If InStr(1, LCase(ContentControl.Range.Text), "test") > 0 Then
                    FillCB "CCbox1", True
                    FillCB "CCbox4", True
                End If
                If InStr(1, LCase(ContentControl.Range.Text), "actual") > 0 Then
                    FillCB "CCbox2", True
                    FillCB "CCbox3", True
                End If
            End If
        Case Else
    End Select
End Sub

Private Sub FillCB(strCCTitle As String, bValue As Boolean, Optional bLock As Boolean = False)
'Graham Mayor - https://www.gmayor.com - Last updated - 15 Dec 2021
Dim oCC As ContentControl
    On Error GoTo lbl_Exit
    For Each oCC In ActiveDocument.ContentControls
        If LCase(oCC.Title) = LCase(strCCTitle) Then
            oCC.LockContents = False
            oCC.Checked = bValue
            oCC.LockContentControl = True
            If bLock = True Then oCC.LockContents = True
            Exit For
        End If
    Next oCC
lbl_Exit:
    Set oCC = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-13-2022, 04:25 AM
gsfousteris gsfousteris is offline VB Script using Content Control Windows 10 VB Script using Content Control Office 2019
Novice
VB Script using Content Control
 
Join Date: Dec 2021
Posts: 5
gsfousteris is on a distinguished road
Default

It does not run as it asks to create a new Macro.
Reply With Quote
  #4  
Old 01-13-2022, 04:57 AM
gmayor's Avatar
gmayor gmayor is offline VB Script using Content Control Windows 10 VB Script using Content Control Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It is impossible to guess what you have done wrong as all the macros required are present in the listing.

See example attached. Change the value in the text box to 'test' or 'actual' and click elsewhere in the document to run the macro.
Attached Files
File Type: docm example.docm (29.1 KB, 14 views)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 01-13-2022, 05:15 AM
gsfousteris gsfousteris is offline VB Script using Content Control Windows 10 VB Script using Content Control Office 2019
Novice
VB Script using Content Control
 
Join Date: Dec 2021
Posts: 5
gsfousteris is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
It is impossible to guess what you have done wrong as all the macros required are present in the listing.

See example attached. Change the value in the text box to 'test' or 'actual' and click elsewhere in the document to run the macro.
i just renamed the control fields.
Attached Files
File Type: docm Attachment1.docm (39.4 KB, 16 views)
Reply With Quote
  #6  
Old 01-13-2022, 10:51 PM
gmayor's Avatar
gmayor gmayor is offline VB Script using Content Control Windows 10 VB Script using Content Control Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The code references are to lower case so
Code:
Case Is = "ComSystem"
is never going to be true

Use instead
Code:
Case Is = "comsystem"
Similarly
Code:
If InStr(1, LCase(ContentControl.Range.Text), "test") > 0 Then
There doesn't appear to be a content control titled "BVW" in the document?
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 01-14-2022, 01:02 AM
gsfousteris gsfousteris is offline VB Script using Content Control Windows 10 VB Script using Content Control Office 2019
Novice
VB Script using Content Control
 
Join Date: Dec 2021
Posts: 5
gsfousteris is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
The code references are to lower case so
Code:
Case Is = "ComSystem"
is never going to be true

Use instead
Code:
Case Is = "comsystem"
Similarly
Code:
If InStr(1, LCase(ContentControl.Range.Text), "test") > 0 Then
There doesn't appear to be a content control titled "BVW" in the document?
Yeeeees! You really rock and thanks for your valuable assistance. It worked perfectly.

One last question, as the "comsystem" value comes from an automated script and there is no manual intervention to word document, i see that unless i press anywhere in the doc, content control checkboxes are not updated.
Is there any suggestion, to be updated while "Save as" or "Export" are automatically hitted?
Reply With Quote
  #8  
Old 01-14-2022, 04:00 AM
gmayor's Avatar
gmayor gmayor is offline VB Script using Content Control Windows 10 VB Script using Content Control Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If there is no manual intervention, you need a different approach. Lose the code in the ThisDocument module and create a new module and add the following.
Code:
Option Explicit

Sub FileSave()
    Document_UpdateContentControls
    ActiveDocument.Save
End Sub

Sub Document_UpdateContentControls()
Dim oCC As ContentControl
    For Each oCC In ActiveDocument.ContentControls
        Select Case LCase(oCC.Title)
            Case Is = "comsystem"
                FillCB "Int4", False
                FillCB "BRY5", False
                FillCB "BVW", False
                FillCB "Con3", False
                If InStr(1, LCase(oCC.Range.Text), "test") > 0 Then
                    FillCB "Int4", True
                    FillCB "Con3", True
                End If
                If InStr(1, LCase(oCC.Range.Text), "actual") > 0 Then
                    FillCB "BRY5", True
                    FillCB "BVW", True
                End If
            Case Else
        End Select
    Next oCC
    Set oCC = Nothing
End Sub

Private Sub FillCB(strCCTitle As String, bValue As Boolean, Optional bLock As Boolean = False)

Dim oCC As ContentControl
    On Error GoTo lbl_Exit
    For Each oCC In ActiveDocument.ContentControls
        If LCase(oCC.Title) = LCase(strCCTitle) Then
            oCC.LockContents = False
            oCC.Checked = bValue
            oCC.LockContentControl = True
            If bLock = True Then oCC.LockContents = True
            Exit For
        End If
    Next oCC
lbl_Exit:
    Set oCC = Nothing
    Exit Sub
End Sub

You need to be able to call the Document_UpdateContentControls macro - preferably from your automated script, but here it is called from a FileSave macro which intercepts the save command CTRL+S and runs the macro before saving.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 01-26-2022, 01:11 AM
gsfousteris gsfousteris is offline VB Script using Content Control Windows 10 VB Script using Content Control Office 2019
Novice
VB Script using Content Control
 
Join Date: Dec 2021
Posts: 5
gsfousteris is on a distinguished road
Default

I really appreciate your help. It worked smoothly.
I called the macro from my automated script and now it is up & running.

Thank you @gmayor
Reply With Quote
Reply

Tags
content control events, vba combobox, vba word

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB Script using Content Control Change Value of a Content Control based on the value of another content Control jsc_msoffice Word VBA 2 05-21-2021 09:51 PM
VB Script using Content Control Content Control Dropdowns - How to duplicate the content control and allow a second choice ashleyf Word VBA 2 03-19-2020 09:11 AM
Content Control content deleted when re-uploading to SharePoint Peterson Word 5 06-27-2018 08:13 PM
VB Script using Content Control Clicking the selected Content Control checkbox returns wrong control in vba event DougsGraphics Word VBA 2 06-24-2015 07:31 AM
VB Script using Content Control Deleting a table from a content control -- preserving the content control BrainSlugs83 Word Tables 8 11-14-2013 03:06 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:45 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