Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-10-2017, 02:20 PM
kevinbradley57 kevinbradley57 is offline Duplicating one or more table rows or an entire table with content controls Windows 7 64bit Duplicating one or more table rows or an entire table with content controls Office 2010 64bit
Advanced Beginner
Duplicating one or more table rows or an entire table with content controls
 
Join Date: Jul 2017
Posts: 84
kevinbradley57 is on a distinguished road
Default Duplicating one or more table rows or an entire table with content controls

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.
Attached Files
File Type: docm Add New Row Template.docm (70.5 KB, 14 views)
Reply With Quote
  #2  
Old 08-10-2017, 03:09 PM
macropod's Avatar
macropod macropod is offline Duplicating one or more table rows or an entire table with content controls Windows 7 64bit Duplicating one or more table rows or an entire table with content controls Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

See, for example:
https://www.msofficeforums.com/word-...html#post87989
https://www.msofficeforums.com/word-...html#post38461
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 08-11-2017, 09:09 AM
kevinbradley57 kevinbradley57 is offline Duplicating one or more table rows or an entire table with content controls Windows 7 64bit Duplicating one or more table rows or an entire table with content controls Office 2010 64bit
Advanced Beginner
Duplicating one or more table rows or an entire table with content controls
 
Join Date: Jul 2017
Posts: 84
kevinbradley57 is on a distinguished road
Default

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?
Reply With Quote
  #4  
Old 08-11-2017, 03:11 PM
macropod's Avatar
macropod macropod is offline Duplicating one or more table rows or an entire table with content controls Windows 7 64bit Duplicating one or more table rows or an entire table with content controls Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
Of course, you'd need to integrate that with the other code that checks whether you're in the right table, whether the user actually wants to add a new table and, if so, clear any content controls in the new table.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 08-14-2017, 04:21 PM
kevinbradley57 kevinbradley57 is offline Duplicating one or more table rows or an entire table with content controls Windows 7 64bit Duplicating one or more table rows or an entire table with content controls Office 2010 64bit
Advanced Beginner
Duplicating one or more table rows or an entire table with content controls
 
Join Date: Jul 2017
Posts: 84
kevinbradley57 is on a distinguished road
Default

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?
Reply With Quote
  #6  
Old 08-14-2017, 04:24 PM
macropod's Avatar
macropod macropod is offline Duplicating one or more table rows or an entire table with content controls Windows 7 64bit Duplicating one or more table rows or an entire table with content controls Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 08-14-2017, 04:26 PM
kevinbradley57 kevinbradley57 is offline Duplicating one or more table rows or an entire table with content controls Windows 7 64bit Duplicating one or more table rows or an entire table with content controls Office 2010 64bit
Advanced Beginner
Duplicating one or more table rows or an entire table with content controls
 
Join Date: Jul 2017
Posts: 84
kevinbradley57 is on a distinguished road
Default

That helps -- thanks!
Reply With Quote
  #8  
Old 08-14-2017, 08:28 PM
kevinbradley57 kevinbradley57 is offline Duplicating one or more table rows or an entire table with content controls Windows 7 64bit Duplicating one or more table rows or an entire table with content controls Office 2010 64bit
Advanced Beginner
Duplicating one or more table rows or an entire table with content controls
 
Join Date: Jul 2017
Posts: 84
kevinbradley57 is on a distinguished road
Default Bug -- add table row with content control

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?
Attached Files
File Type: docm Content controls in last table row.docm (67.5 KB, 17 views)

Last edited by kevinbradley57; 08-14-2017 at 08:33 PM. Reason: Fix typo
Reply With Quote
  #9  
Old 08-14-2017, 10:31 PM
macropod's Avatar
macropod macropod is offline Duplicating one or more table rows or an entire table with content controls Windows 7 64bit Duplicating one or more table rows or an entire table with content controls Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #10  
Old 08-17-2017, 11:26 AM
kevinbradley57 kevinbradley57 is offline Duplicating one or more table rows or an entire table with content controls Windows 7 64bit Duplicating one or more table rows or an entire table with content controls Office 2010 64bit
Advanced Beginner
Duplicating one or more table rows or an entire table with content controls
 
Join Date: Jul 2017
Posts: 84
kevinbradley57 is on a distinguished road
Default

Paul - That codes works great but doesn't copy the cell shading. What's missing?
Reply With Quote
  #11  
Old 08-17-2017, 02:13 PM
macropod's Avatar
macropod macropod is offline Duplicating one or more table rows or an entire table with content controls Windows 7 64bit Duplicating one or more table rows or an entire table with content controls Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply

Thread Tools
Display Modes


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 one or more table rows or an entire table with content controls Duplicating table set in word emmanpelayo Word VBA 2 08-08-2016 08:41 PM
Duplicating one or more table rows or an entire table with content controls 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
Duplicating one or more table rows or an entire table with content controls Grouping table rows to prevent individual rows from breaking across pages dennist77 Word 1 10-29-2013 11:39 PM

Other Forums: Access Forums

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