Clear Button for Word forms

P

Pamela

I need a button that will clear data added to form fields in a protected form
- so it is ready to reuse. I also need to have this button not print when
document is printed.

Any suggestions? Thanks
 
J

Jay Freedman

Pamela said:
I need a button that will clear data added to form fields in a
protected form - so it is ready to reuse. I also need to have this
button not print when document is printed.

Any suggestions? Thanks

The main suggestion is to create the form as a template instead of a
document, and use File > New to create a new document based on that template
for each use. That way the fields will always start out empty, and you don't
need a button.

The alternative is fairly nasty. Create a macro like this (assuming all form
fields are text, not dropdowns or checkboxes):

Sub ClearFields()
Dim fld As FormField
For Each fld In ActiveDocument.FormFields
fld.Result = ""
Next
End Sub

Insert a macrobutton field,

{MacroButton ClearFields Double-click here to clear fields}

(see http://www.word.mvps.org/FAQs/TblsFldsFms/UsingMacroButton.htm).

To prevent the macrobutton field from printing, surround it with a
bookmark -- say you name it ClearButton. Then write two macros to intercept
the File > Print menu command and the Print button on the toolbar
(http://www.word.mvps.org/FAQs/MacrosVBA/InterceptSavePrint.htm):

Sub FilePrint()
With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect
End If

.Bookmarks("ClearButton").Range.Font.Color = wdColorWhite
Dialogs(wdDialogFilePrint).Show
.Bookmarks("ClearButton").Range.Font.Color = wdColorAutomatic
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End With
End Sub

Sub FilePrintDefault()
With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect
End If

.Bookmarks("ClearButton").Range.Font.Color = wdColorWhite
.PrintOut Background:=False
.Bookmarks("ClearButton").Range.Font.Color = wdColorAutomatic
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
P

Pamela

Thanks - I guess I should have mentioned that I am using a template - which
is great, but my users like the 'clear button' we use on our pdf forms which
removes all data with a single click.

I'll give your suggestion a try - but we do use drop downs and check boxes,
so I guess that will reneder your 'alternative' useless.
 
R

Russ

Pamela,
Maybe you could include in your template a Menu item or toolbar button to
just close without saving the current partially data-loaded document and
reopen a fresh document based on your document with all new blank fields.
Wouldn't that accomplish the same thing?
 
R

Russ

Correct inline below.
Pamela,
Maybe you could include in your template a Menu item or toolbar button to
just close without saving the current partially data-loaded document and
reopen a fresh document based on your document with all new blank fields.

That should read "fresh document based on your template"
 
P

Pamela

Another good thought - if I was still using 2003 I would know how to do that.

Do you know how with 2007?
 
J

Jay Freedman

Thanks - I guess I should have mentioned that I am using a template - which
is great, but my users like the 'clear button' we use on our pdf forms which
removes all data with a single click.

I'll give your suggestion a try - but we do use drop downs and check boxes,
so I guess that will reneder your 'alternative' useless.

No, it isn't useless, it's just written for the simplest case. To
handle all three kinds of fields, change the macro to

Sub ClearFields()
Dim fld As FormField
For Each fld In ActiveDocument.FormFields
If fld.Type = wdFieldFormTextInput Then
fld.Result = ""
ElseIf fld.Type = wdFieldFormCheckBox Then
fld.CheckBox.Value = False
ElseIf fld.Type = wdFieldFormDropDown Then
fld.DropDown.Value = 1
End If
Next
End Sub

Also, Russ's idea about a toolbar button is good. For Word 2007, put
the button on the Quick Access Toolbar -- click the down arrow at the
end of the bar, choose 'Customize the Quick Access Toolbar', select
the Macros group in the dialog, and add your macro to the list on the
right. You can also choose an icon for the button (that's allowed only
for macros, not for other kinds of commands).

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
D

Doug Robbins - Word MVP

I would suggest that you use a userform instead of a protected document with
formfields.

See the article "How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm


--
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
 

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