![]() |
|
|||||||
|
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
I have a macro activated via QAT command button that allows the user to duplicate a table row with content controls, but it doesn't work on a table that has multiple content controls on each row. In the attached file, the macro works great on the Section 1 table but not Section 4.
Also, in Section 2, Section 3, and Section 5, I want the user to be able to add more tables as needed, retaining the content controls. If it would be easier to eliminate the spaces between the tables of the individual sections, that's okay. I added the spaces to make it easier to move the tables around while developing the document. |
|
#2
|
||||
|
||||
|
See, for example:
https://www.msofficeforums.com/word-...html#post87989 https://www.msofficeforums.com/word-...html#post38461
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Thanks, Paul. Both of those posts appear to be about duplicating the row the cursor is in. I need to either duplicate the row the cursor is in plus 1-3 preceding rows (depending on the table), or duplicate the entire table as a new table. Is that possible?
|
|
#4
|
||||
|
||||
|
The logic to add more than one row is essentially the same - you just need to extend the range being replicated to include those additional rows. ISTR posting code here some time ago to do something like that.
The code to replicate a table might be as simple as: Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
With CCtrl
Call TableDuplicate(.Range.Tables(1).Range)
End With
End Sub
Sub TableDuplicate(RngSrc As Range)
Dim RngTgt As Range
With ActiveDocument
On Error Resume Next
With RngSrc
With .Tables(1).Range.Duplicate
.Collapse wdCollapseEnd
.InsertAfter vbCr & vbCr
Set RngTgt = .Characters.Last
End With
End With
RngTgt.FormattedText = RngSrc.FormattedText
End With
Set RngTgt = Nothing
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#5
|
|||
|
|||
|
If I'm reading it right, the code depends on the last cell of the table having a Content Control. Is that correct? Is there a way to do the same thing without having a Content Control in the last cell?
|
|
#6
|
||||
|
||||
|
The code in https://www.msofficeforums.com/word-...html#post87989 triggers from the last content control in the table; it doesn't necessarily have to be a content control in the last cell.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#7
|
|||
|
|||
|
That helps -- thanks!
|
|
#8
|
|||
|
|||
|
Attached is a file with several tables and a QAT-activated macro that adds a table row including content controls. It works in all but one. The macro thinks there are no content controls in the last row of Table 4, but there are six.
Any suggestions? Last edited by kevinbradley57; 08-14-2017 at 08:33 PM. Reason: Fix typo |
|
#9
|
||||
|
||||
|
Try:
Code:
Sub InsertRowWithContent()
Application.ScreenUpdating = False
Dim vProtectionType As Variant, strPassword As String
Dim oRng As Word.Range, c As Long, i As Long, j As Long, t As Long
With Selection
If .Information(wdWithInTable) Then
With ActiveDocument
vProtectionType = .ProtectionType
If vProtectionType <> wdNoProtection Then
strPassword = "" 'Insert password here
.Unprotect Password:=strPassword
End If
End With
With .Tables(1)
j = .Range.Cells.Count + 1
.Rows.Add
For c = .Range.Cells.Count To j Step -1
Set oRng = .Cell(.Range.Cells(c).RowIndex - 1, .Range.Cells(c).ColumnIndex).Range
oRng.End = oRng.End - 1
With .Range.Cells(c).Range
'create new row content.
On Error Resume Next
.FormattedText = oRng.FormattedText
On Error GoTo 0
If .ContentControls.Count > 0 Then .Shading.BackgroundPatternColorIndex = wdNoHighlight
'Work with new row content.
For i = 1 To .ContentControls.Count
With .ContentControls(i)
Select Case .Type
Case wdContentControlCheckBox: .Checked = False
Case wdContentControlRichText, wdContentControlText, wdContentControlDate: .Range.Text = ""
Case wdContentControlDropdownList, wdContentControlComboBox
t = .Type
.Type = wdContentControlText
.Range.Text = ""
.Type = t
Case wdContentControlPicture
If Not .ShowingPlaceholderText Then
If .Range.InlineShapes.Count > 0 Then .Range.InlineShapes(1).Delete
End If
End Select
End With
Next i
End With
Next c
End With
If vProtectionType > wdNoProtection Then ActiveDocument.Protect Type:=vProtectionType, Password:=strPassword
Else
MsgBox "The cursor must be locate in a table row containing content controls.", _
vbInformation, vbOKOnly, "INVALID SELECTION"
End If
End With
Application.ScreenUpdating = False
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#10
|
|||
|
|||
|
Paul - That codes works great but doesn't copy the cell shading. What's missing?
|
|
#11
|
||||
|
||||
|
Did you select an option from the dropddown? If you're not getting the shading when you do, that's because of how your conditional shading code works, not because of how my code works.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Macro to save docx to doc that checks compatibility and converts content controls to static content. | staicumihai | Word VBA | 4 | 10-12-2016 08:23 PM |
Duplicating table set in word
|
emmanpelayo | Word VBA | 2 | 08-08-2016 08:41 PM |
Is it possible to copy non-contiguous rows of a Table and paste them as a separate Table in Word?
|
Joey Cheung | Word Tables | 1 | 08-12-2014 05:15 PM |
| Content Controls - Add Table Rows | dgiromini | Word VBA | 1 | 04-11-2014 03:04 PM |
Grouping table rows to prevent individual rows from breaking across pages
|
dennist77 | Word | 1 | 10-29-2013 11:39 PM |