Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-18-2016, 06:28 AM
tbrookes tbrookes is offline Add new row to protected form including content controls via Command Button Windows 7 64bit Add new row to protected form including content controls via Command Button Office 2010 64bit
Novice
Add new row to protected form including content controls via Command Button
 
Join Date: Jun 2016
Posts: 3
tbrookes is on a distinguished road
Default Add new row to protected form including content controls via Command Button

Hello
I'm very new to this - I have a number of tables in my protected form all with content controls including date pickers.

What I want to do is have a button below each table which says "Add Row". When a user clicks it, a macro will run that adds a new row to the table, complete with the content controls from the row above (not the data they've entered, just the fields). I've worked out how to create the button and have the below code to add a new row but I don't know how to change it so that it copies the content controls as well.


ActiveDocument.Tables(9).Select
Selection.InsertRowsBelow 1



I have looked in other threads but couldn't find anything that would work, so would really appreciate any advice.
Thanks in advance
Reply With Quote
  #2  
Old 06-18-2016, 08:56 AM
macropod's Avatar
macropod macropod is offline Add new row to protected form including content controls via Command Button Windows 7 64bit Add new row to protected form including content controls via Command Button Office 2010 32bit
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

There have been multiple threads discussing this issue, though all assume only a single table is subject to the addition of new rows. The following ContentControlOnExit macro allows you to nominate any set of tables you consider appropriate for the conditional addition of new rows. No command buttons are needed - the macro automatically prompts to add a new row when you exit the last content control in any of the nominated tables. As coded, this occurs in tables 1, 3, 5, & 6; you can change the series to any other set of tables.

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
'Exit if we're not in a table.
If CCtrl.Range.Information(wdWithInTable) = False Then Exit Sub
With CCtrl
  'Check that the Content Control is within one of our designated tables.
  i = ActiveDocument.Range(0, .Range.End).Tables.Count
  Select Case i
    Case 1, 3, 5, 6 'The series of tables to process; other tables are excluded
      'Get the # 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
      '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
        ' Re-protect the document, if applicable
        .Protect Type:=Prot, Password:=Pwd
      End With
    Case Else
  End Select
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-19-2016, 05:42 AM
tbrookes tbrookes is offline Add new row to protected form including content controls via Command Button Windows 7 64bit Add new row to protected form including content controls via Command Button Office 2010 64bit
Novice
Add new row to protected form including content controls via Command Button
 
Join Date: Jun 2016
Posts: 3
tbrookes is on a distinguished road
Default

Thankyou Paul for the code you provided - I am very new to this so apologies for any "stupid" questions. I copied the code you provided and changed the tables line:
Case 1, 3, 5, 6 'The series of tables to process; other tables are excluded

to this:

Case 2, 4, 9, 10 'The series of tables to process; other tables are excluded

When I tabbed out of the last field in Table 2; I got an error that said "Compile Error: Method or data member not found". It highlighted this row of code:
If .Type = wdContentControlCheckBox Then .Checked = False
but specifically the word " .Checked"

I have no idea, what I've done wrong - can you help please?
Reply With Quote
  #4  
Old 06-19-2016, 03:45 PM
macropod's Avatar
macropod macropod is offline Add new row to protected form including content controls via Command Button Windows 7 64bit Add new row to protected form including content controls via Command Button Office 2010 32bit
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

That suggests you're using a document that doesn't have checkbox content controls, because it's in Word 2007 compatibility mode. In that case, you can delete that line of code.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 06-19-2016, 08:58 PM
tbrookes tbrookes is offline Add new row to protected form including content controls via Command Button Windows 7 64bit Add new row to protected form including content controls via Command Button Office 2010 64bit
Novice
Add new row to protected form including content controls via Command Button
 
Join Date: Jun 2016
Posts: 3
tbrookes is on a distinguished road
Default

Hi Paul,

When I click out of the last control in the table, I now get a "Run-time error'5941': The requested member of the collection does not exist. When I click Debug, I now have an error with this line of code:

With Selection.Table (1).Rows

The last control is a date picker if that's relevant?

Any ideas please?
Reply With Quote
  #6  
Old 06-20-2016, 02:48 AM
macropod's Avatar
macropod macropod is offline Add new row to protected form including content controls via Command Button Windows 7 64bit Add new row to protected form including content controls via Command Button Office 2010 32bit
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

I'm unable to generate that error. That said, you might try changing:
Selection
to:
CCtrl.Range
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Form with content controls - expands but at the bottom of the form louiseword Word 3 05-27-2016 12:47 AM
Creating and using a template (online form with content controls) Esme Word 2 10-17-2014 07:18 AM
Add new row to protected form including content controls via Command Button Updating an old form with Content Controls Something Anon Word 4 03-26-2014 03:53 PM
Add new row to protected form including content controls via Command Button Command Button will not work when document is protected brockjensen Word 1 11-02-2012 06:59 PM
Content Controls Form Programming beachdog Word VBA 4 09-20-2011 10:26 AM

Other Forums: Access Forums

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