Results 1 to 7 of 7
  1. #1
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reactivating document window (VBA-Word 2003)

    I'm having awful trouble with what should be simple code. The following successfully toggles the visibility of the Word application and allows the userform to stay visible. But upon exiting the dialog (CommandButton2) the original document window is unable to receive focus, requiring an "ungracious" killing of Word.
    <pre>Option Explicit

    Dim refDoc As Document

    Sub toggle()
    With Application
    If .Visible = True Then
    .Visible = False
    Me.Show
    Else
    .Visible = True
    End If

    End With
    End Sub

    Private Sub CommandButton1_Click()
    toggle
    End Sub

    Private Sub CommandButton2_Click()
    Application.Visible = True
    refDoc.Activate
    Unload Me

    End Sub

    Private Sub UserForm_Initialize()
    Set refDoc = ThisDocument
    End Sub
    </pre>


    I've tried everything I can think of to retrieve focus. Any ideas please?

    Alan

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

    Re: Reactivating document window (VBA-Word 2003)

    Why do you want to hide Word?

  3. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reactivating document window (VBA-Word 2003)

    Ideally, I'd like to hide/show the document window to which the code is attached, but I can see no method for doing this in the object model. The Application seems to be the only object to support such action.

    Alan

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Reactivating document window (VBA-Word 2003)

    You make the application visible but is the document within the application not enabled?

    Do you get the same behaviour if you step through the code? If not perhaps the refDoc.Activate line could move in front of the Application.Visible = True line or you could try a delay to give the Application time to become visible.

    Another possibility is to set the visibility in the code which called the UserForm rather than in the UserForm itself.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reactivating document window (VBA-Word 2003)

    The visibility of the Application doesn't seem to be the issue - that action appears to be "instant". The problem is that I can't regain focus on it - either through code or even manually. It's as if the modal userform is still holding the focus, even though it's not visible and supposedly unloaded.

    The Unload sub works OK provided I haven't used the toggle routine; but as soon as I've used CommandButton_1... <img src=/S/meltdown.gif border=0 alt=meltdown width=15 height=15>

    Alan

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

    Re: Reactivating document window (VBA-Word 2003)

    It works for me if I set the ShowModal property of the userform to False (or if I open it using UserForm1.Show vbModeless).

    And to set focus to the document window, the following works:

    Sub toggle()
    With ThisDocument.ActiveWindow
    If .Visible = True Then
    .Visible = False
    Me.Show vbModeless
    Else
    .Visible = True
    End If
    End With
    End Sub

    Private Sub CommandButton1_Click()
    toggle
    End Sub

    Private Sub CommandButton2_Click()
    Me.Hide
    ThisDocument.ActiveWindow.Visible = True
    ThisDocument.Activate
    Unload Me
    End Sub

  7. #7
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reactivating document window (VBA-Word 2003)

    Thanks Hans - works the way I was after. I was gradually heading towards form modality I think, but you picked it straight away! <img src=/S/clever.gif border=0 alt=clever width=15 height=15>

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>
    Alan

Posting Permissions

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