Results 1 to 2 of 2
  1. #1
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211

    BringWindowToTop not Working

    My Access application automates Word and does a mailmerge. I want the Word application to remain on top when I create the merge letters.


    The code does several things, such as exporting the proper records to a CSV file then automating Word to do a merge from that file.

    The code runs 2 different ways. If I want to create letters with all selected records on the continuous form, it does this when I click a command button in the form's header. This executes the merge, saves the resulting Word document and activates Word to bring it to the front.

    Code:
    Private Sub cmdMergeToWord_Click()    
        TempVars!TYLetterMode = "ExportTYData"
        TempVars!TYTableName = "SelectedTYLetters"
        Application.CommandBars("ThankYouLetters").ShowPopup
    End Sub
    If I want to show a "preview" of the Word document for a particular record, I click a control on the continuous form, and it should do almost the same thing:
    Code:
    Private Sub txtPreview_Click()
        Me.chkPending.SetFocus
        If Me!IsSelected Then
            TempVars!TYLetterMode = "Preview"
            TempVars!SelectedDonationID = Me!DonationID.Value
            TempVars!TYTableName = "SelectedTYLetters"
            Application.CommandBars("ThankYouLetters").ShowPopup
        End If
    End Sub
    The differences being that this code doesn't execute if I click a control that is on a record not "selected" (there is a check box bound to the record's IsSelected column), and it sets a TempVar to the ID of the record I chose. This causes Word to do the merge and display the document without saving it, so it is called "Document 1" or "Form Letters 1" or something like that.

    The problem occurs when I invoke the second code to show a preview of the document. Word does the merge, and the Word window flashes on top for a moment, but then the Access window becomes active.

    The last bit of code in the MergeToWord function callss the BringWindowToTop API using the hWnd of the Word window. This is executed regardless of which method above is used to invoke the MergeToWord function. I can't figure out why this happens when activated from a control on the continuous form record, but not from a control in the form's header.

    Any ideas? Thanks...

  2. #2
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211
    Everything I tried would not work properly. Finally, I created a work-around. Here is new click event of preview control on continuous form:
    Code:
    Private Sub txtPreview_Click()
    
        Dim wd      As WindowDimensionsType
        Dim myLeft  As Long
        Dim mytop   As Long
        
        Me.cmdMergeToWord.SetFocus
        If Me!IsSelected Then
            
            wd = WindowDimensions
            With Me.cmdMergeToWord
                myLeft = (.Left + wd.WindowLeftTwips + 400) / TwipsPerPixelX
                mytop = (.top + wd.WindowTopTwips + 100) / TwipsPerPixelY
            End With
            SetCursorPos myLeft, mytop
            TempVars!SelectedDonationID = Me!DonationID.Value
            Me.cmdMergeToWord.SetFocus
            SendKeys "{Enter}"
        End If
    
    
    End Sub
    The wd structure in the code above simply contains various statistics about the Access window. So I calculated where the button was in screen coordinates, then moved the cursor there, set the focus to that button and clicked the button using SendKeys. That solved the problem, and the Word document moves to the front and stays there.

    I hate using SendKeys because it causes issues, but this time I couldn't think of an alternative. This code seems to work fine.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 05-14-2017, 10:07 AM
  2. Working query stops working after importing
    By Abacus1234 in forum Import/Export Data
    Replies: 3
    Last Post: 10-25-2015, 09:12 PM
  3. Replies: 1
    Last Post: 12-27-2014, 12:38 PM
  4. Replies: 3
    Last Post: 01-29-2013, 04:34 AM
  5. BringWindowToTop does not work with Access 2010
    By asterix48 in forum Programming
    Replies: 2
    Last Post: 12-07-2012, 10:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums