-
Im using a shell command to execute Microsoft Excel and open an Excel spreedsheet.
I've had to hard code the location of Excel, therefore if the user has installed Excel to somewhere other than 'C:\Program Files\Microsoft Office\Office' it fails.
Is there a way to find the location that Excel is installed on the users machine?
-
<?>
Code:
Option Explicit
'put this in a bas module
'
Public Declare Function ShellEx Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As Any, _
ByVal lpDirectory As Any, ByVal nShowCmd As Long) As Long
'
Sub ShellDef(strFileName)
x = ShellEx(Form1.hwnd, "open", strFileName, "", "", 1)
End Sub
'''''''''''''''''''''''''''''''''''''''
'form code
Option Explicit
Private Sub Form_Load()
'
Dim strYourFileVariable$
strYourFileVariable = "excel.exe"
ShellDef strYourFileVariable
End Sub
-
Or you could open Excel in this manner:
Code:
Private XL As Object
Private Sub StartExcel()
Set XL = CreateObject("Excel.Application")
XL.Visible = True
End Sub
Good luck!
-
Thanks for your help.
Both methods work but I need to pass an excel worksheet filename as well. By doing this excel starts and automatically loads the worksheet.
Can you help?
-
not at a point where I can test it to be sure, but I think what you need is this:
Code:
Dim RetVal
RetVall = Shell("Path to excel.exe", "Path to your .xls file), vbMaximizedFocus
-
sorry David I just reread your post and realize that's not what you need. It looks like you need to be able to return the pathname of excel.exe I also have tried to do such things with no luck. Anyone know the answer?
-
<?>
yes..the 2 first answers will do the trick.
-
You can use ShellExecute as HeSaidJoe suggested, just pass the path and file name of the workbook you want to open instead of Excel.exe.
Or you can use the automation I suggested:
Code:
Private XL As Object
Private Sub OpenWorkbook(Filename As String)
Set XL = CreateObject("Excel.Application")
XL.Workbooks.Open Filename
'or if you just want to open a new workbook:
'XL.Workbooks.Add
XL.Visible = True
End Sub
Good luck!
-
Many thanks to all.
I've got it working now!!
Cheers!
-
Neat Info!!!
I had the same problem as DavidBlack. I used the
RetVall = Shell("Path to excel.exe", "Path to your .xls file)
code but could not use long file names with spaces. It didn't like spaces in the filenames. (??)
Will try the code by Joacim.
-
<?>
If you have trouble with long names just use the replace funtion to remove the spaces.
String1 = "long name of the file desired"
String2 = Replace(String1, " ", "")
'string2 = longnameofthefiledesired
PS..In retrospect excel.exe was a bad example of using
shelldef...
-
You can't just remove the spaces from a filename can you?
You can however put quotation marks around the name or you could use the GetShortPathName API function:
Code:
Private Declare Function GetShortPathName _
Lib "kernel32" Alias "GetShortPathNameA" ( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Long) As Long
Public Function GetShortName(ByVal LongName As String) As String
Dim sShortName As String
sShortName = Space$(Len(LongName) + 1)
GetShortPathName LongName, sShortName, Len(sShortName)
GetShortName = Left$(sShortName, InStr(sShortName, vbNullChar) - 1)
End Function