Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-08-2015, 08:17 AM
ksigcajun ksigcajun is offline Code to add new row in table Windows 7 64bit Code to add new row in table Office 2010 64bit
Advanced Beginner
Code to add new row in table
 
Join Date: May 2014
Posts: 76
ksigcajun is on a distinguished road
Default Code to add new row in table

I initially wanted this code to prompt for the addition of a new row in a table. What the easiest way to change the code where it only prompts in one specific table and not every table in my word document.



Do I need to name the specific table somehow first?

Thanks for the help!

PHP Code:
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
With Selection.Range
  
If .Information(wdWithInTableThen
    
If .Cells(1).RowIndex = .Tables(1).Range.Cells _
                            
(.Tables(1).Range.Cells.Count).RowIndex Then
      
If .Cells(1).ColumnIndex = .Tables(1).Range.Cells _
                                 
(.Tables(1).Range.Cells.Count).ColumnIndex Then
        
If MsgBox(" Do you want to add a new row to the table?"_
                  vbQuestion 
vbYesNo"Question") = vbYes Then
          InsertRowWithContent
        End 
If
        
Set p_oTargetRow Selection.Rows(1)
      
End If
    
End If
  
End If
End With
End Sub 
Reply With Quote
  #2  
Old 09-08-2015, 04:22 PM
Guessed's Avatar
Guessed Guessed is offline Code to add new row in table Windows 7 32bit Code to add new row in table Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 2,554
Guessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to behold
Default

Do the other tables need to be in the Content Control too? That would be a simple fix if not.

The way I normally 'identify' a table is by applying a specific table style to it. The table style could have all the same attributes as the other tables but it allows you to test that attribute and hence you can have the macro only run if the 'right' table style is applied to the table.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 09-08-2015, 05:13 PM
macropod's Avatar
macropod macropod is offline Code to add new row in table Windows 7 64bit Code to add new row in table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,212
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

I'd normally do this kind of thing via an on-exit macro in the document's 'ThisDocument' code module:
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 Variant
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"
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 range.
  ' One could test for a particular table instead
  If .Range.InRange(ActiveDocument.Bookmarks(StrBkMk).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
    Selection.Tables(1).Range.Bookmarks.Add (StrBkMk)
    '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
  ' Re-protect the document, if applicable
  .Protect Type:=Prot, Password:=Pwd
End With
End Sub
The above code assumes the table is bookmarked 'TblBkMk'. As per the comments in the code, this allows for the possibility that other tables might be inserted/deleted before the one you're interested in. If you're not concerned about that, you could delete:
Code:
'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"
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
and change:
Code:
  'Check that the Content Control is within our bookmarked range.
  ' One could test for a particular table instead
  If .Range.InRange(ActiveDocument.Bookmarks(StrBkMk).Range) = False Then Exit Sub
to:
Code:
  'Check that the Content Control is within our table.
  If .Range.InRange(ActiveDocument.Tables(#).Range) = False Then Exit Sub
where # is your table number.

Note: If you simply delete the following code block, instead of changing it, the macro will apply to all tables with content controls:
Code:
  'Check that the Content Control is within our bookmarked range.
  ' One could test for a particular table instead
  If .Range.InRange(ActiveDocument.Bookmarks(StrBkMk).Range) = False Then Exit Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]

Last edited by macropod; 09-08-2015 at 08:03 PM. Reason: Revised code, with more commenting
Reply With Quote
  #4  
Old 07-13-2021, 02:57 PM
Kathy Kathy is offline Code to add new row in table Windows 10 Code to add new row in table Office 2016
Novice
 
Join Date: Jul 2021
Posts: 8
Kathy is on a distinguished road
Default

Macropod, thank you very much for the code. I think it's the last piece I need for a complicated form I've been working on (well, complicated for me, because I'm a self-taught Word user with no knowledge of macros and code, but very appreciative of you all who share them). However, I'm only able to add one new row to the table. If I try to add another row, nothing happens. Is that the intent of the code or am I/is it missing something?
Reply With Quote
  #5  
Old 07-13-2021, 03:51 PM
macropod's Avatar
macropod macropod is offline Code to add new row in table Windows 10 Code to add new row in table Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,212
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

The code only runs when you exit whatever is currently the last content control in the table. If you add a new row, the current last content control in the table becomes the last content control in the newly-added row.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 07-15-2021, 11:43 AM
Kathy Kathy is offline Code to add new row in table Windows 10 Code to add new row in table Office 2016
Novice
 
Join Date: Jul 2021
Posts: 8
Kathy is on a distinguished road
Default

Yes, I understood that. It just doesn't add another row when I click out of the new last content control of the table (in the original row, I have to click to the cell before it and not after it). I've attached the form in case it helps. The form uses hidden text in the content controls and there are links so the information entered into the top row shows up in the header of the following pages. (I haven't tried it, but I'm assuming the green text in the macro are only comments and can be deleted as long as the indents and everything else remains the same?) Thank you. I appreciate your time.
Attached Files
File Type: docm form.docm (62.0 KB, 1 views)
Reply With Quote
  #7  
Old 07-18-2021, 04:32 PM
macropod's Avatar
macropod macropod is offline Code to add new row in table Windows 10 Code to add new row in table Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,212
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

OK, before:
'Reset all content controls in the new last row
insert:
Selection.Tables(1).Range.Bookmarks.Add (StrBkMk)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 07-20-2021, 07:54 AM
Kathy Kathy is offline Code to add new row in table Windows 10 Code to add new row in table Office 2016
Novice
 
Join Date: Jul 2021
Posts: 8
Kathy is on a distinguished road
Default

Thank you. That worked. However, it works only when the form isn't password protected. The macro seems to run, but the new row doesn't show up. I thought it was working before, but maybe it wasn't?
Reply With Quote
  #9  
Old 07-20-2021, 04:59 PM
macropod's Avatar
macropod macropod is offline Code to add new row in table Windows 10 Code to add new row in table Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,212
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

Did you add the password to the code?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 07-22-2021, 09:04 AM
Kathy Kathy is offline Code to add new row in table Windows 10 Code to add new row in table Office 2016
Novice
 
Join Date: Jul 2021
Posts: 8
Kathy is on a distinguished road
Default

Yes, assuming it is only required in the line Const Pwd As String = "OHRForm"
I figured it would pick up the password every place it had Pwd.
Reply With Quote
  #11  
Old 07-23-2021, 12:54 AM
macropod's Avatar
macropod macropod is offline Code to add new row in table Windows 10 Code to add new row in table Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,212
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

Quote:
Originally Posted by Kathy View Post
Yes, assuming it is only required in the line Const Pwd As String = "OHRForm"
I figured it would pick up the password every place it had Pwd.
That is correct. What kind of document protection are you using?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 07-23-2021, 09:07 AM
Kathy Kathy is offline Code to add new row in table Windows 10 Code to add new row in table Office 2016
Novice
 
Join Date: Jul 2021
Posts: 8
Kathy is on a distinguished road
Default

Filling in forms. No formatting restrictions.
Reply With Quote
  #13  
Old 07-26-2021, 05:08 PM
macropod's Avatar
macropod macropod is offline Code to add new row in table Windows 10 Code to add new row in table Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,212
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

The code works fine for me with or without the 'filling in forms' protection. When the protection is applied, the code unprotects the document before adding the new row, then re-protects it afterwards.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #14  
Old 07-28-2021, 01:01 PM
Kathy Kathy is offline Code to add new row in table Windows 10 Code to add new row in table Office 2016
Novice
 
Join Date: Jul 2021
Posts: 8
Kathy is on a distinguished road
Default

Thank you for checking my work. I'll double check my steps to see if I can figure out why it doesn't work for me.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code to add new row in table Colour code mail merge header table cell backgrounds ScotsMaverick Mail Merge 20 05-08-2020 12:44 AM
Code to add new row in table Code to disable spacing between cells in table properties bloomhaven Word VBA 3 03-11-2015 04:08 PM
Creating VBA Code to Delete Empty Column in Table Faugs Word VBA 5 08-07-2014 03:29 PM
Code to add new row in table VBA Code to take data from a table in word document and place it in a summary table VBLearner Word VBA 1 03-09-2014 08:42 PM
VBA sort table code mikec Excel Programming 8 10-01-2013 04:37 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:38 AM.


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