Share via

Modeless UserForm and Document Focus

Anonymous
2010-11-04T22:12:28+00:00

Couldn't find and answer to this question, so thought I'd try asking. :)

I have a UserForm that prompts my users through a complex, long, Word form. There are fields on the form, but my users can navigate better if I guide them by using several UserForms (one's a menu) to complete the various sections on the underlying Word 2003 Document.

But there are several sections on the Word form that are not protected so the users can paste spreadsheets, images, and other data in those sections only. I use a Menu option on the main UserForm to navigate to the section, and give the users a modeless UserForm to return to the main menu when they're done. That way the can type, paste, and insert to their hearts content and I don't (in theory) lose control of where they are in the document.

However, when I show that Return to Menu UserForm, the focus remains on the userform. Is there a way in code to place the focus directly in the section on the document itself? It's a little thing, but my users want to just start typing right away without having to click into the document.

Any suggestions?

Thanks

--james

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2010-11-05T04:36:28+00:00

Without using some API that I don't know about, this can be done with a little trickery.

Create another form the same size as your form of interest call it frmTemp.  It the frmTemp module place the following code:

Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub UserForm_Activate()

DoEvents

Sleep 5 'That is 5 milliseconds

Unload Me

ThisDocument.Activate

End Sub

In a code module call the frmTemp and your frmOfInterest as follows:

Public Sub CallForm()

  frmOfInterest.Show vbModeless

  frmTemp.Show vbModeless

End Sub

Basically your frmOfInterest is displayed.  Then it loses focus to frmTemp which loses focus to the document in 5 miliseconds.


Greg Maxey --- Visit my website at: http://gregmaxey.mvps.org/word\_tips.htm

Was this answer helpful?

0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Anonymous
    2010-11-05T18:26:09+00:00

    jkitzy, Andreas,

    Well now I feel sort of silly.  After looking at Andreas' suggestions I tinkered some more and it seems that it is can be this simple:

    Sub CallForm()

    UserForm1.Show vbModeless

    Application.Activate

    End Sub

    I couldn't figure that out years ago and have used the convoluted method I first posted ever since :-(


    Greg Maxey --- Visit my website at: http://gregmaxey.mvps.org/word\_tips.htm

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2010-11-05T13:50:04+00:00

    Am 05.11.2010 13:01, schrieb Greg Maxey:

    Thanks for the post.  As already indicated, I am not familiar with APIs and I think that is what you have shown us here.  It looks interesting but I can't make it work.

    Okay, step by step, maybe some other people has the same problem.

    Make a new file

    Open the VBA editor by pressing Alt-F11

    Insert a userform and create a commandbutton on it

    Put all code from my last post in the code module of that form

    Insert a normal module and insert this code:

    --- schnipp ---

    Sub test()

      UserForm1.Show vbModeless

    End Sub

    --- schnapp ---

    Close the VBA editor

    Press Alt-F8 and execute the macro test, the userform appears and has the focus.

    Click on the commandbutton, the userforms stays open and the document got the focus.

    A technical note: The window of the userform is not shown when UserForm_Initialize is executed, so the foreground window is allways the main application. Only when UserForm_Activate is executed, the window of the userform is visible.

    Andreas.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2010-11-05T12:01:12+00:00

    Andreas,

    Thanks for the post.  As already indicated, I am not familiar with APIs and I think that is what you have shown us here.  It looks interesting but I can't make it work. 

    I could be wrong or just don't know how to apply what you have provided, but I don't think it fully addresses the problem either.  I think the OP wants to click a button in a main UserForm, hide that form, show a new modeless form with the focus on the text in the document (not on the new form).

    Then when as button in the new form is click it hides it and returns the main form to focus.

    Is there some way to use the functions that you have provided to achieve that?

    Thanks.


    Greg Maxey --- Visit my website at: http://gregmaxey.mvps.org/word\_tips.htm

    Was this answer helpful?

    0 comments No comments
  4. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2010-11-05T08:21:29+00:00

    Option Explicit

    Private Declare Function GetForegroundWindow Lib "user32" () As Long

    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long

    Private hWnd As Long

    Private Sub CommandButton1_Click()

      SetForegroundWindow hWnd

    End Sub

    Private Sub UserForm_Initialize()

      hWnd = GetForegroundWindow

    End Sub

    Was this answer helpful?

    0 comments No comments