Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-31-2020, 10:23 PM
lord_kaiser lord_kaiser is offline VBA Adding Rows To Table With Content Control Windows 10 VBA Adding Rows To Table With Content Control Office 2016
Novice
VBA Adding Rows To Table With Content Control
 
Join Date: Apr 2018
Posts: 13
lord_kaiser is on a distinguished road
Default VBA Adding Rows To Table With Content Control

Hi,



I found the following code online for adding rows with content control to a table and this works well when exiting the content control.

How can i modify the code below to work only when the user clicks the command button?

Can someone point me in the right direction. Apologies if this has been answered previously but i was not able to find a solution in the forum.


Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) 'The following code conditionally adds a new row, with content controls, to the designated table. Dim i As Long, j As Long, Prot As Long Const Pwd As String = "" 'Insert password (if any) here 'Bookmarking the table provides the flexibility of being able to deal with the addition/deletion ' of other tables before the one we want to process. Const StrBkMk As String = "TblBkMk" 'Exit if we're not in a table - we don't really need this is using a bookmarked table, ' but it's a safeguard against the bookmark having been expanded/moved. If CCtrl.Range.Information(wdWithInTable) = False Then Exit Sub With ActiveDocument   If .Bookmarks.Exists(StrBkMk) = False Then     MsgBox "The table bookmark: '" & StrBkMk & "' is missing." & vbCr & _     "Please add it to the relevant table before continuing.", vbExclamation     Exit Sub   End If End With With CCtrl   'Check that the Content Control is within our bookmarked table's range.   If .Range.InRange(ActiveDocument.Bookmarks(StrBkMk).Range) = False Then Exit Sub   ' One could test for a particular table instead, in which case all the code dealing   ' with wdWithInTable & StrBkMk can be deleted. For example:   'If .Range.InRange(ActiveDocument.Tables(1).Range) = False Then Exit Sub   'Get the number of ContentControls in the table   i = .Range.Tables(1).Range.ContentControls.Count   'Get our ContentControl's index # in the table   j = ActiveDocument.Range(.Range.Tables(1).Range.Start, .Range.End).ContentControls.Count   'Check that we're using the last content control   If i <> j Then Exit Sub End With 'Solicit user input If MsgBox("Add new row?", vbQuestion + vbYesNo) <> vbYes Then Exit Sub With ActiveDocument   ' Un-protect the document, if applicable   Prot = .ProtectionType   If .ProtectionType <> wdNoProtection Then     Prot = .ProtectionType     .Unprotect Password:=Pwd   End If   With Selection.Tables(1).Rows     'Insert an empty paragraph after our table, then replace it with a replica of the last row     With .Last.Range       .Next.InsertBefore vbCr       .Next.FormattedText = .FormattedText     End With     'Reset all content controls in the new last row     For Each CCtrl In .Last.Range.ContentControls       With CCtrl         If .Type = wdContentControlCheckBox Then .Checked = False         If .Type = wdContentControlRichText Or .Type = wdContentControlText Then .Range.Text = ""         If .Type = wdContentControlDropdownList Then .DropdownListEntries(1).Select         If .Type = wdContentControlComboBox Then .DropdownListEntries(1).Select         If .Type = wdContentControlDate Then .Range.Text = ""       End With     Next   End With   'Update the bookmarked range   .Bookmarks.Add Name:=StrBkMk, Range:=Selection.Tables(1).Range   ' Re-protect the document, if applicable   .Protect Type:=Prot, Password:=Pwd End With End Sub
Reply With Quote
  #2  
Old 07-31-2020, 11:01 PM
gmayor's Avatar
gmayor gmayor is offline VBA Adding Rows To Table With Content Control Windows 10 VBA Adding Rows To Table With Content Control Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,211
gmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of light
Default

Based on your code example the following will add a row to Table1 in the current document. The macro should go in an ordinary module and not ThisDocument


Code:
Public Sub AddRow()
'The following code conditionally adds a new row, with content controls, to the designated table.
Dim i As Long, j As Long, Prot As Long
Dim oTable As Table
Dim CCtrl As ContentControl
Const Pwd As String = ""    'Insert password (if any) here

    Set oTable = ActiveDocument.Tables(1)

    'Solicit user input
    If MsgBox("Add new row?", vbQuestion + vbYesNo) <> vbYes Then Exit Sub
    With ActiveDocument
        ' Un-protect the document, if applicable
        Prot = .ProtectionType
        If .ProtectionType <> wdNoProtection Then
            Prot = .ProtectionType
            .Unprotect Password:=Pwd
        End If
        With oTable.Rows
            'Insert an empty paragraph after our table, then replace it with a replica of the last row
            With .Last.Range
                .Next.InsertBefore vbCr
                .Next.FormattedText = .FormattedText
            End With
            'Reset all content controls in the new last row
            For Each CCtrl In .Last.Range.ContentControls
                With CCtrl
                    If .Type = wdContentControlCheckBox Then .Checked = False
                    If .Type = wdContentControlRichText Or .Type = wdContentControlText Then .Range.Text = ""
                    If .Type = wdContentControlDropdownList Then .DropdownListEntries(1).Select
                    If .Type = wdContentControlComboBox Then .DropdownListEntries(1).Select
                    If .Type = wdContentControlDate Then .Range.Text = ""
                End With
            Next
        End With
        .Protect Type:=Prot, Password:=Pwd
    End With
    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 08-01-2020, 01:01 AM
lord_kaiser lord_kaiser is offline VBA Adding Rows To Table With Content Control Windows 10 VBA Adding Rows To Table With Content Control Office 2016
Novice
VBA Adding Rows To Table With Content Control
 
Join Date: Apr 2018
Posts: 13
lord_kaiser is on a distinguished road
Default

Thank you very much. That worked!!
Reply With Quote
Reply

Tags
content control, tables and objects, vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Adding Rows To Table With Content Control Add Rows to table that will include content controls of previous rows bobsagat Word VBA 20 01-27-2020 08:00 AM
Adding Row with Content control using Command Button baes10 Word VBA 0 12-13-2017 11:30 AM
VBA Adding Rows To Table With Content Control Duplicating one or more table rows or an entire table with content controls kevinbradley57 Word VBA 10 08-17-2017 02:13 PM
VBA Adding Rows To Table With Content Control Adding auto text to end of content control field ksigcajun Word VBA 4 10-13-2014 05:37 AM
VBA Adding Rows To Table With 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 - Senior Forums

All times are GMT -7. The time now is 03:38 PM.


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