Checkbox -- delete or keep text

D

Dawn Rhoads

I am looking for a way to select from choices of boilerplate text. We
normally use the low-tech fill-in fields in Word, where you tab through
fill-in fields in a document.

I was thinking maybe we could put in the various paragraphs, and where we
have choices, we would put in a check box next to each paragraph. Then, once
the person has checked all their boxes, click a toolbar button to run a macro
which will delete any paragraphs which have an unchecked box. I am guessing
that each paragraph and each checkbox would need to be identified with
bookmarks (I know how to do that part at least!). :) We often put
instructions into hidden text in our documents, so I think I would just make
the checkboxes themselves as hidden text as well, so no need to delete the
remaining box, although would be nice if possible to do so.

I have been looking at solutions using autotext or a mail-merge type
operation, but turns out that is not a good option, since people usually need
to see the entire paragraphs to know which ones to choose.

Sample code I can copy and paste would be greatly appreciated, as I'm still
in the record-a-macro-and-mess-with-it stage. Thanks in advance for any
help/ideas!
 
D

Dawn Rhoads

Thanks Greg, I will try to figure this out; on first glance I am concerned we
may end up with a problem attempting to use autotext just because of a couple
of quirks in our system set up that make things a bit out of the ordinary for
how autotext functions -- mostly has to do with how our document management
system interfaces with Word. But this definitely looks like it would do
exactly what I am trying to do, if I could get the autotext part of it to
work right on our system!

See also my newly attached post which has some code I've cobbled together
from some other posts on similar topics from here. It seems to work, the one
drawback being that I will need a different macro for every different
bookmarked paragraph that I have... If you have any thoughts for
improvements to it, I'd love your input.

Thanks again for the link, looks very promising!
 
D

Dawn Rhoads

Well, with the help of some other posts on similar topics throughout the
newgroup, I've cobbled together something that pretty much works. The
drawback to this is that I will need a separate macro for each different
paragraph that is bookmarked. A bit cumbersome, but will work. If anyone
has any suggestions for improvement, that would be great. Thanks in advance!

Dim ffname As String
ffname = Selection.FormFields(1).Name
With ActiveDocument
.Unprotect
If .FormFields(ffname).CheckBox.Value = False Then
Selection.GoTo What:=wdGoToBookmark, Name:="paragraph1"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.Delete Unit:=wdCharacter, Count:=1
End If
.Protect wdAllowOnlyFormFields, noreset
End With
 
G

Greg Maxey

Why use bookmarks?

Try:
Sub Test2()
Dim ffname As String
ffname = Selection.FormFields(1).Name
With ActiveDocument
.Unprotect
If .FormFields(ffname).CheckBox.Value = False Then
Selection.Paragraphs(1).Format.Style = "Hidden"
Else
Selection.Paragraphs(1).Format.Style = "Normal"
End If
.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End With
End Sub
 
G

Greg Maxey

Sorry,

You will need to create a style named "Hidden" that is an exact duplicate of
"Normal" (or whatever you are using as your text style) except the font
attribute in the "Hidden" style is "hidden."
 
D

Dawn Rhoads

Hi -- Hmmm...well, I thought I would need bookmarks (or something) for the
macro to identify which text needed to be deleted/hidden.

I guess I'm not sure how to use the code you suggested. When I tried it (I
set a checkbox to run this macro on exit) if the box was unchecked upon exit
I get an error saying "item with specified name does not exist." The
debugger highlights the line 'Selection.Paragraphs(1).Format.Style = "Hidden"
' No idea what that means! :)
 
G

Greg Maxey

The macro uses the selection to identify the text to be deleted just like
you were using the selection to determine the formfield name (i.e.,
selection.paragraphs(1))

Our posts must have crossed. You need to create a style named "Hidden." It
is the style that you will apply to the selection paragraph if the box isn't
checked. The Hidden style needs to have the "hidden" font attribute.
 
D

Dawn Rhoads

Wow, that works just great, thanks very much! In playing with it, something
I noticed is that one has to actually click or tab to a new field in order to
get the macro to fire, since the macro only fires upon "exit" from the
checkbox. Is there a way to do this same kind of macro operation, but rather
than running the macro from exiting each checkbox, to use a toolbar button to
run a macro to go and look at each checkbox that is in the form and apply the
same settings like this based on whether the checkbox is true/false?

My apologies if I'm asking for something either extremely complicated or
just dumb; I can't yet tell the difference in VBA between something
relatively easy to do and something practically impossible... Thanks again
for your help!

~Dawn Rhoads
 
G

Greg

Try something like this:

Sub Test()
Dim oPar As Paragraph
On Error Resume Next
ActiveDocument.Unprotect
On Error GoTo 0
For Each oPar In ActiveDocument.Paragraphs
If oPar.Range.FormFields(1).CheckBox.Value = False Then
oPar.Style = "Hidden"
Else
oPar.Style = "Normal"
End If
Next
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End Sub

This looks at the value of the "first" checkbox in each paragraph.

Add the macro to a toolbar button or keyboard shortcut.
 
D

Dawn Rhoads

Thanks Greg -- Unfortunately, I must be doing something wrong again, when I
run this I get an error saying "the requested member of the collection does
not exist" and the debugger higlights the line 'If
oPar.Range.FormFields(1).CheckBox.Value = False Then '

Ideas? And thanks again, I really appreciate your time!
 
G

Greg

No that was my goober. If there isn't a formfield in the para it will
generate the error. Try:

Sub Test()
Dim oPar As Paragraph
On Error Resume Next
ActiveDocument.Unprotect
On Error GoTo 0
For Each oPar In ActiveDocument.Paragraphs
If oPar.Range.FormFields.Count > 0 Then
If oPar.Range.FormFields(1).CheckBox.Value = False Then
oPar.Style = "Hidden"
Else
oPar.Style = "Normal"
End If
End If
Next
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End Sub
 
