Results 1 to 8 of 8
  1. #1
    2 Star Lounger
    Join Date
    Aug 2002
    Location
    Adelaide, South Australia, Australia
    Posts
    146
    Thanks
    5
    Thanked 5 Times in 5 Posts

    Previous Control in Word Userform (Word 2007)

    In Access VBA I can click a button in a form to execute a procedure, using Screen.PreviousControl to note the control that had the focus before I clicked the button. In so doing, I can return to the previous control after the code attached to the button has executed.

    Does anyone know the equivalent method for a Word UserForm?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Previous Control in Word Userform (Word 2007)

    As far as I know, there is no built-in support for that, so you'll have to program it yourself, for example like this:

    Private PreviousControl As MSForms.Control

    Private Sub CommandButton1_Click()
    ...
    If Not PreviousControl Is Nothing Then
    PreviousControl.SetFocus
    End If
    End Sub

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Set PreviousControl = Me.TextBox1
    End Sub

    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Set PreviousControl = Me.TextBox2
    End Sub

    Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Set PreviousControl = Me.TextBox3
    End Sub

  3. #3
    2 Star Lounger
    Join Date
    Aug 2002
    Location
    Adelaide, South Australia, Australia
    Posts
    146
    Thanks
    5
    Thanked 5 Times in 5 Posts

    Re: Previous Control in Word Userform (Word 2007)

    Hi Hans - I used to feel guilty that I didn't get time to answer other peoples' posts, but you are so quick off the mark that I'm still working out what the question is by the time you post the answer!! <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23> Do you ever sleep?!

    Anyway, thanks for your quick response. I had a feeling that I might have to program something myself, and you have confirmed the direction I was anticipating - pity that something so easy in Access is absent from Word, but at least I know I'm not reinventing the wheel by using the procedure you've suggested.

    Cheers

  4. #4
    2 Star Lounger
    Join Date
    Aug 2002
    Location
    Adelaide, South Australia, Australia
    Posts
    146
    Thanks
    5
    Thanked 5 Times in 5 Posts

    Re: Previous Control in Word Userform (Word 2007)

    PS - I changed:

    Set PreviousControl = Me.TextBox1

    to:

    Set PreviousControl =Me.ActiveControl

    which made it easier to copy and paste the code to the other 17 controls in the userform.

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Previous Control in Word Userform (Word 2007)

    Excellent idea! <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15>

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Previous Control in Word Userform (Word 2007)

    *I* answered this over a year before Hans did.
    I'm waaaay faster!
    <post:=666,177>post 666,177</post:>

  7. #7
    2 Star Lounger
    Join Date
    Aug 2002
    Location
    Adelaide, South Australia, Australia
    Posts
    146
    Thanks
    5
    Thanked 5 Times in 5 Posts

    Re: Previous Control in Word Userform (Word 2007)

    Well, what hope do I have when you are answering questions before they've even been asked?! <img src=/S/dizzy.gif border=0 alt=dizzy width=15 height=15>

    Mind you, I've had a look at the attachment to that earlier post, and am not sure that it actually addresses what I was trying to do - I wasn't wanting to undo the previous control, just reselect it after using the button - am I missing something?

  8. #8
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Previous Control in Word Userform (Word 2007)

    >am I missing something?

    You wanted to know something about "Previous" control, to be able to identify and presumably to do something with/about it.
    I developed a working example, not too robust, that remembered previously selected controls, going back quite a ways.
    I reasoned that if I could remember back, say, 6 controls, then certainly I could remember back one control.
    If by "previous" we both mean "had the focus before I clicked the button", then take my sample code and modify it; you can probably just take the element from the top of the stack, I think that's all you need.

    (later) OK I took a quick look at my doc. I seem to preserve the content of each control. This makes sense to me because at the time I had a client who wanted to UnDo something that had just been clicked/browsed/typed.
    Your needs are that you preserve the identity of the latest GUI event.
    To do that you could modify my typGUI to hold a boolean value for every control, and at any control event, set all boolean values in the structure to false, except the one just clicked - set it to true.
    Once you have reclaimed the identity of the previous control, you can activate/select it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •