How do I use a command button to open an excel file?

J

Justin

I am trying to open a specific excel file using a command button in a form.
I have tried several steps but keep getting an error.
 
K

Ken Sheridan

Justin:

There are various ways you could do it. If you want to manipulate the
contents of the workbook from within Access you'd use automation so that you
can return a reference to the workbook in your Access application. If you
just want to open the Excel file, however, you can use the VBA Shell function
(check it out in Help). Or you can call the Widows API ShellExecute
function. I find this last copes with many 'associated' file types which the
Shell function doesn't handle well. Put the following module in your
application:

''''module starts''''
Option Compare Database
Option Explicit

Declare Function ShellExecute& Lib "shell32.dll" Alias "ShellExecuteA"
(ByVal _
hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal _
lpParameters As String, ByVal lpDirectory As String, ByVal nshowcm As Long)

Sub ShellToFile(strPath As String, ByVal lngHwnd As Long)

Dim lngRetVal As Long

lngRetVal = ShellExecute(lngHwnd, "open", strPath, _
vbNullString, CurDir, 1)

If lngRetVal < 32 Then
MsgBox "Unable to open file " & strPath, vbInformation, "Warning"
End If

End Sub
''''module ends''''

You can then call it in the button's Click event procedure with:

Dim strPath As String

strPath = < get the path to the Excel file from somewhere>

ShellToFile strPath, hwnd

Ken Sheridan
Stafford, England
 
F

fredg

I am trying to open a specific excel file using a command button in a form.
I have tried several steps but keep getting an error.

Easiest way is to code the Command Button click event:
Application.FollowHyperlink "PathToFolder\SpreadsheetName.xls"
 
Top