Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #10  
Old 04-04-2018, 08:32 AM
slaycock slaycock is offline Testing for 'Object has been deleted' error Windows 7 64bit Testing for 'Object has been deleted' error Office 2016
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

'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
Applying the same approach to you code gives

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...
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
set row object variable error CLoos Excel Programming 6 03-10-2017 04:48 PM
Testing for 'Object has been deleted' error Run-time error '424': Object required zlodeh Excel Programming 1 02-24-2016 01:58 AM
Testing for 'Object has been deleted' error Run Time Error 424 - Object Required Doug Needham Excel Programming 4 01-12-2015 10:54 PM
Testing for 'Object has been deleted' error Error Message: Could not load an object... 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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:48 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft