none
Reset all Form RRS feed

  • Question

  • Hi,

    I took the following code from Mayor's web and i'm getting a bug. I checked all of my formfileds and they all have bookmarks.

    Private Sub CommandButton3_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

    What is doing is:

    It does clean everything but creates a new formfields with no bookmark name.

    Please help


    Thanks for everything, all your help is well appreciated
    Thursday, April 7, 2011 1:17 PM

Answers

  • Peter, Thanks a lot! The following code does everything i needed. Private Sub CommandButton3_Click() With ActiveDocument If .ProtectionType <> wdNoProtection Then .Unprotect Password:=Pwd .ResetFormFields .Fields.Update .Protect wdAllowOnlyFormFields, NoReset:=True, Password:=Pwd End With Private Sub CommandButton3_Click() Dim objFF As FormField For Each objFF In ActiveDocument.FormFields With objFF '.Select Select Case .Type Case WdFieldType.wdFieldFormCheckBox .CheckBox.Value = .CheckBox.Default Case WdFieldType.wdFieldFormDropDown If .DropDown.ListEntries.Count > 0 Then If (.DropDown.Default > 0) And (.DropDown.Default < .DropDown.ListEntries.Count) Then .DropDown.Value = .DropDown.Default Else .DropDown.Value = 1 End If End If Case WdFieldType.wdFieldFormTextInput Select Case .TextInput.Type Case WdTextFormFieldType.wdCalculationText .Result = 0 Case Else .Result = .TextInput.Default ' End Select Case Else ' End Select End With Next ActiveDocument.Fields.Update End Sub Thanks for everything, all your help is well appreciated
    • Marked as answer by scorpdevil Friday, April 8, 2011 8:23 PM
    Friday, April 8, 2011 8:20 PM
  • Hi scorpdevil-

    My email contained an error, because I forgot an End Sub. You should be able to use

    Private Sub CommandButton3_Click()

    With ActiveDocument

      If .ProtectionType <> wdNoProtection Then .Unprotect Password:=Pwd

      .ResetFormFields .Fields.Update

      .Protect wdAllowOnlyFormFields, NoReset:=True, Password:=Pwd

    End With 

    End Sub

     

    Assuming it works OK, it should be much quicker. If it doesn't then the other approach should work, i.e. the one that starts with the second Private Sub CommandButton3_Click



     


    Peter Jamieson
    • Marked as answer by scorpdevil Friday, April 8, 2011 9:08 PM
    Friday, April 8, 2011 9:02 PM

