Results 1 to 12 of 12

Thread: Find excel.exe

  1. #1
    Guest
    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?


  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    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
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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!

  4. #4
    Guest
    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?

  5. #5
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Columbia, SC USA
    Posts
    374
    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

  6. #6
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Columbia, SC USA
    Posts
    374
    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?

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    yes..the 2 first answers will do the trick.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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!

  9. #9
    Guest
    Many thanks to all.

    I've got it working now!!

    Cheers!

  10. #10
    Junior Member
    Join Date
    Sep 2000
    Posts
    20
    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.

  11. #11
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    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...
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width