MS Office Forum / Word / Programming / November 2007
Checkbox -- delete or keep text
|
|
Thread rating:  |
Dawn Rhoads - 17 Nov 2005 23:54 GMT 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!
Greg Maxey - 18 Nov 2005 00:10 GMT Dawn,
Perhaps you will get a few ideas here: http://gregmaxey.mvps.org/Toggle_Data_Display.htm
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> 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 [quoted text clipped - 19 lines] > still in the record-a-macro-and-mess-with-it stage. Thanks in > advance for any help/ideas! Dawn Rhoads - 18 Nov 2005 00:41 GMT 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!
> Dawn, > [quoted text clipped - 24 lines] > > still in the record-a-macro-and-mess-with-it stage. Thanks in > > advance for any help/ideas! Dawn Rhoads - 18 Nov 2005 00:46 GMT 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
> 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 [quoted text clipped - 17 lines] > in the record-a-macro-and-mess-with-it stage. Thanks in advance for any > help/ideas! Greg Maxey - 18 Nov 2005 01:41 GMT 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
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> Well, with the help of some other posts on similar topics throughout > the newgroup, I've cobbled together something that pretty much works. [quoted text clipped - 41 lines] >> I'm still in the record-a-macro-and-mess-with-it stage. Thanks in >> advance for any help/ideas! Greg Maxey - 18 Nov 2005 01:48 GMT 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."
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> Why use bookmarks? > [quoted text clipped - 58 lines] >>> I'm still in the record-a-macro-and-mess-with-it stage. Thanks in >>> advance for any help/ideas! Dawn Rhoads - 18 Nov 2005 02:04 GMT 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! :)
> Why use bookmarks? > [quoted text clipped - 58 lines] > >> I'm still in the record-a-macro-and-mess-with-it stage. Thanks in > >> advance for any help/ideas! Greg Maxey - 18 Nov 2005 03:12 GMT 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.
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> Hi -- Hmmm...well, I thought I would need bookmarks (or something) > for the macro to identify which text needed to be deleted/hidden. [quoted text clipped - 78 lines] >>>> I'm still in the record-a-macro-and-mess-with-it stage. Thanks in >>>> advance for any help/ideas! Dawn Rhoads - 18 Nov 2005 17:57 GMT 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
> 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., [quoted text clipped - 86 lines] > >>>> I'm still in the record-a-macro-and-mess-with-it stage. Thanks in > >>>> advance for any help/ideas! Greg - 18 Nov 2005 19:22 GMT 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.
Dawn Rhoads - 18 Nov 2005 21:53 GMT 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!
> Try something like this: > [quoted text clipped - 16 lines] > > Add the macro to a toolbar button or keyboard shortcut. Greg - 18 Nov 2005 22:19 GMT 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
Dawn Rhoads - 18 Nov 2005 22:28 GMT 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!!
> No that was my goober. If there isn't a formfield in the para it will > generate the error. Try: [quoted text clipped - 15 lines] > ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True > End Sub Dawn Rhoads - 19 Nov 2005 00:29 GMT 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
> No that was my goober. If there isn't a formfield in the para it will > generate the error. Try: [quoted text clipped - 15 lines] > ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True > End Sub Greg Maxey - 19 Nov 2005 00:59 GMT 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
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> 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. [quoted text clipped - 63 lines] >> ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True >> End Sub Dawn Rhoads - 19 Nov 2005 01:41 GMT 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
> Ok, try: > [quoted text clipped - 90 lines] > >> ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True > >> End Sub Greg Maxey - 19 Nov 2005 03:04 GMT 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.
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> 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 [quoted text clipped - 119 lines] >>>> ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True >>>> End Sub Dawn Rhoads - 21 Nov 2005 22:00 GMT 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
> Dawn, > [quoted text clipped - 124 lines] > >>>> ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True > >>>> End Sub Greg Maxey - 21 Nov 2005 22:41 GMT 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
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> 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 [quoted text clipped - 158 lines] >>>>>> ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True >>>>>> End Sub Dawn Rhoads - 21 Nov 2005 22:57 GMT 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.
> Dawn, > [quoted text clipped - 186 lines] > >>>>>> ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True > >>>>>> End Sub Greg Maxey - 21 Nov 2005 23:14 GMT 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
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> 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! [quoted text clipped - 210 lines] >>>>>>>> ActiveDocument.Protect Type:=wdAllowOnlyFormFields, >>>>>>>> noreset:=True End Sub Dawn Rhoads - 22 Nov 2005 00:06 GMT 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.
> Try this: > [quoted text clipped - 237 lines] > >>>>>>>> ActiveDocument.Protect Type:=wdAllowOnlyFormFields, > >>>>>>>> noreset:=True End Sub Greg Maxey - 22 Nov 2005 01:45 GMT 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.
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> 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 [quoted text clipped - 264 lines] >>>>>>>>>> ActiveDocument.Protect Type:=wdAllowOnlyFormFields, >>>>>>>>>> noreset:=True End Sub Dawn Rhoads - 22 Nov 2005 20:29 GMT 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!)
> Dawn, > [quoted text clipped - 271 lines] > >>>>>>>>> and ran the macro with hidden text not visible on the screen, > >>>>>>>>> the Greg Maxey - 22 Nov 2005 23:01 GMT 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
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> Interesting...you're right, I forgot to check out other kind of > fields. [quoted text clipped - 327 lines] >>>>>>>>>>> screen, >>>>>>>>>>> the MIrving - 09 Nov 2007 03:32 GMT 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.
> Try something like this: > [quoted text clipped - 16 lines] > > Add the macro to a toolbar button or keyboard shortcut. Greg Maxey - 11 Nov 2007 20:34 GMT 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
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> Hi Greg > [quoted text clipped - 25 lines] >> >> Add the macro to a toolbar button or keyboard shortcut.
|
|
|