I use the following code to open an Excel report, passing the report path (in the format "C:\temp\file.xls")to the routine .... it is very similar for Word I believe.
Make sure you make a reference to the Excel object library for this one .. and the Word object library for yours.
Cheers,
The Creator
Create a new module and paste this into it -
Option Compare Database
Option Explicit
' Most of this is courteous of the Microsoft Knowledge Bsse -
Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, _
ByVal wCmd As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String, ByVal CCh As Long) _
As Long
Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) _
As Long
Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
' This function returns the Caption Text of each window passed to
' it. If a window does not have a Caption bar, then this function
' returns a zero-length string ("")
Function GetAppName(Lnghwnd As Long)
Dim LngResult As Long
Dim StrWinText As String * 255
Dim LngCCh As Long
LngResult = GetWindowText(Lnghwnd, StrWinText, 255)
GetAppName = Left(StrWinText, LngResult)
End Function
' This function counts all instances of an application that are open,
' including any windows that are not visible.
' Arguments: LngHwnd = Any valid window handle.
' StrAppCaption = The window caption to search for.
' Example: GetCountOfWindows(hWndAccessApp,"Microsoft Access")
Function GetCountOfWindows(Lnghwnd, StrAppCaption)
Dim LngResult As Long
Dim LngICount As Long
Dim StrAppName As String
LngResult = GetWindow(Lnghwnd, GW_HWNDFIRST)
Do Until LngResult = 0
If IsWindowVisible(LngResult) Then
StrAppName = GetAppName(LngResult)
If InStr(1, StrAppName, StrAppCaption) Then
LngICount = LngICount + 1
End If
End If
LngResult = GetWindow(LngResult, GW_HWNDNEXT)
Loop
GetCountOfWindows = LngICount
End Function
Sub DisplayReport(filename)
' **********************************************************
' Requires Reference to Microsoft Excel 8.0 Object Library
' **********************************************************
On Error GoTo DisplayReport_Err
' accepts a file name from the calling function and opens that report in Excel
Dim xlApp As Excel.Application
Dim xlBook As Object
Dim xlSheet As Object
' if Excel is already open, use this instance otherwise create a new instance
If GetCountOfWindows(hWndAccessApp, "Microsoft Excel") > 0 Then
Set xlApp = GetObject(, "Excel.Application")
Else
Set xlApp = CreateObject("Excel.Application")
End If
' make Excel active window and display workbook
xlApp.Application.Visible = True
Set xlBook = xlApp.Workbooks.Open(filename)
xlBook.ActiveSheet.Range("A2").Select
DisplayReport_Bye:
' release objects
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
Exit Sub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.