Checkbox -- delete or keep text

G

Greg Maxey

Try this:

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
On Error GoTo Proceed
If .FormFields(1).Type = wdFieldFormCheckBox Then
If .FormFields(1).CheckBox.Value = False Then
oPar.Style = "Hidden"
Else
Proceed:
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

Hey, that did the trick -- genius! Any chance of explaining to a novice what
you changed to make that work? It looks like it has something to do with the
bit that says "proceed:" but that was as much as I could figure out. :)

Thanks again for your time and expertise, this is a great macro!

~Dawn R.
 
G

Greg Maxey

Dawn,

I am afraid that I am no genuis. Fact is I am not exactly sure what is
going on here and hoping one of the real geniuses comes along and explains
to us both.

Your problem is not isloated to a dropdown field. In fact the same problem
occurs if you there is a checkbox, dropdown, followed by a paragraph mark
(with or without text in between) and then a line that terminates with the
end of cell marker. It appears that the .formfield.count on the range
containing the end of cell marker retains the count value (1) of the
preceeding paragraph. I can't explain why.

I cleaned up the error handling a little:

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
On Error GoTo Handler
If .FormFields(1).Type = wdFieldFormCheckBox Then
If .FormFields(1).CheckBox.Value = False Then
oPar.Style = "Hidden"
Else
Proceed:
oPar.Style = "Normal"
End If
End If
End If
End With
Next
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
ActiveWindow.View.ShowHiddenText = False
Exit Sub
Handler:
Resume Proceed
End Sub

Ok, we know the conditions above will generate an error on the following
line:
If .FormFields(1).Type = wdFieldFormCheckBox Then

