![]() |
#10
|
|||
|
|||
![]()
'oTbl is nothing' - you are comparing objects not values.
The 'is' operator is not the same as the '=' operator'. The 'is' operator compares the references to objects, not the content of the objects. Deleting all rows deletes the word object and leaves oTbl in an indeterminate state. Thus you can refer to the VBA object but trying to access its information gives an error because there is no object. If the object has been deleted then oTbl will be nothing. Hence you will be comparing the reference to nothing with the reference to nothing which is always true. One of the little joys of working with objects. The simplest way to deal with this situation is by abstracting the object range (in fact ranges are by far the best way to work in VBA for Word). The following code works without needing an on error statement assuming there is at least one uniform table in the active document. Code:
Sub Macro2() Dim oTbl As Word.Table Dim oTbl_range As Word.Range Set oTbl = ActiveDocument.Tables(1) Set oTbl_range = oTbl.Range Do Until oTbl_range.Tables.Count = 0 Debug.Print oTbl.Range.Rows.Count oTbl.Rows(1).Delete Loop End Sub Code:
Dim oTbl_range As Word.Range Set oTbl_range = oTbl.Range ' Remove empty rows With oTble_range.Tables(1) RowCount = .Rows.Count columnCount = .Columns.Count For Row = RowCount To 1 Step -1 Delete = True For Column = 1 To columnCount If Not (.Cell(Row, Column).Range.Text = Chr(13) & Chr(7)) Then Delete = False Exit For End If Next Column If (Delete) Then ' Remove row Call .Rows(Row).Delete End If Next Row End With ' NOTE: Need to check to see if table still exists here (If all rows were deleted) If oTbl_range.Tables.Count > 0 Then ' by abstracting the table range you can subsequently test for the presence of a table in the range ' Do other processing steps to table... |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
set row object variable error | CLoos | Excel Programming | 6 | 03-10-2017 04:48 PM |
![]() |
zlodeh | Excel Programming | 1 | 02-24-2016 01:58 AM |
![]() |
Doug Needham | Excel Programming | 4 | 01-12-2015 10:54 PM |
![]() |
simstem | Word | 1 | 10-06-2012 08:44 PM |
XML parsing & Object variable not set (Error 91) | tinfanide | Excel Programming | 0 | 12-29-2011 08:43 AM |