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...