Deleting the last two tables on a word 2002 document
-
- 3StarLounger
- Posts: 204
- Joined: 02 Feb 2010, 23:58
Deleting the last two tables on a word 2002 document
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.
-
- Administrator
- Posts: 12629
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: Deleting the last two tables on a word 2002 document
Open the Visual Basic Editor (Alt F11)
Go to the immediate window (Control-G)
Enter the following command
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.
Go to the immediate window (Control-G)
Enter the following command
Code: Select all
ActiveDocument.Tables(ActiveDocument.Tables.Count).Delete
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
-
- Administrator
- Posts: 78631
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Deleting the last two tables on a word 2002 document
This macro will delete the last two tables in the active document:
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.
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
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 204
- Joined: 02 Feb 2010, 23:58
Re: Deleting the last two tables on a word 2002 document
Yes please I need that kind of help multiple files on a folder for processing. Thanks for the help.
-
- Administrator
- Posts: 78631
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Deleting the last two tables on a word 2002 document
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
Hans
-
- 3StarLounger
- Posts: 204
- Joined: 02 Feb 2010, 23:58
Re: Deleting the last two tables on a word 2002 document
Thanks very much. I works great!
Have a really nice day!
Have a really nice day!