none
Clearing Form Fields RRS feed

  • Question

  • I have a macro which I need to clear the value of all text formfields within a boomark range in the document.

    If I set the oFF.Result to a value, for example "test", then it works no problem.

    However when I use oFF.Result="", I get an error.

    Can anyone tell me where I am going wrong? Thanks.

    Sub ClearEvents()
    ActiveDocument.Unprotect Password:=""
    Dim oFF As FormField
    For Each oFF In ActiveDocument.Bookmarks("Sec7").Range.FormFields
    Select Case oFF.Name
    Case Is = ""
    oFF.Result = ""
    End Select
    Next oFF
    ActiveDocument.Protect (wdAllowOnlyFormFields), Password:="", NoReset:=True
    End Sub

    Wednesday, September 17, 2014 9:19 AM

Answers

  • If you want to clear all formfields in the range "Sec7" then why have you added the condition that oFF.Name = ""?

    The following works here without error:

    Sub ClearEvents()
    Dim oFF As FormField
      ActiveDocument.Unprotect Password:=""
      For Each oFF In ActiveDocument.Bookmarks("Sec7").Range.FormFields
        Select Case oFF.Name
          Case Is = ""
            Select Case oFF.Type
              Case Is = wdFieldFormTextInput, wdFieldFormCheckBox
                oFF.Result = ""
              Case wdFieldFormDropDown
                oFF.DropDown.Value = 1
            End Select
          Case Else
            'Do nothing.
        End Select
      Next oFF
      ActiveDocument.Protect (wdAllowOnlyFormFields), Password:="", NoReset:=True
    End Sub


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    • Marked as answer by HarrierJet Thursday, September 18, 2014 6:51 AM
    Wednesday, September 17, 2014 8:53 PM

All replies

  • If you want to clear all formfields in the range "Sec7" then why have you added the condition that oFF.Name = ""?

    The following works here without error:

    Sub ClearEvents()
    Dim oFF As FormField
      ActiveDocument.Unprotect Password:=""
      For Each oFF In ActiveDocument.Bookmarks("Sec7").Range.FormFields
        Select Case oFF.Name
          Case Is = ""
            Select Case oFF.Type
              Case Is = wdFieldFormTextInput, wdFieldFormCheckBox
                oFF.Result = ""
              Case wdFieldFormDropDown
                oFF.DropDown.Value = 1
            End Select
          Case Else
            'Do nothing.
        End Select
      Next oFF
      ActiveDocument.Protect (wdAllowOnlyFormFields), Password:="", NoReset:=True
    End Sub


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    • Marked as answer by HarrierJet Thursday, September 18, 2014 6:51 AM
    Wednesday, September 17, 2014 8:53 PM
  • Thanks Greg, it worked exactly as required.

    Thursday, September 18, 2014 6:51 AM