View Single Post
 
Old 12-19-2017, 12:30 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
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 of
Default

You need to check your shortcuts as they don't work. However to delete from a protected document, you must remove the protection first, just as in your macro to add rows, and reapply it afterwards e.g.

Code:
Private Sub Delete_Click()
Dim vProtectionType As Variant, strPassword As String

    vProtectionType = ActiveDocument.ProtectionType

    If vProtectionType <> wdNoProtection Then
        strPassword = ""    'Insert password here
        ActiveDocument.Unprotect Password:=strPassword
    End If
    Application.ScreenUpdating = False
    Dim Tbl As Table, cel As Cell, r As Long, x As Long
    Dim bDel As Boolean, CCtrl As ContentControl
    With ActiveDocument
        For Each Tbl In .Tables
            For r = Tbl.Rows.Count To 1 Step -1
                With Tbl.Rows(r)
                    If Len(.Range.Text) = .Cells.Count * 2 + 2 Then
                        .Delete
                    Else
                        bDel = True
                        For Each cel In .Cells
                            If Len(cel.Range.Text) > 2 Then
                                If .Range.ContentControls.Count = 0 Then
                                    bDel = False: Exit For
                                Else
                                    For Each CCtrl In .Range.ContentControls
                                        If CCtrl.ShowingPlaceholderText = False Then
                                            bDel = False: Exit For
                                        End If
                                        x = x + Len(CCtrl.Range.Text)
                                    Next
                                    If x + 2 <= Len(cel.Range.Text) Then
                                        bDel = False: Exit For
                                    End If
                                End If
                            End If
                        Next cel
                        If bDel = True Then Tbl.Rows(r).Delete
                    End If
                End With
            Next r
        Next Tbl
    End With
    If vProtectionType > -1 Then
        ActiveDocument.Protect Type:=vProtectionType, Password:=strPassword
    End If
    Set cel = Nothing: Set Tbl = Nothing
    Application.ScreenUpdating = True
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