The error is generated because one part of the code is saying there in one
field in the paragraph (there isn't) and another part wants to check if that
field (that doesn't exist) is a checkbox field.

So we put in an error handler:
On Error GoTo Handler

The Error handler sends us the the Tag "Handler"
We then use a Resume statement to clear the error an move on to the tag
"Proceed." This simply routes us around the problem and on with processing.

This isn't pretty, but it is the best I can come up with.

Been fun.
 
D

Dawn Rhoads

Interesting...you're right, I forgot to check out other kind of fields.

FYI, I tested out the latest version of your code, and I think I may
actually prefer the previous error handling since it produces an error in the
event that the "hidden" style has not been set up in the document. In the
newest version, it seems to just run the macro without the error being
apparent, so it just ends up with nothing being done to the paragraphs that
should be hidden. Having the error pop up is a good reminder that I forgot
to set up my document properly! :)

If you are still interested in a related challenge, I am wondering if you
can dream up a way to do a similar operation, but that will work upon
specific text that is within a paragraph. So, for instance, to keep or
delete one sentence or phrase from within a paragraph. I think I could still
use the checkboxes, even within a paragraph, because I plan on setting them
to be hidden text. That seems to work fine, at least with the
select-a-paragraph method you devised.

FYI, in case you're interested, I have also been working on a macro that
will find and delete any text in the document that is set to the "hidden"
style (and a couple of other styles that I use for other purposes that would
also be great to have deleted at the same time). I have a couple issues with
it, which I will put in a separate post. I figure this way, a person could
just "hide" the text until they were absolutely sure they had the choices the
way they wanted it, and then they could take the extra step of deleting the
hidden stuff completely, and then I could delete any instructions built into
the document at the same time.

Thanks again for your insight and time, sure do appreciate the help! (BTW,
If you're bored of this topic and want to move on to other challenges, don't
concern yourself that I will be offended or anything -- I'm really grateful
for the time you've spent already!)
 
G

Greg Maxey

Dawn,

Your right, the error handler I supplied treats all the same. You can
handle both errors separately. Try:

Sub Test()
Dim oPar As Paragraph
Dim oTest As String
ActiveWindow.View.ShowHiddenText = True
Retry:
On Error GoTo Handler1
oTest = ActiveDocument.Styles("Hidden").Description
On Error Resume Next
ActiveDocument.Unprotect
On Error GoTo 0
For Each oPar In ActiveDocument.Paragraphs
With oPar.Range
If .FormFields.Count > 0 Then
On Error GoTo Handler2
If .FormFields(1).Type = wdFieldFormCheckBox Then
If .FormFields(1).CheckBox.Value = False Then
oPar.Style = "Hidden"
Else
Proceed:
oPar.Style = "Normal"
End If
End If
End If
End With
Next
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
ActiveWindow.View.ShowHiddenText = False
Exit Sub
Handler1:
MsgBox "Yo!, you forgot to create a style named Hidden."
Application.Dialogs(wdDialogFormatStyle).Show
Resume Retry
Exit Sub
Handler2:
Resume Proceed
End Sub

Looks like you have enlisted the help of Helmut Weber with your new
projects. Now there is a real genius!! I agree, this string is run its
coarse. Post your other challenges as new strings and if I am interested I
will take a stab.

Cheers
 
M

MIrving

Hi Greg

I found your macro perfect for deleting unselected check box paragraphs.
The only additional functionality I would like to add to my document is for
the remaining check boxes (ie. the checked boxes) to also be deleted, leaving
only the paragraph text for the selected items. Is there anyway of achieving
this? Thank you for any suggestions.
 
G

Greg Maxey

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(1).CheckBox.Value = False Then
oPar.Style = "Hidden"
Else
oPar.Style = "Normal"
oPar.Range.FormFields(1).Delete
End If
Next
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End Sub
 
S

sg

Hi, Greg. I don't know if you'll get this since this post is from a while
ago, but I thought it would be worth a shot.

You suggested the code below to delete text for an unmarked form field
checkbox as well as to delete the checkboxes that were checked leaving just
the text.

I have been trying to use this code, but have not had any luck and have
exhausted everything I know to try.

I get an error: Run-time error 5941: The requested member of the
collection does not exist.

The following line is highlighted:

If oPar.Range.FormFields(1).CheckBox.Value = False Then

Do you have any idea what I might be doing wrong? From reading the other
posts here, I have tried making this field the first field in the form, but
that didn't work. I don't know what else to do.

If you get this, I would appreciate your help!
 
G

Greg Maxey

sg,

Do you have a style named "Hidden?" Is each checkbox and its associated text
comprise a unique paragraph? If you open the VB Editor and step through the
code one line at a time using F8 which line generates the error?

You could try:

Sub Test2()
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.Delete
Else
oPar.Style = "Normal"
oPar.Range.FormFields(1).Delete
End If
Next
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End Sub
 
S

sg

Thanks for getting back to me so quickly.

I do have a style named Hidden. Each checkbox and the paragraph is in its
own paragraph. The checkbox is separated from the text with a tab, but I
tried removing it and it didn't make a difference.

I tried the new code you suggested. This time I get a compile error and
then it says Method or data member not found.

When I do the F8 thing you suggested, in the following line it highlights
the .Delete after Par:

If oPar.Range.FormFields(1).CheckBox.Value = False Then

Does that mean anything? I can run the same deal on the old code I was
using if you want.

Thanks again for your help - I hate to bug you with this, but I really need
this to work.

Thanks.
oPar.Delete
 
S

sg

Thank you so much - that is a very generous offer. I won't be able to e-mail
the whole document because of sensitive information in it, but I will send
you the important stuff.

How do I find your e-mail address? I didn't see it in any of the previous
posts - figured you wouldn't want it out there...
 
S

sg

Call me blind, but I don't see it anywhere...I'll post mine here if you would
rather and you can e-mail me directly so I'll know how to send the document
to you.

Let me know! Thanks.
 
D

Doug Robbins - Word MVP on news.microsoft.com

When I click on Reply Group for Greg's message, I see
(e-mail address removed)

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

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