Deleting a table based on contents of a bookmark

P

Peter

I'm working on a forty page Word 2000 form used to collect
information from faculty about their activities. The form
is emailed as an attachment, filled out on an individual's
PC, and printed. The data is collected in about 150 two-
column tables that have various numbers of rows from four
to sixteen. In the first row of every table, the first
column contains a numeric bookmark that is the sum of the
numeric booksmarks in the first column of the two-to-
sixteen rows following. This sum is the result of
a 'Calculation' in the Text Form Field Option box. The
second column in the first row of every table simply
contains text describing a particular faculty activity and
the points it can be awarded. The second row is always
blank to enhance readability.

In rows three through sixteen, the first column contains a
Number bookmark where the user enters a number that
assigns a point value for the text they have entered in
the Regular Text bookmark in the second column. The Number
bookmark is from the 'Number' option in the Text Form
Field Option box, and 'Calculate on exit' is checked.
The 'Regular Text' option with a length of 750 characters
takes care of the text entries in the second column.

The form described above works well. I have described it
in some detail as requested it the 'Tips for Posting to
Newsgroups'.

What I am trying to work out, and my reason for writing
this group, is as follows:

1. Since faculty will not have any activities to report in
many of the tables, we would like to delete all tables
that have a zero in the Calculated (sum) bookmark that
always appears in the first column of the first row, and
also delete the blank line that follows each table on the
page, before the form is printed.
2. Place a button on the form at the end of the tables
that the user can click to initiate the deletion process
prior to printing the form. This deletion of empty tables
will ultimately save thousands of pages of paper, and
result in much cleaner and more readable reports.
3. Sum all of the Calculated bookmarks and place the grand
total in a field at the end of the document.

I have looked through nearly all the word.vba.beginners,
word.tables, and word.vba.general subject lines, as well
as the text of many of the associated items, but so far
haven't found any topics that involve deleting tables
based on the contents of a bookmark, though I could easily
be overlooking something since I'm new to vba.

Based on my limited experience with dBase IV and COBOL, I
think the pseudo code for what I'm trying to do would look
something like this:

1. See if the 'Delete Blank Tables' button has been clicked
2. Unprotect the form (my assumption since must unprotect
to modify current version)
3. Increment a loop until first table is found or EOF
(there's a lot of other text in the document not in tables)
4. When a table is found, see if the first (the calculated
sum) bookmark = 0. (There are as many as 16 bookmarks in a
table, but they will all be 0 if the calculated (total)
bookmark = zero)
5. If first bookmark = 0, delete the table and the line
directly following the table; If first bookmark NOT = 0,
add its contents to the running total.
6. Look for the next table
7. When reach EOF, place the running total in the total
field.
8. Exit the deletion routine.
9. Protect the form again
10. Return to the form

I would certainly appreciate any assistance anyone can
give me, including directing me to material that would
help me understand how vba moves through a word document,
identifies various objects and performs operations on what
it encounters. I've purchased three vba books I've seen
mentioned on this list, but none of them have examples
that are of much help to a beginner.

Thank you,

Peter
 
D

Doug Robbins - Word MVP

Hi Peter,

If you put a macro button field on the form, that ran a macro containing the
following code, it would delete any table that had 0 in the first cell in
its first row and then add up the values in those cells of the remaining
tables and insert the sum in a formfield with the Bookmark name "GrandTotal"

Dim myrange As Range, i As Integer, GTotal As Long
ActiveDocument.Fields.Update
ActiveDocument.Unprotect
For i = ActiveDocument.Tables.Count To 1 Step -1
Set myrange = ActiveDocument.Tables(i).Cell(1, 1).Range
myrange.End = myrange.End - 1
If myrange = 0 Then
myrange.Tables(1).Delete
End If
Next i
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset
GTotal = 0
For i = 1 To ActiveDocument.Tables.Count
GTotal = GTotal + ActiveDocument.Tables(i).Cell(1,
1).Range.FormFields(1).Result
Next i
ActiveDocument.FormFields("GrandTotal").Result = GTotal

I haven't covered the empty paragrapy after a table. If the deletion of a
table results in there being two empty paragraphs between the remaining
tables, then the easiest way to rectify that will be to include in the macro
the commands to replace two paragraphs marks in succession ^p^p with a
single paragraph mark ^p. You can get the code for this by using the macro
recorder.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

JGM

Hi Peter,

I just had a thought looking at Doug,s code,
If you want, try this slight modification:

If myrange = 0 Then
Set myrange = myrange.Tables(1).Range
myrange.SetRange Start:=myrange.Start, _
End:=myrange.End + 1
myrange.Delete
Else
myrange.Tables(1).Rows(2).Delete
End If

This should delete the following paragraph mark and the second row...

HTH
Cheers!
 
G

Guest

Good evening Jean-Guy,

Thank for the useful addition to Doug's code. I appreciate
you taking the time to respond.

Peter
 

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