D

Dawn Rhoads

It's a beautiful thing! Thanks so much, that is completely cool. I haven't
yet had a chance to try out the toggle method you gave me the link for, but
that also looks extremely promising. Thanks again, really appreciate the
help!!
 
D

Dawn Rhoads

Hi Greg -- Darn it, I came across a problem. This code works great as long
as the only type of field in the document is a checkbox. However, I normally
have several different kinds of fields in a document. If there are other
field types, I get a "bad parameter" error. I found some code in another
newsgroup posting talking about this same issue, but I can't figure out how
to incorporate it into your code, I just keep creating a bunch of errors.
Any ideas? Thanks in advance!

If oField.Type = wdFieldFormCheckBox Then

Also, for the benefit of anyone else reading this thread, one other issue
that I found is that it seems any hidden text must be visible on screen when
the macro runs in order for the code to work each time (if it is run more
than once on the same document). So, if I unchecked a few boxes, ran the
macro which hid some of the paragraphs, and then went back and re-checked the
boxes, and ran the macro with hidden text not visible on the screen, the
selected paragraphs would not show up again. So I recorded a bit of code to
add to the beginning to show hidden text, and a bit at the end to make it not
visible again. I'm sure since I recorded that code, so it's probably 5 times
longer than it needs to be, but it seems to work. :)

With ActiveWindow
With .View
.ShowHiddenText = True
End With
End With

Dim oPar As Paragraph
On Error Resume Next
On Error GoTo 0
For Each oPar In ActiveDocument.Paragraphs
If oPar.Range.FormFields.Count > 0 Then
If oPar.Range.FormFields(1).CheckBox.Value = False Then
oPar.Style = "Hidden"
Else
oPar.Style = "Normal"
End If
End If
Next
With ActiveWindow
With .View
.ShowHiddenText = False
End With
End With
 
G

Greg Maxey

Ok, try:

Sub Test()
Dim oPar As Paragraph
ActiveWindow.View.ShowHiddenText = True
On Error Resume Next
ActiveDocument.Unprotect
On Error GoTo 0
For Each oPar In ActiveDocument.Paragraphs
With oPar.Range
If .FormFields.Count > 0 Then 'If there is a formfield
If .FormFields(1).Type = wdFieldFormCheckBox Then 'If the first
formfield is a checkbox
If .FormFields(1).CheckBox.Value = False Then 'If its value is
false
oPar.Style = "Hidden"
Else
oPar.Style = "Normal"
End If
End If
End If
End With
Next
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
ActiveWindow.View.ShowHiddenText = False
End Sub
 
D

Dawn Rhoads

Hi Greg -- that does the trick, you rock! There is one very specific set of
circumstances that must be some kind of bug in Word that causes an error, but
I can work around it. But for some reason, if there is a dropdown field,
that is inside a table, that appears before the first checkbox (yep, all
three of those things must be true to produce the error) for some reason I
get a "the requested member of the collection does not exist" error when it
reaches this line:

If .FormFields(1).Type = wdFieldFormCheckBox Then 'If the first formfield is
a checkbox

For the documents I'm working with I can just change around some field type
and still get this to work, which is a small price to pay to be able to use
such a cool macro! Of course, if you have any thoughts on getting around
that very werid problem, that would be great -- but you've already worked a
miracle as far as I'm concerned.

Thanks so much for your help!

~Dawn Rhoads
 
G

Greg Maxey

Dawn,

Can't replicate that problem here. Send me the document. (Strip out your
existing macros first) I will have a look if I get time.
 
D

Dawn Rhoads

Hi Greg -- thanks for the offer, but I feel like I've taken enough of your
time! It's easy enough for me to work around as is. If you're really
curious and want to take a look, I'm happy to send it to you, but only if you
are interested.

For the benefit of others, I discovered that it doesn't actually matter that
the drop down field appears before the checkboxes, what matters is that it is
in a table and that a hard return follows the drop down box somewhere in the
table cell. The table can be anywhere in the document. If I take the drop
down out of the table,viola, no error. If I remove any hard return AFTER the
drop down in the same table cell, again no error. Sure sounds like a bug in
Word to me.

If you want me to send the document, I'm not sure what your email is, let me
know where I can find it and I will send the document along.

Thanks again, appreciate your time and your help, what you came up with here
is *great*!
Dawn
 
G

Greg Maxey

Dawn,

I will let you be chief tester. Try it with this error handler:
ActiveWindow.View.ShowHiddenText = True
On Error Resume Next
ActiveDocument.Unprotect
On Error GoTo 0
For Each oPar In ActiveDocument.Paragraphs
With oPar.Range
If .FormFields.Count > 0 Then

If .FormFields(1).Type = wdFieldFormCheckBox Then
On Error GoTo Proceed
If .FormFields(1).CheckBox.Value = False Then
oPar.Style = "Hidden"
Else
Proceed:
oPar.Style = "Normal"
End If
End If
End If
End With
Bypass:
Next
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
ActiveWindow.View.ShowHiddenText = False
End Sub
 
D

Dawn Rhoads

Hi Greg -- I just created a whole new macro with this new code and ran it on
the same document for the test. Hope that was the idea! :) Using this code
didn't seem to change things; I get the same error (error 5941, "The
requested member of the collection does not exist"), stopping at the same
line (If .FormFields(1).Type = wdFieldFormCheckBox Then). This macro also
works just fine, like your other one, as long as I get rid of the table with
the drop-down field, or delete any hard returns in the cell after the
drop-down field.
 

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