View Single Post
 
Old 06-28-2021, 08:38 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Ideally a form with content controls should be protected as readonly with editors added to the content controls, but you can protect it for forms. The code for both is included


Code:
Option Explicit
Const sPassword As String = "password"
Private oCC As ContentControl
Private oTable As Table
Private oRng As Range

Private Sub CommandButton1_Click()

    If Not ActiveDocument.ProtectionType = wdNoProtection Then
        ActiveDocument.Unprotect sPassword
    End If
    Set oTable = ActiveDocument.Tables(3)
    For Each oCC In oTable.Range.ContentControls
        oCC.LockContentControl = False
    Next oCC
    oTable.Rows.Last.Range.Copy
    Set oRng = oTable.Range
    oRng.Collapse 0
    oRng.Paste
    oRng.ContentControls(1).DropdownListEntries.Item(1).Select
    For Each oCC In ActiveDocument.Range.ContentControls
        oCC.LockContentControl = True
    '    oCC.Range.Editors.Add (wdEditorEveryone)
    Next oCC
    'ActiveDocument.Protect wdAllowOnlyReading, , sPassword
    ActiveDocument.Protect wdAllowOnlyFormFields, , sPassword
    Set oCC = Nothing
    Set oTable = Nothing
    Set oRng = Nothing
End Sub

Private Sub CommandButton2_Click()
    If Not ActiveDocument.ProtectionType = wdNoProtection Then
        ActiveDocument.Unprotect sPassword
    End If
    Set oTable = ActiveDocument.Tables(3)
    For Each oCC In oTable.Range.ContentControls
        oCC.LockContentControl = False
    Next oCC
    oTable.Rows.Last.Delete
    For Each oCC In ActiveDocument.Range.ContentControls
        oCC.LockContentControl = True
        'oCC.Range.Editors.Add (wdEditorEveryone)
    Next oCC
    ActiveDocument.Protect wdAllowOnlyFormFields, , sPassword
    'ActiveDocument.Protect wdAllowOnlyReading, , sPassword
    Set oCC = Nothing
    Set oTable = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote