Deleting the last table in a documnet

M

Mikel

I am trying to automate a process that deals with tables.
I need to delete the last table in my document but the
table number can change each time the process is run.
I have used the following to tell me how many tables are
in my documnet.
MsgBox ActiveDocument.Tables.Count & " table"
How do I put the results into the following
Selection.Tables().Delete

Thank you for you time and help.

Mikel
 
J

Jay Freedman

Hi Mikel

It may be more understandable if you create a variable to hold the
count, like this:

Dim nTables As Integer
nTables = ActiveDocument.Tables.Count
ActiveDocument.Tables(nTables).Delete

You can collapse that into one line like this:

ActiveDocument.Tables(ActiveDocument.Tables.Count).Delete

In either case, the Selection has nothing to do with this operation.
In general you almost *never* have to select something in order to
work on it with VBA, and there are definite advantages to not
selecting. It's faster (because Word doesn't have to scroll and redraw
the screen), it's easier (you don't have to store the original
Selection and go back to it at the end of the macro), and it looks
better (the screen doesn't flicker).
 
M

Mikel

Thank you, that worked great!
One more question with tables.
I have an old macro written in wordbasic and I am trying
to convert it to VBA for Word 97/2000. What the present
macro does is find for a style. Only one table in the
document has this style but the table # can vary. After
the style is found it puts the insertion point in the
table then selects it and deletes it. Can VBA find for
the style and then tell you which table number has this
style? If not can you provide guidance on other possible
ways to approach this?
Thank you for your time and wonderful help,
Mikel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top