none
Problem with formfiels with Word 2002 RRS feed

  • Question

  • Hi,

    I have a document with some drop down menu (word 2002). What I need to do is:

    1. If someone is trying to change a value from the menu then word would ask a password. I know the document is already protect.

    2. If the password is ok then proceed with the new value.

    3. If the password is wrong then the actual value remain.

    I tried the following code

    Sub MotDePasse()
    Dim stTemp As String
    stTemp = ActiveDocument.FormFields(2).Result
    Debug.Print stTemp
    If InputBox("Introduisez votre mot de passe !") <> "Moi" Then
    ActiveDocument.FormFields(2).Result = stTemp
    ActiveDocument.FormFields(2).Select
    ActiveDocument.FormFields(2).Enabled = False
    Debug.Print stTemp
    End If
    End Sub

    My problems are:

    1. How may I get the initial value before the change was made?

    2. As I have some drop down, how may I program which formfields was selected?

    3. I notice the macro is run only when the drop down focus is lost... What may I do?

    Thanks,

    François

    Thursday, November 28, 2013 3:53 PM

Answers

  • I wouldn't normally suggest using the ActiveX forms controls, but I think checking for this type of change would be much simpler if you did. I think you would be able to use a Listbox and the GetFocus and Change events.

    In Windows Word 2007 and later you coud probably use Content Controls connected to an XML Custom Part and the ContentControlBeforeStoreUpdate event to detect a change.

    As far as I can see, the one way you could get around point (3) using Legacy Form Fields would be to use a Timer to simulate an Event, along the lines described in Greg Maxey's article here . I have never tried it for real, and personally would usually avoid that kind of approach, but a quick look suggests that it might be feasible. 

    In that example, a Timer is user to fire a subroutine very frequently. That routine can examine the state of any aspect of the document. So, for example, you could iterate over all the dropdown formfields that you needed to check, and look at either theFormfield.Result or theFormFIeld.DropDown.Value. If it has changed, you could then pop up your question and restore the original value if necessary.

    As for getting the initial value, the On Enter macro doesn't help very much because it does fire at the point that you might hope when the user clicks in the dropdown rather than tabbing to it. But I don't think you need to. I think you can get it when the document is opened, and store it. Your monitor routine should detect any change. If the user decides to cancel their action, you set the .Value of the dropdown to its previous value. If the user decides to commit their action, you save the new value. You could probably store values in variables declared inside the Monitor routine as Static (so their values are retained in subsequent calls). Or you could store them in Document Variable objects (which are name-value pairs you can store inthe document).

    For (2), in normal formfield processing, one way you can do it is to look at Selection.Formfields(1).Name. But if you did it the way I have suggested above, I think you would be constantly iterating through any formfields of interest and you would not have to detect which field you were looking at.


    Peter Jamieson

    Friday, November 29, 2013 8:42 PM

All replies

  • I wouldn't normally suggest using the ActiveX forms controls, but I think checking for this type of change would be much simpler if you did. I think you would be able to use a Listbox and the GetFocus and Change events.

    In Windows Word 2007 and later you coud probably use Content Controls connected to an XML Custom Part and the ContentControlBeforeStoreUpdate event to detect a change.

    As far as I can see, the one way you could get around point (3) using Legacy Form Fields would be to use a Timer to simulate an Event, along the lines described in Greg Maxey's article here . I have never tried it for real, and personally would usually avoid that kind of approach, but a quick look suggests that it might be feasible. 

    In that example, a Timer is user to fire a subroutine very frequently. That routine can examine the state of any aspect of the document. So, for example, you could iterate over all the dropdown formfields that you needed to check, and look at either theFormfield.Result or theFormFIeld.DropDown.Value. If it has changed, you could then pop up your question and restore the original value if necessary.

    As for getting the initial value, the On Enter macro doesn't help very much because it does fire at the point that you might hope when the user clicks in the dropdown rather than tabbing to it. But I don't think you need to. I think you can get it when the document is opened, and store it. Your monitor routine should detect any change. If the user decides to cancel their action, you set the .Value of the dropdown to its previous value. If the user decides to commit their action, you save the new value. You could probably store values in variables declared inside the Monitor routine as Static (so their values are retained in subsequent calls). Or you could store them in Document Variable objects (which are name-value pairs you can store inthe document).

    For (2), in normal formfield processing, one way you can do it is to look at Selection.Formfields(1).Name. But if you did it the way I have suggested above, I think you would be constantly iterating through any formfields of interest and you would not have to detect which field you were looking at.


    Peter Jamieson

    Friday, November 29, 2013 8:42 PM
  • Hi François,

    To get the entry value, simply use a macro attached to the Entry property. For example, assuming 'VieuxMot' as the entry sub:

    Dim stTemp As String

    Sub VieuxMot()
    stTemp = ActiveDocument.FormFields(2).Result
    End Sub

    Sub MotDePasse()
    If InputBox("Introduisez votre mot de passe !") <> "Moi" Then _
       ActiveDocument.FormFields(2).Result = stTemp
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, November 30, 2013 1:18 AM
  • simply use a macro attached to the Entry property

    It doesn't always fire when you _click_ in the field. It seems (sometimes) to start that way, then can change to a state where the Entry macro is only fired when a new selection is made in the dropdown.

    Peter Jamieson

    Saturday, November 30, 2013 11:56 AM