Solved: Only allow certain table rows to be deleted
I am trying to create macro that will look at the row that the cursor is in and if that row is less than or equal to 13 then I want it to display a message box telling the user they cannot delete that row. I can do this in excel no prob but can't quite get it in Word. I got the rest of the code off this site. (Thanks, everyone.) :clap: :clap:
Here is what I have so far.
[vba]Sub CustomDeleteRow()
If Selection.Information(wdWithInTable) Then
With ActiveDocument
If .ProtectionType <> wdNoProtection Then .Unprotect Password:="password"
'this is where i run into problems - at least i think this is the spot
If ActiveDocument.Tables.Rows.Count <= 13 Then _
MsgBox ("You cannot delete this row."), vbOKOnly
Dim myCheck As VbMsgBoxResult
myCheck = MsgBox("Are you sure you want to delete this row?", vbYesNo)
If myCheck = vbYes Then
Selection.Rows(1).Delete
Else
Exit Sub
End If
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="password"
End With
Else
Exit Sub
End If
End Sub[/vba]
Any help is greatly appreciated!! Thanks. :bow:
</IMG></IMG>
Sorry it took so long to get back to this post
Here is the code I came up with.
[vba]Sub CustomDeleteRow()
Dim myCheck As VbMsgBoxResult
ActiveDocument.Unprotect Password:="password"
If Selection.Cells(1).RowIndex <= 8 Then
MsgBox ("You cannot delete this row."), vbOKOnly
Else
myCheck = MsgBox("Are you sure you want to delete this row?", vbYesNo)
If myCheck = vbYes Then
Selection.Rows(1).Delete
Else
Exit Sub
End If
End If
ActiveDocument.Protect wdAllowOnlyFormFields, Password:="password"
End Sub
[/vba]
Maybe it could be done better but this is working for me.