?
Solved

Field Properties In Word Form

Posted on 2003-03-11
7
Medium Priority
?
1,281 Views
Last Modified: 2008-02-26
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
Comment
Question by:70sgirl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 1

Expert Comment

by:MacroWizard
ID: 8113783
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
 
LVL 1

Author Comment

by:70sgirl
ID: 8113894
The problem is that there are no names.  The bookmark field is blank in the properties window.
0
 
LVL 11

Expert Comment

by:Steiner
ID: 8118028
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 1

Author Comment

by:70sgirl
ID: 8118818
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
 
LVL 1

Accepted Solution

by:
MacroWizard earned 200 total points
ID: 8119538
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
 
LVL 1

Author Comment

by:70sgirl
ID: 8120164
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
 
LVL 1

Expert Comment

by:MacroWizard
ID: 8120315
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question