Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening Word Document

Status
Not open for further replies.

dongle

IS-IT--Management
May 31, 2001
78
GB
Any one know of an easy way to open a specific Word document using a button on an Access Form
 
Hi Dongle,

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 [pipe]

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

DisplayReport_Err:
MsgBox Err.Description
Resume DisplayReport_Bye

End Sub
 
Thanks. I will give this a try and let you know.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top