liono
07-14-2016, 01:32 AM
Good Morning
I am new to this site and am wanting some assistance in completing the code I am trying to put together. I have found various parts of the code in different places.
What I have is a Word (2016) form that is protected allowing the user to fill in the form. The form has 9 tables with various content controls.
I am wanting the vba to work only on the final table and to ask if the user wants to add another row. It currently asks in
The problems i am having:
When other check boxes are selected in the previous tables it runs through the rest of the check box controls to the end of the table and then asks if i would like another row.
When the form is unprotected it will copy the last row of table 9 but when the form is protected i get "runtime error 5991"
Please advise
VBA
Option Explicit
Dim bLastCell As Boolean
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
With Selection.Range
If .Information(wdWithInTable) Then
'To limit processing to a particular table, change:
' Selection.Tables(1).Range below, to:
' ActiveDocument.Tables(#).Range, where # is the table index.
With ActiveDocument.Tables(9).Range
If Selection.Cells(1).RowIndex = .Cells(.Cells.Count).RowIndex Then
If Selection.Cells(1).ColumnIndex = .Cells(.Cells.Count).ColumnIndex Then
bLastCell = True
End If
End If
End With
End If
End With
End Sub
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim CCtrl As ContentControl, Prot As Variant, Pwd As String
If bLastCell = True Then
If MsgBox("Add new row?", vbQuestion + vbYesNo) = vbYes Then
With ActiveDocument
Prot = .ProtectionType
If .ProtectionType <> wdNoProtection Then
Pwd = "" 'Insert password here
Prot = .ProtectionType
.Unprotect Password:=Pwd
End If
With Selection.Tables(1).Rows
With .Last.Range
.Copy
.Next.InsertBefore vbCr
.Next.Paste
End With
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
.Protect Type:=Prot, Password:=Pwd
End With
End If
bLastCell = False
End If
End Sub
Liono
I am new to this site and am wanting some assistance in completing the code I am trying to put together. I have found various parts of the code in different places.
What I have is a Word (2016) form that is protected allowing the user to fill in the form. The form has 9 tables with various content controls.
I am wanting the vba to work only on the final table and to ask if the user wants to add another row. It currently asks in
The problems i am having:
When other check boxes are selected in the previous tables it runs through the rest of the check box controls to the end of the table and then asks if i would like another row.
When the form is unprotected it will copy the last row of table 9 but when the form is protected i get "runtime error 5991"
Please advise
VBA
Option Explicit
Dim bLastCell As Boolean
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
With Selection.Range
If .Information(wdWithInTable) Then
'To limit processing to a particular table, change:
' Selection.Tables(1).Range below, to:
' ActiveDocument.Tables(#).Range, where # is the table index.
With ActiveDocument.Tables(9).Range
If Selection.Cells(1).RowIndex = .Cells(.Cells.Count).RowIndex Then
If Selection.Cells(1).ColumnIndex = .Cells(.Cells.Count).ColumnIndex Then
bLastCell = True
End If
End If
End With
End If
End With
End Sub
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim CCtrl As ContentControl, Prot As Variant, Pwd As String
If bLastCell = True Then
If MsgBox("Add new row?", vbQuestion + vbYesNo) = vbYes Then
With ActiveDocument
Prot = .ProtectionType
If .ProtectionType <> wdNoProtection Then
Pwd = "" 'Insert password here
Prot = .ProtectionType
.Unprotect Password:=Pwd
End If
With Selection.Tables(1).Rows
With .Last.Range
.Copy
.Next.InsertBefore vbCr
.Next.Paste
End With
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
.Protect Type:=Prot, Password:=Pwd
End With
End If
bLastCell = False
End If
End Sub
Liono