Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-01-2022, 08:59 PM
CellCharger CellCharger is offline Create additional CCs based on Combo Box selection Windows 10 Create additional CCs based on Combo Box selection Office 2019
Novice
Create additional CCs based on Combo Box selection
 
Join Date: Oct 2021
Location: NJ, USA
Posts: 24
CellCharger is on a distinguished road
Default Create additional CCs based on Combo Box selection

Hi everyone,

I am not knowledgeable in VBA. I have a protected form with multiple Content Controls. One of which is a Combo Box.
Based on the Combo Box selection "C", I want the form to create two fields (Rich Text CC and Combo Box (with 3 selections: ACC, Ambient and Long-term), plus a text representing the fields' title).

To provide a visual, the form looks like the image to the left.
There are 3 empty cells where I need the additional CCs to be created in the area circled in red.
Once the user selects C from the "Protocol Type" Combo Box, 2 additional CC will be created. So basically, the form would look like the image to the right.



Something else I need to mention is that there is an existing macro in the form.

I tried to be as clear and descriptive as possible but please let me know if need to clarify anything else. I also attached the form.
Attached Images
File Type: png before.png (17.6 KB, 14 views)
File Type: png After.png (21.4 KB, 13 views)
Attached Files
File Type: docm Test form new macro enabled.docm (91.0 KB, 6 views)

Last edited by CellCharger; 11-02-2022 at 10:14 AM.
Reply With Quote
  #2  
Old 11-01-2022, 10:31 PM
gmayor's Avatar
gmayor gmayor is offline Create additional CCs based on Combo Box selection Windows 10 Create additional CCs based on Combo Box selection Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

There is no macro in your document. DOCX does not support macros.
The following will do what you ask. Put the code in the ThisDocument module of the document and save as macro enabled.
Code:
Option Explicit

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim oTable As Table
Dim oCC As ContentControl
Dim oRng As Range
Dim i As Integer
    Set oTable = ActiveDocument.Tables(1)
    If ContentControl.Title = "Protocol Type" Then
        If Not ActiveDocument.ProtectionType = wdNoProtection Then
            ActiveDocument.Unprotect
        End If
        If ContentControl.Range.Text = "D" Then
            Set oRng = oTable.Range.Cells(38).Range
            oRng.End = oRng.End - 1
            oRng.Text = "Stability timepoint"

            Set oRng = oTable.Range.Cells(39).Range
            oRng.End = oRng.End - 1
            Set oCC = oRng.ContentControls.Add(wdContentControlText)
            With oCC
                .Title = "Timepoint"
                .Tag = "Timepoint"
                .SetPlaceholderText , , "Timepoint"
                .Range.Editors.Add (wdEditorEveryone)
            End With

            Set oRng = oTable.Range.Cells(40).Range
            oRng.End = oRng.End - 1
            Set oCC = oRng.ContentControls.Add(wdContentControlDropdownList)
            With oCC
                .Title = "Temperature"
                .Tag = "Temperature"
                .SetPlaceholderText , , "Select Temperature"
                .DropdownListEntries.Add .PlaceholderText, ""
                .DropdownListEntries.Add "ACC", "ACC"
                .DropdownListEntries.Add "Long Term", "Long Term"
                .DropdownListEntries.Add "Ambient", "Ambient"
                .Range.Editors.Add (wdEditorEveryone)
            End With
        Else
            For i = 38 To 40
                Set oRng = oTable.Range.Cells(i).Range
                oRng.End = oRng.End - 1
                oRng.Text = ""
            Next i
        End If
        ContentControl.Range.Editors.Add (wdEditorEveryone)
        ActiveDocument.Protect (wdAllowOnlyReading)
    End If
lbl_Exit:
    Set oCC = Nothing
    Set oRng = Nothing
    Set oTable = 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 11-02-2022, 06:10 AM
CellCharger CellCharger is offline Create additional CCs based on Combo Box selection Windows 10 Create additional CCs based on Combo Box selection Office 2019
Novice
Create additional CCs based on Combo Box selection
 
Join Date: Oct 2021
Location: NJ, USA
Posts: 24
CellCharger is on a distinguished road
Default

Thank you Graham for your response.

This is the existing macro. It adds 30 days in the due date field . I am not sure but it looks it it disappeared when I saved the file as DOCX.

Code:
Option Explicit

Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Application.ScreenUpdating = False
Dim Dt As Date, StrDt As String
With CCtrl
  If .Title <> "Date of Initiation" Then Exit Sub
  If .ShowingPlaceholderText = True Then
    ActiveDocument.SelectContentControlsByTitle("Due Date")(1).Range.Text = ""
  Else
    StrDt = .Range.Text
    If IsDate(StrDt) Then
      Dt = CDate(StrDt)
    Else
      Dt = CDate(Split(StrDt, (Split(StrDt, " ")(0)))(1))
    End If
    ActiveDocument.SelectContentControlsByTitle("Due Date")(1).Range.Text = Format(Dt + 30, .DateDisplayFormat)
  End If
End With
Application.ScreenUpdating = True
End Sub
I also uploaded a macro enabled test form in the original post.

Last edited by CellCharger; 11-02-2022 at 09:36 AM.
Reply With Quote
  #4  
Old 11-02-2022, 09:31 PM
gmayor's Avatar
gmayor gmayor is offline Create additional CCs based on Combo Box selection Windows 10 Create additional CCs based on Combo Box selection Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

As I said earlier, DOCX format does not support macros.
It is simple enough to combine the macros.
Code:
Option Explicit

Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
    Application.ScreenUpdating = False
    Dim Dt As Date, StrDt As String
    Dim oTable As Table
    Dim oCC As ContentControl
    Dim oRng As Range
    Dim i As Integer
    Set oTable = ActiveDocument.Tables(1)
    Select Case CCtrl.Title
        Case "Protocol Type"
            If Not ActiveDocument.ProtectionType = wdNoProtection Then
                ActiveDocument.Unprotect
            End If
            If CCtrl.Range.Text = "D" Then
                Set oRng = oTable.Range.Cells(38).Range
                oRng.End = oRng.End - 1
                oRng.Text = "Stability timepoint"

                Set oRng = oTable.Range.Cells(39).Range
                oRng.End = oRng.End - 1
                Set oCC = oRng.ContentControls.Add(wdContentControlText)
                With oCC
                    .Title = "Timepoint"
                    .Tag = "Timepoint"
                    .SetPlaceholderText , , "Timepoint"
                    .Range.Editors.Add (wdEditorEveryone)
                End With

                Set oRng = oTable.Range.Cells(40).Range
                oRng.End = oRng.End - 1
                Set oCC = oRng.ContentControls.Add(wdContentControlDropdownList)
                With oCC
                    .Title = "Temperature"
                    .Tag = "Temperature"
                    .SetPlaceholderText , , "Select Temperature"
                    .DropdownListEntries.Add .PlaceholderText, ""
                    .DropdownListEntries.Add "ACC", "ACC"
                    .DropdownListEntries.Add "Long Term", "Long Term"
                    .DropdownListEntries.Add "Ambient", "Ambient"
                    .Range.Editors.Add (wdEditorEveryone)
                End With
            Else
                For i = 38 To 40
                    Set oRng = oTable.Range.Cells(i).Range
                    oRng.End = oRng.End - 1
                    oRng.Text = ""
                Next i
            End If
            CCtrl.Range.Editors.Add (wdEditorEveryone)
            ActiveDocument.Protect (wdAllowOnlyReading)
        Case "Date of Initiation"
            With CCtrl
                If .ShowingPlaceholderText = True Then
                    ActiveDocument.SelectContentControlsByTitle("Due Date")(1).Range.Text = ""
                Else
                    StrDt = .Range.Text
                    If IsDate(StrDt) Then
                        Dt = CDate(StrDt)
                    Else
                        Dt = CDate(Split(StrDt, (Split(StrDt, " ")(0)))(1))
                    End If
                    ActiveDocument.SelectContentControlsByTitle("Due Date")(1).Range.Text = Format(Dt + 30, .DateDisplayFormat)
                End If
            End With
        Case Else
    End Select
lbl_Exit:
    Application.ScreenUpdating = True
    Set oCC = Nothing
    Set oRng = Nothing
    Set oTable = 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
  #5  
Old 11-03-2022, 06:29 AM
CellCharger CellCharger is offline Create additional CCs based on Combo Box selection Windows 10 Create additional CCs based on Combo Box selection Office 2019
Novice
Create additional CCs based on Combo Box selection
 
Join Date: Oct 2021
Location: NJ, USA
Posts: 24
CellCharger is on a distinguished road
Default

Thank you so much Graham. It works great.

I appreciate your help!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Text based on selection of dropdown list cloud67 Word VBA 2 08-09-2019 06:46 AM
Reveal portion of document based on dropdown selection chappeja Word VBA 1 03-27-2019 08:36 PM
Create additional CCs based on Combo Box selection Mail Merge Using Rules "IF" to add additional Text Based On Merge Field Content Alfred Mail Merge 2 05-23-2017 10:59 PM
text based on Combo box selection rosscortb Word VBA 3 03-16-2015 06:57 PM
Inserting a particular image based on a combobox selection LeonieD PowerPoint 2 06-27-2014 05:39 PM

Other Forums: Access Forums

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