Consulting

Results 1 to 4 of 4

Thread: Solved: Only allow certain table rows to be deleted

  1. #1
    VBAX Regular TButhe's Avatar
    Joined
    Sep 2004
    Location
    Sioux Falls, SD
    Posts
    64
    Location

    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.)

    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.
    </IMG></IMG>
    Thanks,
    Tracy

  2. #2
    VBAX Regular TButhe's Avatar
    Joined
    Sep 2004
    Location
    Sioux Falls, SD
    Posts
    64
    Location
    Thanks for looking but I think I have it figured out. I'm marking this solved. Hope I didn't waste anyones time.
    Thanks,
    Tracy

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    If you figured it out, please post what you figured out.

    I can tell you right now that:[vba]ActiveDocument.Tables.Rows.Count[/vba]will fail as there is no index for the table. You need something like:[vba]ActiveDocument.Tables(2).Rows.Count[/vba]for example. This action the second table. Or:[vba]Selection.Tables(1).Rows.Count[/vba]which actions the table the Selection is in.

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

  4. #4
    VBAX Regular TButhe's Avatar
    Joined
    Sep 2004
    Location
    Sioux Falls, SD
    Posts
    64
    Location

    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.
    Thanks,
    Tracy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •