View Single Post
 
Old 02-14-2023, 10:48 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
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 ofgmayor has much to be proud of
Default

Hmmm. I can foresee a couple of issues here.

1. As you can only make one selection from a combo box, the only justification for having four rich text controls would be if the four related paragraphs are scattered around the document. Otherwise you could put the related values in just one control.
2. As you point out, a combo box allows for user entry. What do you want entered if the user enters in a non-standard text?
3. You can make the unused content controls disappear by filling them with a zero length space - ChrW(8203)
The following code Will do what you describe. It goes in the ThisDocument module
Code:
Option Explicit

Private OCC As ContentControl
Private sNul  As String

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim sCC As String
    sNul = ChrW(8203)
    If ContentControl.Title = "Combo1" Then
        If ContentControl.ShowingPlaceholderText = False Then
            Select Case Trim(ContentControl.Range.Text)
                Case "BCA_DCC"
                    For Each OCC In ActiveDocument.ContentControls
                        If OCC.Title = "BCA_DCC.TXT" Then
                            OCC.Range.Text = "Paragraph 1"
                        Else
                            If OCC.Type = wdContentControlRichText Then
                                OCC.Range.Text = sNul
                            End If
                        End If
                    Next OCC
                Case "BCA_NODCC"
                    For Each OCC In ActiveDocument.ContentControls
                        If OCC.Title = "BCA_NODCC.TXT" Then
                            OCC.Range.Text = "Paragraph 2"
                        Else
                            If OCC.Type = wdContentControlRichText Then
                                OCC.Range.Text = sNul
                            End If
                        End If
                    Next OCC
                Case "SOA_DCC"
                    For Each OCC In ActiveDocument.ContentControls
                        If OCC.Title = "SOA_DCC.TXT" Then
                            OCC.Range.Text = "Paragraph 3"
                        Else
                            If OCC.Type = wdContentControlRichText Then
                                OCC.Range.Text = sNul
                            End If
                        End If
                    Next OCC
                Case "SOA_NODCC"
                    For Each OCC In ActiveDocument.ContentControls
                        If OCC.Title = "SOA_NODCC.TXT" Then
                            OCC.Range.Text = "Paragraph 4"
                        Else
                            If OCC.Type = wdContentControlRichText Then
                                OCC.Range.Text = sNul
                            End If
                        End If
                    Next OCC
                Case Else
                    For Each OCC In ActiveDocument.ContentControls
                        If OCC.Type = wdContentControlRichText Then
                            OCC.Range.Text = sNul
                        End If
                    Next OCC
            End Select
        Else
            For Each OCC In ActiveDocument.ContentControls
                If OCC.Type = wdContentControlRichText Then
                    OCC.Range.Text = sNul
                End If
            Next OCC
        End If
    End If
End Sub
If the document is a template, which it almost certainly should be, you can hide the rich text controls with
Code:
Private Sub Document_New()
    sNul = ChrW(8203)
    For Each OCC In ActiveDocument.ContentControls
        If OCC.Type = wdContentControlRichText Then
            OCC.Range.Text = sNul
        End If
    Next OCC
End Sub
in the same module.
You will also need a macro to display them again for the purpose of editing (or you could use Insert Content Control Add-In

Code:
Sub ShowCCs()
Dim oCC As ContentControl
    For Each oCC In ActiveDocument.ContentControls
        oCC.Range.Text = ""
    Next oCC
End Sub
Attached Files
File Type: dotm Choose an item.dotm (28.1 KB, 7 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