Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-22-2015, 04:25 PM
EAGLE SEU EAGLE SEU is offline table removal syntax Windows 7 64bit table removal syntax Office 2007
Novice
table removal syntax
 
Join Date: Jan 2015
Posts: 3
EAGLE SEU is on a distinguished road
Question table removal syntax

Hello everyone
I have a lot of tables in word document
and I want to delete one table and leave one .. until the end of document
I found in internet this code - to remove all tables -
but still have a syntax error ...
--------------------
Sub Removetables ()
Dim oTable As Table
Each oTable In ActiveDocument.Tables
oTable.Delete
Next oTable
End Sub
-----------------


Can someone help me to write code to delete one table and leave one ...

Thanks for everyone
Reply With Quote
  #2  
Old 01-22-2015, 08:50 PM
Guessed's Avatar
Guessed Guessed is offline table removal syntax Windows 7 32bit table removal syntax Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You need to work backwards since the act of deleting a table reduces the number of tables in the document.
Sub RemoveTables2()
Dim i As Integer
For i = ActiveDocument.Tables.Count To 2 Step -2
ActiveDocument.Tables(i).Delete
Next i
End Sub

PS. The code you posted is erroring because you are missing the word 'For' in the front of the 'Each ...' line
Reply With Quote
  #3  
Old 01-22-2015, 11:32 PM
gmayor's Avatar
gmayor gmayor is offline table removal syntax Windows 7 64bit table removal syntax Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Quote:
For i = ActiveDocument.Tables.Count To 2 Step -2
Unless you want to retain every other table you need Step -1
This will leave the first table.

You could also modify the original code method to introduce a counter e.g.

Code:
Sub Removetables()
Dim oTable As Table
Dim i As Integer
    i = 1
    For Each oTable In ActiveDocument.Tables
        If i > 1 Then oTable.Delete
        i = i + 1
    Next oTable
End Sub
Both methods are likely to leave empty paragraphs.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #4  
Old 01-23-2015, 03:11 AM
EAGLE SEU EAGLE SEU is offline table removal syntax Windows 7 64bit table removal syntax Office 2007
Novice
table removal syntax
 
Join Date: Jan 2015
Posts: 3
EAGLE SEU is on a distinguished road
Thumbs up Best Answer

Quote:
Originally Posted by Guessed View Post
You need to work backwards since the act of deleting a table reduces the number of tables in the document.
Sub RemoveTables2()
Dim i As Integer
For i = ActiveDocument.Tables.Count To 2 Step -2
ActiveDocument.Tables(i).Delete
Next i
End Sub

PS. The code you posted is erroring because you are missing the word 'For' in the front of the 'Each ...' line
Thank you My dear .. this code works well , Also thank you for corrected the syntax error of my code ..


Quote:
Originally Posted by gmayor View Post
Unless you want to retain every other table you need Step -1
This will leave the first table.

You could also modify the original code method to introduce a counter e.g.

Code:
Sub Removetables()
Dim oTable As Table
Dim i As Integer
    i = 1
    For Each oTable In ActiveDocument.Tables
        If i > 1 Then oTable.Delete
        i = i + 1
    Next oTable
End Sub
Both methods are likely to leave empty paragraphs.
Thank you my dear .. your code works but it's deletes all tables accept the first one


Thanks for all once again
May God reward you
Reply With Quote
  #5  
Old 01-23-2015, 03:49 AM
gmayor's Avatar
gmayor gmayor is offline table removal syntax Windows 7 64bit table removal syntax Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Quote:
your code works but it's deletes all tables accept the first one
Indeed it does, which is what I took your request to mean. However if you want every other table removed then using the similar syntax:

Code:
Sub Removetables()
Dim oTable As Table
Dim i As Integer
    i = 1
    For Each oTable In ActiveDocument.Tables
        If i Mod 2 = 1 Then oTable.Delete
        i = i + 1
    Next oTable
End Sub
This will remove table 1, table 3, table 5 etc
If you want to remove table 2, table 4, table 6 etc then change
Code:
If i Mod 2 = 1 Then oTable.Delete
to
Code:
If i Mod 2 = 0 Then oTable.Delete
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #6  
Old 01-23-2015, 10:57 AM
EAGLE SEU EAGLE SEU is offline table removal syntax Windows 7 64bit table removal syntax Office 2007
Novice
table removal syntax
 
Join Date: Jan 2015
Posts: 3
EAGLE SEU is on a distinguished road
Thumbs up Fantastic Answer

Quote:
Originally Posted by gmayor View Post
Indeed it does, which is what I took your request to mean. However if you want every other table removed then using the similar syntax:

Aha it's okay , anyway , Thank you for trying to lend a hand

Quote:
Originally Posted by gmayor View Post
Code:
Sub Removetables()
Dim oTable As Table
Dim i As Integer
    i = 1
    For Each oTable In ActiveDocument.Tables
        If i Mod 2 = 1 Then oTable.Delete
        i = i + 1
    Next oTable
End Sub
This will remove table 1, table 3, table 5 etc
If you want to remove table 2, table 4, table 6 etc then change
Quote:
Originally Posted by gmayor View Post
Code:
If i Mod 2 = 1 Then oTable.Delete
to
Code:
If i Mod 2 = 0 Then oTable.Delete
Yes , this one more helpful then delete odd tables , and thank you for explain both of methods which improve my skills

I Appreciating your consideration and cooperation
Reply With Quote
Reply

Tags
help please, tables, word vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
table removal syntax Strange Characters Removal OceansBlue Word 2 04-03-2013 10:01 AM
table removal syntax Please help with header and footer removal pwangdel Word 3 11-03-2011 06:10 AM
table removal syntax Field removal from template Phelony Word 1 10-18-2011 03:28 AM
background graphic removal taffyevo PowerPoint 0 08-16-2011 06:36 AM
table removal syntax Office 2007 removal and re-installation of previous version Buckeyegator Office 1 03-25-2010 09:15 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:23 AM.


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