[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1287
  • Last Modified:

Field Properties In Word Form

I have a protected form in Word.  I am trying to write a macro that will (after being unprotected) set certain fields to calculateonexit = true and give them a bookmark name so I can reference them later.

I have tried to change the properties of the fields, but I do not know the field names for it to go to (ex. With Selection.FormFields(1) or With Selection.FormFields("Text1")).

When the form is unprotected the first field I am trying to work with is already highlighted.  From there I can tab around and get to the other fields, so knowing the names will not be an issue if I can change the setting for the sselected field.  I cannot seem to find a way to change these properties of this field without errors though.  


I have tried a few different things.  Here is one that I recorded that worked when recording, but will not "play" and work.  Thanks for any advice!

    With Selection.FormFields
        .Name = "PartNumber"
        .EntryMacro = ""
        .ExitMacro = ""
        .Enabled = True
        .OwnHelp = False
        .HelpText = ""
        .OwnStatus = False
        .StatusText = ""
        With .TextInput
            .EditType Type:=wdRegularText, Default:="", Format:=""
            .Width = 20
        End With
    End With
    With ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:="PartNumber"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    With ActiveDocument.FormFields("PartNumber")
        .CalculateonExit = True
    End With

0
70sgirl
Asked:
70sgirl
  • 3
  • 3
1 Solution
 
MacroWizardCommented:
How about going about this a different way? Work with the form fields as a collection. All fields are automatically given a name in Word. It is listed as the bookmark name in the properties box for the form field. You can keep the assigned name or change it to suit your purposes. Below is some sample code for you to play with and change the properties as you see fit.

Sub ChangeFormFields()
Dim ff As FormField

For Each ff In ActiveDocument.FormFields
    MsgBox ff.Name
    MsgBox ff.EntryMacro
    If ff.name = "ABC" then
         ff.CalculateOnExit = True
    End If
Next
End Sub

If you need help on any particulars, just shout.
0
 
70sgirlAuthor Commented:
The problem is that there are no names.  The bookmark field is blank in the properties window.
0
 
SteinerCommented:
The problem, why your macro doesn't work is probably that you don't have a formfield selected when you start the macro.
You need to find a way to determine which formfields should be changed (maybe via the number in the collection (is given when you place a formfield, so the first formfield you place in your document should have number 1)).

If you want to manually select the formfield and then run the macro, try the following:

Public Sub FFTest()
    Dim FF As FormField
       
    For Each FF In Selection.FormFields
        FF.CalculateOnExit = True
        ActiveDocument.Bookmarks.Add FF.Name, FF.Range
    Next FF
End Sub

This will go through all formfields in the current selection, set the property calculateonexit=true and will add a bookmark with the same name the formfield has.

Greets
Steiner

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
70sgirlAuthor Commented:
Steiner-

Thanks for the code.  That sounds like a perfect idea, but when I run it I get a Run-time error '5828': Bad bookmark name.  

I made a new document and put a text form field in it to see if the macro would work on it.  It had a default name (in the bookmark box in properties) of Text1.  I ran the macro and it turned calculateonexit on, and no error as far as the bookmark, but when I deleted Text1 from the bookmark field and ran it again I got the same error as before.

That's why I was thinking maybe to just replace the form fields I am trying to change with new ones or give the current ones a name I provide, but I haven't been able to get that to work in a macro either.

Thanks again-
70sgirl
0
 
MacroWizardCommented:
Just to clarify, since ActiveDocument was specified in the first iteration of the macro, that means ALL fields in the current document. You can manipulate all properties for a formfield this way, except for the name. This name issue is a bug and the work around is listed below. Selection is only necessary if you want to iterate through only the fields in a particular area (that is selected) in the document. You do not need to select a form field when you start the macro.

70sGirl, if the issue is that your form fields do not have bookmark names, just go to each field and type the names in the properties box, if this is a template type of form. Or give them bookmark names (VBA refers to it as a name, the properties box refers to it as a bookmark) in the macro.

Somehow you have to be able to identify which formfield it is that you want to change. Usually it is with name or a bookmark. Is the macro changing a form already filled out? Maybe the contents (value) would tell you which fields you need to change? Maybe the type of formfield it is (such as checkbox, number, regular text). Sometimes you have to be creative with your solutions.

Below is the code to assign a bookmark name if there isn’t one.  Each name needs to be unique, that is why the “i” is there.

Sub ChangeFormFields()
Dim ff As FormField
Dim i As Integer

i = 1
For Each ff In ActiveDocument.FormFields
    If ff.Name = "" Then
        ff.Select
        Application.WordBasic.FormFieldOptions Name:="abc" & i
    End If
    i = i + 1
Next
End Sub

0
 
70sgirlAuthor Commented:
The form is already filled out.  We have over 1,000 of these forms with info in them.  The first few fields on each page is for our part number, part name, revision, etc.  Currently when we get a revision change we have to go to each page in the form and change it.  The macro I'm doing will make it so that the pages after page one will automatically update when the info on page one gets updated.

You helped me figure out how to name an unnamed field.  Thanks for your help!!!

MacroWizard-
Your code worked great, but since I only need to change a few fields on each page I adapted it to:

    PartNumber = Selection.Text
    With ActiveDocument.FormFields
        Application.WordBasic.formfieldoptions Name:="PartNumber"
    End With
    With ActiveDocument.FormFields("PartNumber")
        .CalculateonExit = True
    End With
    ActiveDocument.FormFields("PartNumber").Result = PartNumber
   
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    PartName = Selection.Text
    With ActiveDocument.FormFields
        Application.WordBasic.formfieldoptions Name:="PartName"
    End With
    With ActiveDocument.FormFields("PartName")
        .CalculateonExit = True
    End With
    ActiveDocument.FormFields("PartName").Result = PartName

0
 
MacroWizardCommented:
It looks a little funky to me since you using ActiveDocument rather than Selection to change the name of one single form field... it is hard to tell without having seen the actual document you are working on...

But as long as it works for you! Good luck.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now