All replies

  • Hi scorpdevil

    Could you please describe the "bug" you're getting? Error message? If yes, what's the text? And what line is triggering it?

    Which version of Word is involved?


    Cindy Meister, VSTO/Word MVP
    Thursday, April 7, 2011 1:29 PM
    Moderator
  • Hi Cindy,

    The problems is that it is not giving me an error. It cleans everything. But.

    As Mayor said:

    The command line Dialogs(wdDialogFormFieldOptions).Execute will throw an error if the form field is unnamed. As it is poor practice to use unnamed form fields, I have not provided a workaround.

    I have gone thru every formfield in my document and i can't find one with no bookmark name in it.

    So, the problem is that when i run the code it resets everything, but it shows there is one formfields that has no bookmark name. then i go and enter a bookmark name and goes to another formfield.


    Thanks for everything, all your help is well appreciated
    Thursday, April 7, 2011 1:35 PM
  • Hi Scropdevil

    Mmm, perhaps you should provide a link to that page of Graham's that you're working with, so that we can get better context. What you're trying to do isn't really clear.

    Are you just trying to remove the content the user has entered in the form fields?


    Cindy Meister, VSTO/Word MVP
    Thursday, April 7, 2011 1:42 PM
    Moderator
  • Cindy,

    Yes, have created a Command Button and I would like to users use that button if something goes wrong during the process of entering the need info.

    Here you go

    http://www.gmayor.com/formfieldmacros.htm#Reset


    Thanks for everything, all your help is well appreciated
    Thursday, April 7, 2011 2:10 PM
  • Just out of interest, does the following do the right type of resetting for you?

    Sub myResetFormFields()

    Dim objFF As FormField

    For Each objFF In ActiveDocument.FormFields

      With objFF

        Select Case .Type

          Case WdFieldType.wdFieldFormCheckBox

            .CheckBox.Value = .CheckBox.Default

          Case WdFieldType.wdFieldFormDropDown

            .DropDown.Value = .DropDown.Default

          Case WdFieldType.wdFieldFormTextInput

            .Result = .TextInput.Default

          Case Else

            '

        End Select

      End With

    Next

     


    Peter Jamieson
    Thursday, April 7, 2011 2:53 PM
  • Peter,

    At first it debugged

    .DropDown.Value = .DropDown.Default

    Then i tryed again and is guiving me the following error

    Run-time error '4608':

    Value out of range


    Thanks for everything, all your help is well appreciated
    Thursday, April 7, 2011 3:11 PM
  • OK.

    Assuming you never change the defualt, how about

    .DropDown.Value = 1

    ?


    Peter Jamieson
    Thursday, April 7, 2011 3:24 PM
  • Sorry, it looks as if you need at least the following, and you may also need to verify that .DropDown.Default is in the range 1->.DropDown.ListEntries.Count when the count is not zero.

     

            If .DropDown.ListEntries.Count > 0 Then

              .DropDown.Value = .DropDown.Default

            End If


    Peter Jamieson
    Thursday, April 7, 2011 3:30 PM
  • Rich,

    It fixed the DropDown problem but now is giving me another.

    The formfields that are set as a calculation the are showing as follow:

    ($t18,t19,t20,t21,t22,t23,t24,t25.00) insted of $0.00

    And also, it hides some of my formfields and creates another right next to it with no bookmark name.


    Thanks for everything, all your help is well appreciate
    Thursday, April 7, 2011 10:33 PM
  • > Rich,

    I don't think Rich has jumped into this conversation yet. But he might!

    > And also, it hides some of my formfields and creates another right next to it with no bookmark name.

    So that's no different from what you were seeing before when the code was executing Dialogs(wdDialogFormFieldOptions).Execute
    Which makes it even more likely that something abnormal is going on: I wasn't completely sure whether .Execute of that particular Dialog might result in the creation of a new field, although it did not seem likely, but I cannot see anything in the code that I suggested that would do that. Can you? If not, other than "something has gone wrong with your Word installation" the main possiility that I can think of right now is that you have code that is intercepting one of the method calls in these routines (cf. the "FileSave" code we have been discussing elsewhere) and is creating  a new form field as a result.



    Peter Jamieson
    Thursday, April 7, 2011 10:45 PM
  • Peter, sorry!

    Can it be that they are not called by number order?

    Some are called as DOB, DropDownMember and some others are with number like Text1, Text2,3,4,5,6.....and so.

    I have separeted everytning by it self. I wanted to add a commandbutton as a Reset Form.

    Also, i'm using a lot of them hidden.

    I even entered a password protected expression

    Private Sub CommandButton3_Click()
    With ActiveDocument
        If .ProtectionType <> wdNoProtection Then .Unprotect Password:=Pwd
            Dim objFF As FormField
            For Each objFF In ActiveDocument.FormFields
            With objFF
                Select Case .Type
                Case WdFieldType.wdFieldFormCheckBox
                    .CheckBox.Value = .CheckBox.Default
                Case WdFieldType.wdFieldFormDropDown
                    If .DropDown.ListEntries.Count > 0 Then
                        .DropDown.Value = .DropDown.Default
                    End If
                Case WdFieldType.wdFieldFormTextInput
                    .Result = .TextInput.Default
                Case Else
                    '
                End Select
            End With
        Next
        .Protect wdAllowOnlyFormFields, NoReset:=True, Password:=Pwd
    End With
    End Sub


    Thanks for everything, all your help is well appreciated
    Thursday, April 7, 2011 10:59 PM
  • Sorry, scorpdevil. Right now I just can't replicate this. If you are able to send me a copy of your document (or perhaps a "sanitised" version of it that still displays the problem), I will have a look at it here - despam my email :

    pjj at pjjnet dot demon dot co dot uk

    Perhaps the reason will then become clear.

    FWIW other than that problem, Graham's code seems to work fine and is simpler than the route I have suggested + the improvements needed to make that work. 


    Peter Jamieson
    Friday, April 8, 2011 11:51 AM
  • Peter, Thanks a lot! The following code does everything i needed. Private Sub CommandButton3_Click() With ActiveDocument If .ProtectionType <> wdNoProtection Then .Unprotect Password:=Pwd .ResetFormFields .Fields.Update .Protect wdAllowOnlyFormFields, NoReset:=True, Password:=Pwd End With Private Sub CommandButton3_Click() Dim objFF As FormField For Each objFF In ActiveDocument.FormFields With objFF '.Select Select Case .Type Case WdFieldType.wdFieldFormCheckBox .CheckBox.Value = .CheckBox.Default Case WdFieldType.wdFieldFormDropDown If .DropDown.ListEntries.Count > 0 Then If (.DropDown.Default > 0) And (.DropDown.Default < .DropDown.ListEntries.Count) Then .DropDown.Value = .DropDown.Default Else .DropDown.Value = 1 End If End If Case WdFieldType.wdFieldFormTextInput Select Case .TextInput.Type Case WdTextFormFieldType.wdCalculationText .Result = 0 Case Else .Result = .TextInput.Default ' End Select Case Else ' End Select End With Next ActiveDocument.Fields.Update End Sub Thanks for everything, all your help is well appreciated
    • Marked as answer by scorpdevil Friday, April 8, 2011 8:23 PM
    Friday, April 8, 2011 8:20 PM
  • I don't know why is showing like that in my prior answer. Hope other users can see the code. Sorry!
    Thanks for everything, all your help is well appreciated
    Friday, April 8, 2011 8:22 PM
  • Hi scorpdevil-

    My email contained an error, because I forgot an End Sub. You should be able to use

    Private Sub CommandButton3_Click()

    With ActiveDocument

      If .ProtectionType <> wdNoProtection Then .Unprotect Password:=Pwd

      .ResetFormFields .Fields.Update

      .Protect wdAllowOnlyFormFields, NoReset:=True, Password:=Pwd

    End With 

    End Sub

     

    Assuming it works OK, it should be much quicker. If it doesn't then the other approach should work, i.e. the one that starts with the second Private Sub CommandButton3_Click



     


    Peter Jamieson
    • Marked as answer by scorpdevil Friday, April 8, 2011 9:08 PM
    Friday, April 8, 2011 9:02 PM
  • Peter,

    Much, Much Better.....very quick!

    Nice!.


    Thanks for everything, all your help is well appreciated
    Friday, April 8, 2011 9:08 PM