PDA

View Full Version : Solved: Only allow certain table rows to be deleted



TButhe
09-19-2006, 09:53 AM
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.

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

Any help is greatly appreciated!! Thanks. :bow:
</IMG></IMG>

TButhe
09-19-2006, 10:45 AM
Thanks for looking but I think I have it figured out. :bigdance2 I'm marking this solved. Hope I didn't waste anyones time.

fumei
09-19-2006, 04:13 PM
If you figured it out, please post what you figured out.

I can tell you right now that:ActiveDocument.Tables.Rows.Countwill fail as there is no index for the table. You need something like:ActiveDocument.Tables(2).Rows.Countfor example. This action the second table. Or:Selection.Tables(1).Rows.Countwhich actions the table the Selection is in.

In any case, tell what you did do. Thanks.

TButhe
10-16-2006, 12:23 PM
Here is the code I came up with.
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


Maybe it could be done better but this is working for me.