0

I have a long macro that jumps to different bookmarks and deletes columns from the selected tables. Here is an example from my macro:

If ActiveDocument.Bookmarks.Exists("ProposedOverallObj") = True Then
ActiveDocument.Bookmarks.Item("ProposedOverallObj").Select
Call ApproveProposedOverallObj
End If

and then the macro that that calls is:

Sub ApproveProposedOverallObj()

Selection.Cut
Selection.GoTo What:=wdGoToBookmark, Name:="Objectives"
With ActiveDocument.Bookmarks
    .DefaultSorting = wdSortByName
    .ShowHidden = False
End With
Selection.PasteAndFormat (wdPasteDefault)
Selection.Tables(1).Columns(5).Delete
Selection.Tables(1).Columns(4).Delete
Selection.Tables(1).Columns(3).Delete
Selection.Tables(1).Columns(2).SetWidth ColumnWidth:=600.5, RulerStyle:= _
    wdAdjustFirstColumn
End If
End Sub 

Sometimes these run ok, and sometimes they bug out and I get an error:

"Run-time error '5825': Object has been deleted.

Basically it deletes columns 5 and 3 and then bugs and says "I can't delete column 3 because it has been deleted" but...it hasn't. Column 3 is still very much there.

Vityata
  • 42,633
  • 8
  • 55
  • 100
Sylvie
  • 57
  • 3
  • 12
  • Does the table contain merged cells? What is being pasted? – Cindy Meister Feb 08 '19 at 11:40
  • Why and what are you pasting before attempting to delete columns? This could be interfering. Personally, I would consider giving tables an ID in the template rather than using bookmarks which can become detached if not applied and managed consistently. (An ID is intended for HTML/XML use, but this probably isn't a concern.) – Andy G Feb 08 '19 at 11:53

1 Answers1

0

Probably the error is in the Select - it is not very reliable in VBA in general - How to avoid using Select in Excel VBA

As a rule of a thumb, try to assign the object to a variable and refer to it, whenever possible:

Sub TestMe()

    Dim myTable As Table
    Set myTable = ThisDocument.Tables(1)
    MsgBox "First table has " & myTable.Columns.Count & " columns."

    If myTable.Columns.Count >= 5 Then
        With myTable
            .Columns(5).Delete
            .Columns(4).Delete
            .Columns(3).Delete
        End With
    Else
        MsgBox "Not enough columns!"
    End If

End Sub
Vityata
  • 42,633
  • 8
  • 55
  • 100
  • I tried your code as it looks like what I need. The table in question is the 5th table, so I just changed it to ThisDocument.Tables(5) But here's the weird thing - I got the message "Your table has 2 columns" but it definitely has 5 columns!!! Word is only registering that it has 2 columns for some reason but it's not true! – Sylvie Feb 08 '19 at 16:21
  • `ThisDocument.Tables(5)` refers to the fifth table indeed. And if it exists, it is probably not with five columns. Can you delete all the tables but the "fifth" one, then run the code like this - `ThisDocument.Tables(1)` and see what happens? – Vityata Feb 08 '19 at 16:59
  • @Vityata ThisDocument refers to the document or template the macro is being run from, which is not necessarily the ActiveDocument. I very much doubt the OP wants to modify a template, for example. – macropod Feb 09 '19 at 21:14
  • @Sylvie Change *Set myTable = **ThisDocument**.Tables(1)* to *Set myTable = **ActiveDocument**.Tables(5)* – macropod Feb 09 '19 at 21:15