PDA

View Full Version : Problem with command button macro running in protected mode



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

Frosty
02-28-2011, 03:36 PM
What version of Word are you working in?

Also, use the "CODE" or "VBA" tags around your code... makes it a lot easier to read.

My initial take is that I think you're doing too much work with your code, although I know that's not the problem. If I recall correctly, using vba to turn the protection of a document off and back on is enough to reset all of the form fields. However, if you want to reset some and not all, then you would need to loop through all of them and check the naming convention.

A sample document (minus private information) that contains the above code would also be useful in trying to help you troubleshoot.

Frosty
02-28-2011, 03:48 PM
Just a quick follow up... in Word 2010 the above code works on a blank document with a couple of form fields and a macrobutton (these are all considered "legacy" controls).

However, the following code would also have the same effect.


Private Sub CommandButton1_Click()
'comment out whichever you want
NewWay
'OldWay
End Sub
Sub OldWay()
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
Sub NewWay()
Dim bWasProtected As Boolean

'see what our starting off place is
If ActiveDocument.ProtectionType <> wdNoProtection Then
bWasProtected = True
ActiveDocument.Unprotect
End If

'protect it, which resets all form fields
ActiveDocument.Protect wdAllowOnlyFormFields

'if we didn't start protected, then unprotect again
If bWasProtected = False Then
ActiveDocument.Unprotect
End If
End Sub

g8r777
03-02-2011, 12:51 PM
I am using Word 2010.

I figured out that if you click on the button and hold down the click for a moment or so that somehow Word registers this as a double click and will fire the macro.

It seems that the problem resolved itself which is a first for me.

Frosty
03-02-2011, 12:55 PM
Hmm. Dubious. Does it also work when the wind blows in a north-northwesterly direction? ;)

Regardless, glad it's solved...?

g8r777
03-02-2011, 01:22 PM
East or West. Doesn't matter.

I thought it was a little strange that it just started to work.

Better to be lucky than good I guess.