Strange error message

User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

Strange error message

Post by Peter Kinross »

The code below reformats a table. If the cursor is in a table it works just fine (thanks Hans). However if the cursor is not in a table, it doesn't even get to the 'in table' checking. I get this error message from the error handler:
"The requested member of the collection doesn't exist".
Resuming puts the cursor on the line "Selection.Tables(1).Select" at the top of the code.

Code: Select all

Sub ReFormatTable()
Dim c As Cell
On Error GoTo Err_ReFormatTable
Application.ScreenUpdating = False
Selection.Tables(1).Select
If Selection.Information(wdWithInTable) <> True Then
  MsgBox "The cursor is not in a table", vbOKOnly
  GoTo Exit_ReFormatTable
End If
Selection.Font.Name = "Calibri"
Selection.Font.Size = 11
With Selection.ParagraphFormat
    .SpaceBefore = 0
    .SpaceBeforeAuto = False
    .SpaceAfter = 0
    .SpaceAfterAuto = False
    .LineSpacingRule = wdLineSpaceSingle
    .LineUnitBefore = 0
    .LineUnitAfter = 0
End With
Selection.Rows.HeightRule = wdRowHeightExactly
Selection.Rows.Height = CentimetersToPoints(0.5)
Selection.Tables(1).Select
For Each c In Selection.Cells
    With c
        Cntr = Cntr + 1
        If Cntr = 1 Then
            .VerticalAlignment = wdAlignVerticalTop
        End If
        .TopPadding = CentimetersToPoints(0.05)
        .BottomPadding = CentimetersToPoints(0.05)
        .LeftPadding = CentimetersToPoints(0.05)
        .RightPadding = CentimetersToPoints(0.05)
        .WordWrap = True
        .FitText = False
    End With
Next c
With Selection.Tables(1).Rows(1)
    .Alignment = wdAlignRowLeft
    .Height = CentimetersToPoints(0.7)
    .Cells.VerticalAlignment = wdCellAlignVerticalBottom
End With   'Selection.Tables(1).Rows(1)
Exit_ReFormatTable:
Application.ScreenUpdating = True
Exit Sub

Err_ReFormatTable:
MsgBox Err.Description
GoTo Exit_ReFormatTable
Resume
End Sub
Avagr8day, regards, Peter

User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

Re: Strange error message

Post by Peter Kinross »

Ha ha. I moved the 'in table' check to the top and it exits the sub if the cursor is not in a table.
But, it then gives the dialog "Code execution has been interrupted" with 'Continue', 'End', 'Debug', 'help' buttons.
How can I stop this?
Avagr8day, regards, Peter

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Strange error message

Post by StuartR »

Can you step through the code and identify the line that causes that error?

I would put the check for cursor being in a table right at the top, before the Application.Screenupdating line, and include Exit Sub if the selection isn't in a table.
StuartR


User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

Re: Strange error message

Post by Peter Kinross »

Ha ha again. It is the vba bug of a deleted break point still persisting.
Pressing Debug and then Ctrl+Break twice did the trick. (Unless it happens again)
Avagr8day, regards, Peter

User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

Re: Strange error message

Post by Peter Kinross »

thanks Stuart.
The line it stopped on was - "GoTo Exit_ReFormatTable". A method I prefer to Exit Sub.
But it is all currently working. (Note the use of the word currently.)
Avagr8day, regards, Peter