Way to identify last table in a document?

L

Lenny

In a nutshell:
1. Word 2003 protected form template w/form fields
2. Form is a table
3. Page 1 (table) replicates itself to create additional pages in the file.
(the entire table w/form fields has been created as an autotext entry)
4. With macro code, and custom toolbar, user clicks 'add sheet', code
unprotects the document, jumps to bottom of page, inserts page break then
inserts the autotext entry and reprotects the document with a password.
5. Works just fine..... now users want to be able to 'delete' the last page
in case they add too many.

I have identified specific tables (table 1, table 2, etc) (that was in a
fixed position relative to other pages in the document) in other vb code
projects but was wondering if there was a way to identify the 'current' table
the cursor is in or 'last' table?

To accomplish the deletion of the page, I ran a macro that starts with the
cursor in a specific cell in the protected table. The user then clicks on
the toolbar to 'delete sheet' and the macro unlocks the document, moves up to
the first cell in the table, highlights all cells in the table, hits 'table -
rows - delete', back space to bottom of prior page, relocks down with
password.

This works, though very inelegantly. You see the screen briefly go black
from the row highlighting and it then blinks (screen refresh). Is there a
way to identify this table since there is no way of knowing whether it is
table 2 or 20?

Assistance is always appreciated. Lenny
 
L

Lene Fredborg

The following code line will delete the (first) table in the selection. It is
sufficient that the insertion point be in the table, i.e. you do not need to
do anything to select the entire table:

Selection.Tables(1).Delete

The following code will delete the last table in the active document.
“Activedocument.tables.count†is used to find the index number of the last
table. It does not matter where in the document the selection is when running
the code:

With ActiveDocument
.Tables(.Tables.Count).Delete
End With

In general, you can use:
Application.ScreenUpdating = False
to prevent (most) updating of the screen while running some code. However,
it does not necessarily remove all flickering. You can insert:
Application.ScreenUpdating = True
Where you want the screen to start updating again and you can use
Application.ScreenRefresh
to force an update of the screen.

Hope this helps.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
M

macropod

Hi Lenny,

The following macro deletes the whole of the last page, including the hard page-break or section break if that's what you've used to
create it:
Sub DeleteLastPage()
Dim LastPage As Range
Set LastPage = ActiveDocument.GoTo(What:=wdGoToPage, Name:=ActiveDocument.Range.Information(wdNumberOfPagesInDocument))
Set LastPage = LastPage.GoTo(What:=wdGoToBookmark, Name:="\page")
LastPage.MoveStart Unit:=wdCharacter, Count:=-2
LastPage.Delete
Set LastPage = Nothing
End Sub

If you haven't used either a hard page-break or a section break to create the last page, change Count:=-2 to Count:=-1.
 

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