g8r777
02-28-2011, 11:37 AM
I have created a form for people at my company to use. It is saved in a shared location and occassionally gets saved with data in it (as a completed form). I wanted to include a reset button that would clear all of the form fields.
Here is the code I used:
Sub ResetButton_Click()
Dim bProtected As Boolean
Dim oFld As FormFields
Dim i As Long
Set oFld = ActiveDocument.FormFields
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
For i = 1 To oFld.Count
With oFld(i)
.Select
If .Name <> "" Then
Dialogs(wdDialogFormFieldOptions).Execute
End If
End With
Next
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
oFld(1).Select
End Sub
I included a command button in the word document that runs that above code.
My problem is, the code works fine and resets the form fields when the document is unprotected but does nothing if the document is protected. I tried placing the Reset button in a section break that is not protected and that didn't work.
Any help is appreciated.
Thank you,
Brian
Here is the code I used:
Sub ResetButton_Click()
Dim bProtected As Boolean
Dim oFld As FormFields
Dim i As Long
Set oFld = ActiveDocument.FormFields
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
For i = 1 To oFld.Count
With oFld(i)
.Select
If .Name <> "" Then
Dialogs(wdDialogFormFieldOptions).Execute
End If
End With
Next
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
oFld(1).Select
End Sub
I included a command button in the word document that runs that above code.
My problem is, the code works fine and resets the form fields when the document is unprotected but does nothing if the document is protected. I tried placing the Reset button in a section break that is not protected and that didn't work.
Any help is appreciated.
Thank you,
Brian