P
Peter
I appreciate the help received from Doug and Jean-Guy in
response to my November 11 post concerning deleting tables
based on the contents of a particular bookmark in a forty-
page Word 2000 form used to collect information from
faculty about their activities.
The code works perfectly, and is included at the end of
this post for reference.
Four questions about our project have come up that I am
unable to answer, no doubt because this is my first vba
project. I have looked through the MVPS Word FAQs, and the
subject lines in the vba.beginners and vba.general lists,
but haven't found solutions yet, though I may have
overlooked a posting that would be helpful.
1. How do I include my vba code as part of a Word 2000
form emailed as an attachment?
I tried to email a test version of the form to a colleague
and myself so we could test it, but found that no vba code
was included along with the attachment when I opened the
form and then the VB Editor (from Tools->Macro->VB
Editor). I believe I correctly saved the code along with
the document (not in Normal) before sending the email with
attachment, but there was no sign of any vba code.
I emailed myself the code separately, and pasted it into
the code window of the VB Editor, and then it works fine.
But this is too complicated for the faculty to do.
I've read 'Distributing macros to other users' and 'What
do I do with macros sent to me by other newsgroup readers
to help me out?' but neither really addresses how one
sends a Word form as an attachment when the form includes
vba. The fact that I haven't found any FAQ references to
what must be a common activity, leads me to conclude that
I'm missing something really obvious!
2. How do I make a macro button run the vba code
associated with my form?
The code works fine when I run it from within the VB
Editor using the 'Step Into' button where I can observe
its operation, and also runs fine from the 'Run' button,
but only when the cursor is at the first line of the Sub()
in the code window.
I'm not clear about how to attach my macro button to the
code it is supposed to activate: when I click on the macro
button, the cursor simply moves into the first field in my
form, if I double click, the cursor moves into the second
field in the form, but in either case does not run the Sub
code as I would have expected. I do of course have the
name of the vba routine in the macrobutton. The macro
button is located at the end of section 3 (please see
following question).
3. How do I run the 'delete empty tables' code on only
sections 2 and 3 of the three-part form?
The form is in three sections, with a section break
following each of the first two sections with each new
section starting on the next page (Insert->Break-
the related numeric total (from Cell 1,1 of any remaining
tables) at the end of section two or three, rather than
the one GrandTotal that is generated now.
4. In the tables that remain after running the 'delete
empty tables' code, how do I delete rows that haven't been
used and therefore have no text in their text bookmarks,
along with completely empty 'spacer rows' (for
readability) located between rows containing text?
After looking at the results from running the code as it
is now, it occurred to us that a cleaner document would
result, if within a table that _contains_ text in its
first text bookmark (which is always row 3, column 2), it
would be possible to delete any rows that follow but that
may not contain any text. These rows may be completely
empty 'spacer rows' inserted for readability between rows
with text bookmarks, or simply another row like row 3 that
contains a text bookmark but no text in the bookmark.
Tables may contain between 2 and 16 rows having text
bookmarks.
I think the code to do this would need to:
a. Look for the last row in a table that has text in
column 2 (not just the existence of a text field, but a
text field with a Len of > 5 which seems to be the default
length of my text bookmarks for reasons I don't understand
at this point)
b. Delete rows (if any) in the table that follow this last
row.
Thank you very much for any assistance you are able to
provide with these four questions.
Peter
The current version of the code provided by Doug with
additions by Jean-Guy follows:
Sub FSR()
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(3, 2).Range
myrange.End = myrange.End - 1
' Check row 3 column 2 to see if empty-default empty
length is 5
If Len(myrange) = 5 Then
Set myrange = myrange.Tables(1).Range
myrange.SetRange Start:=myrange.Start, _
End:=myrange.End + 1
myrange.Delete
End If
Next i
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset
ScholTotal = 0
For i = 1 To ActiveDocument.Tables.Count
ScholTotal = ScholTotal + ActiveDocument.Tables(i).Cell
(1, 1).Range.FormFields(1).Result
Next i
ActiveDocument.FormFields("ScholTotal").Result =
ScholTotal
End Sub
response to my November 11 post concerning deleting tables
based on the contents of a particular bookmark in a forty-
page Word 2000 form used to collect information from
faculty about their activities.
The code works perfectly, and is included at the end of
this post for reference.
Four questions about our project have come up that I am
unable to answer, no doubt because this is my first vba
project. I have looked through the MVPS Word FAQs, and the
subject lines in the vba.beginners and vba.general lists,
but haven't found solutions yet, though I may have
overlooked a posting that would be helpful.
1. How do I include my vba code as part of a Word 2000
form emailed as an attachment?
I tried to email a test version of the form to a colleague
and myself so we could test it, but found that no vba code
was included along with the attachment when I opened the
form and then the VB Editor (from Tools->Macro->VB
Editor). I believe I correctly saved the code along with
the document (not in Normal) before sending the email with
attachment, but there was no sign of any vba code.
I emailed myself the code separately, and pasted it into
the code window of the VB Editor, and then it works fine.
But this is too complicated for the faculty to do.
I've read 'Distributing macros to other users' and 'What
do I do with macros sent to me by other newsgroup readers
to help me out?' but neither really addresses how one
sends a Word form as an attachment when the form includes
vba. The fact that I haven't found any FAQ references to
what must be a common activity, leads me to conclude that
I'm missing something really obvious!
2. How do I make a macro button run the vba code
associated with my form?
The code works fine when I run it from within the VB
Editor using the 'Step Into' button where I can observe
its operation, and also runs fine from the 'Run' button,
but only when the cursor is at the first line of the Sub()
in the code window.
I'm not clear about how to attach my macro button to the
code it is supposed to activate: when I click on the macro
button, the cursor simply moves into the first field in my
form, if I double click, the cursor moves into the second
field in the form, but in either case does not run the Sub
code as I would have expected. I do of course have the
name of the vba routine in the macrobutton. The macro
button is located at the end of section 3 (please see
following question).
3. How do I run the 'delete empty tables' code on only
sections 2 and 3 of the three-part form?
The form is in three sections, with a section break
following each of the first two sections with each new
section starting on the next page (Insert->Break-
table' code only on sections two and three, and includePageBreak->Next Page). We want to run the 'delete empty
the related numeric total (from Cell 1,1 of any remaining
tables) at the end of section two or three, rather than
the one GrandTotal that is generated now.
4. In the tables that remain after running the 'delete
empty tables' code, how do I delete rows that haven't been
used and therefore have no text in their text bookmarks,
along with completely empty 'spacer rows' (for
readability) located between rows containing text?
After looking at the results from running the code as it
is now, it occurred to us that a cleaner document would
result, if within a table that _contains_ text in its
first text bookmark (which is always row 3, column 2), it
would be possible to delete any rows that follow but that
may not contain any text. These rows may be completely
empty 'spacer rows' inserted for readability between rows
with text bookmarks, or simply another row like row 3 that
contains a text bookmark but no text in the bookmark.
Tables may contain between 2 and 16 rows having text
bookmarks.
I think the code to do this would need to:
a. Look for the last row in a table that has text in
column 2 (not just the existence of a text field, but a
text field with a Len of > 5 which seems to be the default
length of my text bookmarks for reasons I don't understand
at this point)
b. Delete rows (if any) in the table that follow this last
row.
Thank you very much for any assistance you are able to
provide with these four questions.
Peter
The current version of the code provided by Doug with
additions by Jean-Guy follows:
Sub FSR()
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(3, 2).Range
myrange.End = myrange.End - 1
' Check row 3 column 2 to see if empty-default empty
length is 5
If Len(myrange) = 5 Then
Set myrange = myrange.Tables(1).Range
myrange.SetRange Start:=myrange.Start, _
End:=myrange.End + 1
myrange.Delete
End If
Next i
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset
ScholTotal = 0
For i = 1 To ActiveDocument.Tables.Count
ScholTotal = ScholTotal + ActiveDocument.Tables(i).Cell
(1, 1).Range.FormFields(1).Result
Next i
ActiveDocument.FormFields("ScholTotal").Result =
ScholTotal
End Sub