Deleting the last two tables on a word 2002 document

jolas
3StarLounger
Posts: 204
Joined: 02 Feb 2010, 23:58

Deleting the last two tables on a word 2002 document

Post by jolas »

Is there a easier way to delete the last two tables on multiple similarly formatted 4 page word 2002 documents. I think it is the 7th and 8th table. I have a couple of hundreds to manually do. Assistance please. Thanks.

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

Re: Deleting the last two tables on a word 2002 document

Post by StuartR »

Open the Visual Basic Editor (Alt F11)
Go to the immediate window (Control-G)
Enter the following command

Code: Select all

ActiveDocument.Tables(ActiveDocument.Tables.Count).Delete
this will delete the last table in the document
Then issue the same command again.

You could create a simple macro with this line repeated, and add it to a toolbar button, or write some VBA to loop around all the hundreds of documents, open each one, delete the tables, save the document and close the file again.
StuartR


User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Deleting the last two tables on a word 2002 document

Post by HansV »

This macro will delete the last two tables in the active document:

Code: Select all

Sub DeleteLastTables()
  Dim i As Long
  Dim n As Long
  n = ActiveDocument.Tables.Count
  If n > 1 Then
    For i = n To n - 1 Step -1
      ActiveDocument.Tables(i).Delete
    Next i
  End If
End Sub
If you want to delete tables in multiple documents, you could place those documents in a folder that contains no other Word documents and create a macro that processes all documents in the folder. Post back if you need that.
Best wishes,
Hans

jolas
3StarLounger
Posts: 204
Joined: 02 Feb 2010, 23:58

Re: Deleting the last two tables on a word 2002 document

Post by jolas »

Yes please I need that kind of help multiple files on a folder for processing. Thanks for the help.

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Deleting the last two tables on a word 2002 document

Post by HansV »

Try this, modifying the path as needed:

Code: Select all

Sub ProcessDocs()
  Dim strPath As String
  Dim strFile As String
  Dim doc As Document
  Dim i As Long
  Dim n As Long

  Application.ScreenUpdating = False
  strPath = "C:\Docs\" ' Path must end in backslash
  strFile = Dir(strPath & "*.doc")
  Do While strFile <> ""
    ' Open document
    Set doc = Documents.Open(strPath & strFile)
    ' Remove last two tables
    n = doc.Tables.Count
    If n > 1 Then
      For i = n To n - 1 Step -1
        doc.Tables(i).Delete
      Next i
    End If
    doc.Close SaveChanges:=True
    ' On to the next one
    strFile = Dir
  Loop
  Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

jolas
3StarLounger
Posts: 204
Joined: 02 Feb 2010, 23:58

Re: Deleting the last two tables on a word 2002 document

Post by jolas »

Thanks very much. I works great!
Have a really nice day!