Reset FormFields(Text) to Default Text Values Within Range

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Reset FormFields(Text) to Default Text Values Within Range

Post by RaudelJr »

Hi,

I'm trying to Reset Text Form Fields to their Default Text Values Within Range.
Every Text Form Field has Default Text entered in the Form Field properties.

The Sample code below will clear all FormFields of every type then for the Text Fields, ActiveDocument.FormFields("Boomark").Result = "DefaultText" will set the field to the Result Value.
But is there a way to simply have the macro reset to the Defalut Text already entered into the Form Field Properties without having to list every Field into the code with a result? (the list can get pretty long).

Also, What if I want to reset the form fields in a specific section of the document? I currently Highlighted a certain section and assigned the selection a Bookmark.
That Selection for instance can be bookmarked as ResetSelection1, another selecction as ResetSelection2, a third selection as ResetSelection3.
(I could also set Start/End Bookmarks: ResetSelection1Start/ResetSelection1End, ResetSelection2Start/ResetSelection2End, ResetSelection3Start/ResetSelection3End.)

Each ResetSelection1 has a Button assigned to reset the FormFields.

Pressing the Reset FormFields button in ResetSelection1 should reset all TextFormFields in that section to the value already within them in the properties, but it will leave ResetSelection2 & ResetSelection3 alone.

The code below Would reset all fields, and then with code each field can be updated.

Code: Select all

Sub ResetFormTextEntries()
Dim FF As FormField
    For Each FF In ActiveDocument.FormFields
        Select Case FF.Type
            Case wdFieldFormTextInput
                FF.Result = ""
            Case wdFieldFormCheckBox
                FF.CheckBox.Value = False
            Case wdFieldFormDropDown
                FF.DropDown.Value = 1
            Case Else
                ' do nothing
        End Select
    Next FF
    ActiveDocument.FormFields("TextFormField01").Result = "DefaultText01"
    ActiveDocument.FormFields("TextFormField02").Result = "DefaultText02"
    ActiveDocument.FormFields("TextFormField03").Result = "DefaultText03"
    ActiveDocument.FormFields("TextFormField04").Result = "DefaultText04"
End Sub
Thank you in advance for your help.

Raudel

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Reset FormFields(Text) to Default Text Values Within Ran

Post by RaudelJr »

Soon after I posted my original post I found the following code @ http://www.gmayor.com/formfieldmacros.htm#Reset

Code: Select all

Sub ResetFormFields()
Dim bProtected As Boolean
Dim oFld As FormFields
Dim i As Long
Set oFld = ActiveDocument.FormFields
'Unprotect the file
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
'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
oFld(1).Select
End Sub
So resetting to default text property is working, I just need now to be able to reset only a specific section/range within a document.

Thanks again.

Raudel

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Reset FormFields(Text) to Default Text Values Within Ran

Post by HansV »

Try the following:

Code: Select all

Sub ResetFormFields(BookmarkName As String)
    Dim rng As Range
    Dim bProtected As Boolean
    Dim oFld As FormField
    Dim i As Long
    'Unprotect the file
    If ActiveDocument.ProtectionType <> wdNoProtection Then
        bProtected = True
        ActiveDocument.Unprotect
    End If
    Set rng = ActiveDocument.Bookmarks(BookmarkName).Range
    For Each oFld In rng.FormFields
        oFld.Select
        If oFld.Name <> "" Then
            Dialogs(wdDialogFormFieldOptions).Execute
        End If
    Next oFld
    'Reprotect the document.
    If bProtected = True Then
        ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
    End If
    rng.FormFields(1).Select
End Sub
Use as follows to reset form fields in ResetSelection1:

Code: Select all

Sub ResetAction1()
    ResetFormFields "ResetSelection1"
End Sub
etc.

Warning: I haven't tested the code.
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Reset FormFields(Text) to Default Text Values Within Ran

Post by RaudelJr »

Hi I tried the code and referred to the macro on Button Clicks with the descriptive bookmark name.

It gave an error as the one attached.
Error.PNG

I'm attaching the file I'm working on.
FTP CF_CW_ ES Requirement.dotm
Thank you HansV for your help. :)
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Reset FormFields(Text) to Default Text Values Within Ran

Post by HansV »

I won't be able to reply until much later today.
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Reset FormFields(Text) to Default Text Values Within Ran

Post by RaudelJr »

No worries.

Thanks again Hans.

Raudel

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Reset FormFields(Text) to Default Text Values Within Ran

Post by HansV »

The name ResetFormFields is ambiguous, since it is also a built-in procedure.
Apart from that, you changed

Set rng = ActiveDocument.Bookmarks(BookmarkName).Range

to

Set rng = ActiveDocument.Bookmarks(bmCFReset).Range

without discernable reason.

However, after correcting this, the code consistently makes my Word crash. I have no idea why, but it means that the code that I proposed is unusable. :sad:
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Reset FormFields(Text) to Default Text Values Within Ran

Post by RaudelJr »

HansV wrote:The name ResetFormFields is ambiguous, since it is also a built-in procedure.
Apart from that, you changed

Set rng = ActiveDocument.Bookmarks(BookmarkName).Range

to

Set rng = ActiveDocument.Bookmarks(bmCFReset).Range

without discernable reason.

However, after correcting this, the code consistently make my Word crash. I have no idea why, but it means that the code that I proposed is unusable. :sad:
Sorry about the code line change, I was performing some newbie testing and forgot to update the code before uploading to the forum.

Thank you for your help, I'll continue looking into this.

Raudel

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Reset FormFields(Text) to Default Text Values Within Ran

Post by RaudelJr »

Hi HansV,

Reviewing many Code samples for various Macro samples, I was able to modify the code as follows and solved this topic.


Code: Select all

Sub ResetSectionFormFields(BookmarkName As String)
Dim bProtected As Boolean
Dim i As Long
Dim fCount As Long
Dim oRng As Range
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
Selection.GoTo What:=wdGoToBookmark, Name:=BookmarkName
Set oRng = Selection.Range
fCount = oRng.FormFields.Count
For i = 1 To fCount
With oRng.FormFields(i)
.Select
If .Name <> "" Then
Dialogs(wdDialogFormFieldOptions).Execute
End If
End With
Next
oRng.End = Selection.Range.End
'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
oRng.Select
End Sub
Macro is called by various Command Buttons as follows:

Code: Select all

Private Sub CommandButton3_Click()
ResetSectionFormFields "ResetSection1"

End Sub
Thanks a ton for all the help you always provide.

Raudel

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Reset FormFields(Text) to Default Text Values Within Ran

Post by HansV »

I'm glad you found a solution! :thumbup:
Best wishes,
Hans