Results 1 to 8 of 8

Thread: SHELL out to excel

  1. #1

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200

    SHELL out to excel

    ok im am using access (vba) and i am opening excel.exe something like this:

    shell("address to find excel.exe then the file name to open",1)

    or something like that...but here is the problem when i pass it a file in a directory that contains a space in the name excell tries to open the wrong thing.

    example: if i opened excel in vb like:

    shell("C:\excellfolder\excel.exe C:\example folder\example.xls",1)

    then itsays this opon entering excel

    canot find "C:\example.xls"

    then cannot find "folder\example.xls"

    it splits it at the space but i do not want it to what can i do to keep it from splitting it at the space?

    p.s. i know the code example probably sucks but it is something like that.

    thanks ahead of time for the help!

  2. #2
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Shell can't handle spaces.

    You could convert everything to 8.3 naming, but shell still sucks!

    Use shellexecute instead.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" _
    4.     Alias "ShellExecuteA" _
    5.     (ByVal hwnd As Long, _
    6.     ByVal lpOperation As String, _
    7.     ByVal lpFile As String, _
    8.     ByVal lpParameters As String, _
    9.     ByVal lpDirectory As String, _
    10.     ByVal nShowCmd As Long) As Long
    11.  
    12. Private Const SW_SHOWNORMAL = 1
    13. Private Const SW_SHOWDEFAULT = 10
    14. Private Const SW_SHOWMAXIMIZED = 3
    15. Private Const SW_SHOWMINIMIZED = 2
    16. Private Const SW_SHOWMINNOACTIVE = 7
    17. Private Const SW_SHOWNA = 8
    18. Private Const SW_SHOWNOACTIVATE = 4
    19.  
    20. Private Sub Command1_Click()
    21.     Dim lngHandle As Long
    22.     Dim strFile As String
    23.     Dim strDir As String
    24.    
    25.     strFile = "C:\Documents and Settings\Carl\Desktop\Example.xls"
    26.     strDir = "C:\Documents and Settings\Carl\Desktop"
    27.    
    28.     lngHandle = ShellExecute(Me.hwnd, "Open", strFile, vbNullString, strDir, SW_SHOWNORMAL)
    29.    
    30.     If lngHandle <= 32 Then
    31.         MsgBox "Opening excel failed"
    32.     Else
    33.         MsgBox "Successful" & vbCr & vbCr & _
    34.                "The handle to the new excel window is " & lngHandle
    35.     End If
    36.  
    37. End Sub
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  3. #3

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    thanks for your help! Will this still work if i am in VBA? Just out of cureosity why do you check to see if it is <=32 ?

    thanks agian!

  4. #4
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    The Shell function parameter "pathname" takes an argument list separated by spaces (who knows why, but it does). The first argument is the application, the following arguments are the files to be opened by the application (separated by spaces, remember). So if the path name includes spaces, you must enclose the path name with double quotes. You can add double quotes inside a quotation by using two consecutive double quotes (""). So the code would be:
    VB Code:
    1. Shell """C:\excellfolder\excel.exe"" ""C:\example folder\example.xls""", 1
    Note that it is the same code with "" around each path name. (It isn't necessary for the application file path because it doesn't have spaces, but might as wel be consistenet.)

    Of course, that doesn't mean that it wouldn't be better to use ShellExecute, it is just what you need to do if you want to use the Shell function.

  5. #5

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    what is the advantage of shellexecute? what can you do with the handle that it returns?

    and example would be awesome! Thanks for the great responses!

  6. #6
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Originally posted by Buy2easy.com
    thanks for your help! Will this still work if i am in VBA? Just out of cureosity why do you check to see if it is <=32 ?

    thanks agian!
    To answer you question on ShellExecute: the function returns a long (as indicated in the declare) that is consistent with the return values of the FindExecutable API. Any value over 32 means success. Lesser values indicate an error (ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND, ERROR_BAD_FORMAT).

  7. #7
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Originally posted by Buy2easy.com
    what is the advantage of shellexecute? what can you do with the handle that it returns?

    and example would be awesome! Thanks for the great responses!
    As you can see from the code, there are more parameters that can be passed with ShellExecute. The function returns a long indicating success/errors. With ShellExecute you can more closely identify the error that occured.

    Most programmers seem to prefer ShellExecute, but they don't seem to really use the additional parameters available in ShellExecute (as you can see in the above example). I think it is just more popular because using the double quotes in Shell is a hassle. You just have to get used to using the double quotes in Shell and it should work fine.

  8. #8
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Originally posted by Buy2easy.com
    what is the advantage of shellexecute? what can you do with the handle that it returns?

    and example would be awesome! Thanks for the great responses!
    Once you have the handle, you can use other api calls to minimize, maximize, close the window, send keys to the window . . .

    I have had shell fail too many times. ShellExecute seems to work consistently, especially with unc paths.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

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