Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 08-07-2022, 10:36 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,962
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


For example, where the tables of interest are bookmarked as Tbl1, Tbl2, and Tbl3, respectively:
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, StrBkMk As String
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.
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("Tbl1").Range) = False) And _
    (.Range.InRange(ActiveDocument.Bookmarks("Tbl2").Range) = False) And _
    (.Range.InRange(ActiveDocument.Bookmarks("Tbl3").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
  StrBkMk = .Range.Tables(1).Range.Bookmarks(1).Name
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 .Bookmarks(StrBkMk).Range.Tables(1)
    'Insert an empty paragraph after our table, then replace it with a replica of the last row
    With .Rows.Last.Range
      .Next.InsertBefore vbCr
      .Next.FormattedText = .FormattedText
    End With
   .Range.Bookmarks.Add (StrBkMk)
    'Reset all content controls in the new last row
    For Each CCtrl In .Rows.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
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #17  
Old 08-08-2022, 12:05 AM
shanshan89 shanshan89 is offline Code to add new row in table Windows 10 Code to add new row in table Office 2019
Novice
 
Join Date: Jul 2022
Posts: 17
shanshan89 is on a distinguished road
Smile

Dear macropod,

Thanks for the help. This works well for me!

As mentioned earlier, I have 3 tables which I have already bookmarked the tables of interest as Tbl1, Tbl2, Tbl3. Each of these tables have 3 columns and 1 row. At the last column/last row/last cell, instead of one content control, I have multiple content controls, where the last content control (bottom most) is named as "Resolve Date".

As such, when the user is prompted to add a new row, in the new row itself, the bookmark location will be shifted (it will not be in the exact spot “Resolve Date” as in the first row that I want it to be) and the user will be prompted to enter new row again at multiple content controls, not at the content control named as “Resolve Date”.

I have sought Mr Gregory's opinion on this and he has kindly advised me to modify my code to look for a specific tag/name upon ContentControl on Enter.

I tried to incorporate another conditional if statement as follows:
If CCtrl.Title = "Resolve Date" Then
If MsgBox("Add new row?", vbQuestion + vbYesNo) <> vbYes Then Exit Sub
End If

However this does not work. Are you able to advise how I should modify my code pls?

Thanks.
Reply With Quote
  #18  
Old 08-11-2022, 12:43 AM
shanshan89 shanshan89 is offline Code to add new row in table Windows 10 Code to add new row in table Office 2019
Novice
 
Join Date: Jul 2022
Posts: 17
shanshan89 is on a distinguished road
Smile

Hi Macropod,

Thanks for the reply earlier.

I have actually found another method to solve this from Mr Gregory's webpage which is to add a command button in the QAT to append row in the table. This works well for me too.

Thank you very much!
Reply With Quote
  #19  
Old 08-11-2022, 07:48 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,962
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

Quote:
Originally Posted by shanshan89 View Post
As such, when the user is prompted to add a new row, in the new row itself, the bookmark location will be shifted (it will not be in the exact spot “Resolve Date” as in the first row that I want it to be) and the user will be prompted to enter new row again at multiple content controls, not at the content control named as “Resolve Date”.
I have no idea what you mean about a bookmark location being shifted or how that is relevant. As coded, the macro only ever fires when the last content control in a table is exited. What that content control is titled or tagged is immaterial. Note, too, that content control titles and tags have nothing to do with bookmarks.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #20  
Old 08-11-2022, 08:30 PM
shanshan89 shanshan89 is offline Code to add new row in table Windows 10 Code to add new row in table Office 2019
Novice
 
Join Date: Jul 2022
Posts: 17
shanshan89 is on a distinguished road
Smile

Quote:
Originally Posted by macropod View Post
Quote:
Originally Posted by shanshan89 View Post
As such, when the user is prompted to add a new row, in the new row itself, the bookmark location will be shifted (it will not be in the exact spot “Resolve Date” as in the first row that I want it to be) and the user will be prompted to enter new row again at multiple content controls, not at the content control named as “Resolve Date”.
I have no idea what you mean about a bookmark location being shifted or how that is relevant. As coded, the macro only ever fires when the last content control in a table is exited. What that content control is titled or tagged is immaterial. Note, too, that content control titles and tags have nothing to do with bookmarks.
Hi Macropod, I think that is because I have multiple content controls in my table in each cell. The code seems to work if you only have one content control in the last row/last cell. However, I have about 12 content controls in my last row/last cell and I would only like it to exit at the 12th content control. Without any bookmarks, the user will be prompted to add new row at all 12 content controls.
Reply With Quote
  #21  
Old 08-11-2022, 11:28 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,962
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

As written, the code checks whether it is exiting the last content control in the table. The number of content controls in that row is of no consequence. Indeed, the code was tested against tables having every possible kind of content control in a given row - including multiples of the same kind.

That said, it was not envisaged that the code would be run against a table having multiple content controls in the same cell. That could be accomodated by changing:
Code:
  'Get our ContentControl's index # in the table
  j = ActiveDocument.Range(.Range.Tables(1).Range.Start, .Range.End).ContentControls.Count
to:
Code:
  'Get our ContentControl's index # in the table
  j = ActiveDocument.Range(.Range.Tables(1).Range.Start, .Range.Cells(1).Range.Start).ContentControls.Count
  j = j + ActiveDocument.Range(.Range.Cells(1).Range.Start, .Range.End).ContentControls.Count
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #22  
Old 08-12-2022, 12:26 AM
shanshan89 shanshan89 is offline Code to add new row in table Windows 10 Code to add new row in table Office 2019
Novice
 
Join Date: Jul 2022
Posts: 17
shanshan89 is on a distinguished road
Smile

Quote:
Originally Posted by macropod View Post
As written, the code checks whether it is exiting the last content control in the table. The number of content controls in that row is of no consequence. Indeed, the code was tested against tables having every possible kind of content control in a given row - including multiples of the same kind.

That said, it was not envisaged that the code would be run against a table having multiple content controls in the same cell. That could be accomodated by changing:
Code:
  'Get our ContentControl's index # in the table
  j = ActiveDocument.Range(.Range.Tables(1).Range.Start, .Range.End).ContentControls.Count
to:
Code:
  'Get our ContentControl's index # in the table
  j = ActiveDocument.Range(.Range.Tables(1).Range.Start, .Range.Cells(1).Range.Start).ContentControls.Count
  j = j + ActiveDocument.Range(.Range.Cells(1).Range.Start, .Range.End).ContentControls.Count

Thanks macropod for your kind assistance.

I tried but it prompts the user to add new row at one of my content controls in a nested table contained within the last row/cell instead of the last content control. I think I may need to show you how my table looks like in order for you to help me see where the issue lies but I'm afraid it will take up too much of your time.

I forgot to mention that I also have a nested table within the last row/cell containing 3 content controls so it complicates things. But meanwhile, I will stick to using the quick access toolbar to add new row first as I have not encountered any errors with that!

Thanks a lot macropod.
Reply With Quote
  #23  
Old 08-12-2022, 01:25 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,962
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

Quote:
Originally Posted by shanshan89 View Post
I tried but it prompts the user to add new row at one of my content controls in a nested table contained within the last row/cell instead of the last content control. ...

I forgot to mention that I also have a nested table within the last row/cell containing 3 content controls so it complicates things.
Indeed. It really doesn't help that you keep leaving out key issues regarding your document. Do you really need to have a nested table?
Quote:
Originally Posted by shanshan89 View Post
But meanwhile, I will stick to using the quick access toolbar to add new row first as I have not encountered any errors with that!
The obvious problem with that, though, is that you have no control over when the user inserts a new row - which may or may not be needed - plus the adding needs to be available to every PC on which the process is to be run. Good luck with that on Macs...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #24  
Old 08-12-2022, 01:52 AM
shanshan89 shanshan89 is offline Code to add new row in table Windows 10 Code to add new row in table Office 2019
Novice
 
Join Date: Jul 2022
Posts: 17
shanshan89 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Indeed. It really doesn't help that you keep leaving out key issues regarding your document. Do you really need to have a nested table?
Unfortunately, yes The format of the table is fixed and I can't change that. Yes, noted your advice.
Reply With Quote
  #25  
Old 02-16-2023, 03:09 PM
sf_adam2222 sf_adam2222 is offline Code to add new row in table Windows 10 Code to add new row in table Office 2019
Novice
 
Join Date: Feb 2023
Posts: 1
sf_adam2222 is on a distinguished road
Default

macropod or other experts on this thread willing to help a noob out... I've attached a sample document with the version of the macro I think I need, and several tables (It is not currently restricted to forms).

The middle table on the document has been bookmarked 'Tbl1', but I am unable to get the macro to fire, and I'm wondering what I might be missing. Thank you.
Attached Files
File Type: docm SampleDoc2.docm (47.1 KB, 0 views)
Reply With Quote
Reply



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 25 11-04-2021 02:07 PM
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 11:02 PM.


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