Using Shell to open Excel file

S

scott

I've been using the below sub within Access to launch a specific Excel
document that is linked to data from the current spreadsheet. As you can see
below, I've hard-code the path to excel.exe when using the "Shell" command
to open Excel.

I also have references to 2 versions of Excel and 2 different file locations
in case MS Office was installed in the "d:\program files\microsoft office\"
folder.

Is there a way to use windows's built-in variables and allow my code to be
written like %program files\microsoft office% so that my code will open
Excel, no matter if office is installed on c or d drive? Also, without
knowing where MS decides to place future versions of Office app files, could
the "%" variable-style syntax be written so the shell command will open
Excel, regardless of the installed version on the user's pc?



CODE: *****************

Sub LaunchExcel()

Dim MyFile
' Returns "excel.exe" if it exists.

MyFile = Dir("c:\Program Files\Microsoft Office\Office10\excel.exe")

If Len(MyFile) <> 0 Then

If MyFile = "excel.exe" Then
Call Shell("c:\Program Files\Microsoft Office\Office10\excel.exe
""C:\Data\myExcelFile.xls""", 1)
Else
Call Shell("d:\Program Files\Microsoft Office\Office10\excel.exe
""C:\Data\myExcelFile.xls""", 1)
End If

Else

MyFile = Dir("c:\Program Files\Microsoft Office\Office11\excel.exe")

If MyFile = "excel.exe" Then
Call Shell("c:\Program Files\Microsoft Office\Office11\excel.exe
""C:\Data\myExcelFile.xls""", 1)
Else
Call Shell("d:\Program Files\Microsoft Office\Office11\excel.exe
""C:\Data\myExcelFile.xls""", 1)
End If

End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top