[AccessD] Word Window On Top

Heenan, Lambert Lambert.Heenan at AIG.com
Wed Aug 23 14:48:20 CDT 2006


Well here is how I do the same thing with Access opening Excel. It is all
dependant on getting the handle of the Excel window (or Word window in your
case)...

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

Sub ViewInExcel(strRecordSource As String, Optional bFormatColumns As
Boolean = True)
'---------------------------------------------------------------------------
------------
' Procedure : ViewInExcel
'
' Purpose   : Displays a recordset (Table or query) in an Excel worksheet,
but does
'           : not save the Excel file. The user can choose to save the file
if needed.
'---------------------------------------------------------------------------
------------
'
Dim objXL As excel.Application
Dim objSheet As excel.Worksheet
Dim objWB As excel.Workbook
Dim rs As Object
Dim oRng As excel.Range
Dim nCols As Long, n As Long

    Set rs = CurrentDb.OpenRecordset(strRecordSource)
    nCols = rs.Fields.Count
    Set objXL = CreateObject("Excel.Application")
    objXL.SheetsInNewWorkbook = 1
    Set objWB = objXL.Workbooks.Add
    Dim f As Field
    n = 0
    ' CopyFromRecordset does not bring the column headings with it.
    ' So here we add them manually.
    For Each f In rs.Fields
        n = n + 1
        objWB.Sheets("Sheet1").Cells(1, n) = f.Name
    Next f
    ' and now drop the data into row 2
    objWB.Sheets("Sheet1").Cells(2, 1).CopyFromRecordset rs
    If bFormatColumns Then
        Set oRng = objWB.Sheets("Sheet1").Range(CStr(ExcelColumn(1)) & "1:"
& CStr(ExcelColumn(nCols)) & "1")
        oRng.EntireColumn.AutoFit
        oRng.Interior.Color = vbYellow
        oRng.Borders.Color = vbBlack
        Set oRng = Nothing
    End If
    rs.Close
    Set rs = Nothing
    objXL.Visible = True
    SetForegroundWindow objXL.hWnd ' here's the line that brings the window
to the foreground.
End Sub

HTH

Lambert
-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Rocky Smolin -
Beach Access Software
Sent: Wednesday, August 23, 2006 1:33 PM
To: Access Developers discussion and problem solving
Subject: [AccessD] Word Window On Top


Dear List:

Using automation I open a word doc, but the access app is still on top.  
Word is in the tray.  Click it and the doc is there and open.  Q: how 
can I give the word doc window the focus after I open it?

MTIA

Rocky

-- 
Rocky Smolin
Beach Access Software
858-259-4334
www.e-z-mrp.com

-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com



More information about the AccessD mailing list