Hi all,
I have a document populated with Content Controls in a table. I created a macro to add a row at the bottom of the table and populate the fields with additional content controls. Before adding the row the macro unprotects the document...
ActiveDocument.Unprotect
This works perfectly.
The last line of the macro is supposed to protect the document again...
ActiveDocument.Protect wdAllowOnlyFormFields, True
However, it doesn't work. I tried this format as well because I saw it in some other code...
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
But it still doesn't work.
Any suggestions on what I might be doing wrong?
EDIT:
Here is the full code (please excuse the woefully inefficient coding, I am still a newbie in VBA)...
Code:
Sub AddTableRow()
'
' AddTableRow Macro
'
'
Dim tRows As Integer
Dim objCC As ContentControl
Call UnProtectForm
Selection.Tables(1).Rows.Add
Selection.EndKey Unit:=wdColumn
'Selection.Cells(1).Next.Select
Selection.StartOf Unit:=wdRow
tRows = Selection.Information(wdMaximumNumberOfRows)
tRows = tRows - 8
Selection.TypeText tRows
Selection.Cells(1).Next.Select
Set objCC = ActiveDocument.ContentControls.Add(wdContentControlRichText)
objCC.SetPlaceholderText Text:=" "
objCC.Appearance = wdContentControlHidden
Selection.Cells(1).Next.Select
Set objCC = ActiveDocument.ContentControls.Add(wdContentControlPlainText)
objCC.SetPlaceholderText Text:=" "
objCC.Appearance = wdContentControlHidden
Selection.Cells(1).Next.Select
Set objCC = ActiveDocument.ContentControls.Add(wdContentControlPlainText)
objCC.SetPlaceholderText Text:=" "
objCC.Appearance = wdContentControlHidden
Selection.Cells(1).Next.Select
Set objCC = ActiveDocument.ContentControls.Add(wdContentControlPlainText)
objCC.SetPlaceholderText Text:=" "
objCC.Appearance = wdContentControlHidden
Selection.Cells(1).Next.Select
Set objCC = ActiveDocument.ContentControls.Add(wdContentControlPlainText)
objCC.SetPlaceholderText Text:="PASS"
objCC.Appearance = wdContentControlHidden
Selection.StartOf Unit:=wdRow
ThisDocument.NextCell
Call ProtectForm
End Sub
Sub UnProtectForm()
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If
End Sub
Sub ProtectForm()
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
End Sub
The macro "AddTableRow" will be activated when the user is in a content control, if this makes a difference. I'm adding that info because the "ProtectForm()" works fine if I am in the VBA editor and I click
Run - Run Sub/UserForm while the cursor is located inside the "ProtectForm()" code.