Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ShellExecute Problem 1

Status
Not open for further replies.

Scoty

Programmer
Oct 25, 2000
278
US
Hey All,
I have been charged with creating a program that will be used with a command line to launch a "Document" out of our online library system. Now this document could be any extension I.e.: doc, xls, txt, htm, ect. I have found that using ShellExecute takes care of a majority of the file types however I have hit a snag when it comes to excel. When ever I launch and xls it get the error that says "<File Name>.xls could not be found. Check the spelling of the file name, and verify that the file location is correct"...blah, blah, blah. I am not sure why this happening. I have tried using the short path function API to give the true dos path but I get the same results.

Has anyone else had this problem? And if so what did you do to resolve it? I would rather not hard code around this, although that may be my only option

thanks in advance,
Scoty ::)
 
What happens if you double-click an Excel file from Explorer?
Perhaps you could check if your file-associations are correct.
Have you tried on another machine?

Did some checking...

If the lpFile parameter is the full path to the Excel file, then everything's OK (e.g., "I:\Documents\6600 files\Copy of Attendance.xls").

If the lpFile parameter is just the basename (e.g., "Copy of Attendance.xls"), then the lpDirectory must be the folder where the file is (e.g., "I:\Documents\6600 files").

Are you using absolute paths or relative paths?
 
Thanks for your response.

When I double click from explorer it just opens.
The file-association is correct.

I always split out the lpFile and lpDirectory.

As I stated it works for every other file type except excel.

Interesting Fact:
When I went to hard code around for xls file types had a tough time with "Enable Macros" junk.

Finally ended up using the excel com object to open the excel files.

Thanks
Scoty ::)
 
Can we have a look at your implementation of ShellExecute?
 
strongm,
Here is all my code as of today (one module vb program).

Code:
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
                   (ByVal hwnd As Long, ByVal lpszOp As String, _
                    ByVal lpszFile As String, ByVal lpszParams As String, _
                    ByVal LpszDir As String, ByVal FsShowCmd As Long) _
                    As Long


Sub Main()
On Error GoTo ErrorTrap
Dim SplitOut() As String
Dim AppPath As String
Dim objExcelApp As New Excel.Application

Select Case Left(Right(Command, 4), 3)
    Case "xls", "XLS", "Xls" 'this is to open all excels
        myDir = Replace(Command, """", "")
        objExcelApp.Workbooks.Open FileName:=myDir
        objExcelApp.Visible = True
        Set objExcelApp = Nothing
    
    Case Else'this opens everything else
        Dim Ret As Long
        SplitOut = Split(Command, "\", , vbTextCompare)
        For I = 0 To UBound(SplitOut) - 1
            If I = 0 Then
                AppPath = SplitOut(I)
            Else:
                AppPath = AppPath & "\" & SplitOut(I)
            End If
        Next I
        AppPath = AppPath & "\"
        
        AppPath = Replace(AppPath, """", "", 1, , vbTextCompare)
        SplitOut(UBound(SplitOut)) = Replace(SplitOut(UBound(SplitOut)), """", "", 1, , vbTextCompare)

        Ret = ShellExecute(0, "", SplitOut(UBound(SplitOut)), "", AppPath, 1)
        If Ret = 31 Then
            MsgBox "No file association found for " & Command, vbOKOnly
        End If
End Select

Exit Sub
ErrorTrap:
    MsgBox Err.Number & " - " & Err.Description
End Sub

I also make a reference to Microsoft Excel 10.0 Object Library.
 
Ah - no, I wanted to see your original version to see if I could spot what was going wrong, not your workaround. I'm guessign however, that the second part of your case statement is your original implementation

It might just be worth pointing out that "" is not the same as a genuine null string
 
Couple of other points, since we're here: ShellExecute will set the default directory to the path of the document being opened if you don't specify it yourself. All that work to get AppPath is therefore not really necessary. And ShellExecute handles quotes, so you don't need to replace them
 
They are indeed - although to be fair I wan't trying to give string handling hints ...
 
strongm,
Yes the case else is the original implementation. I was unaware of ShellExecute's ability to assign the path with out splitting out the path, so that is noted for future. Although I must point out that the quotes that were passed in with command were causing issues, that is why I removed them.

Thanks for input.

Scoty ::)
 
Yep, they would cause problems to your parsing routine - but you don't really need the parsing routine. Indeed, your entire Case Else can be replaced with:
Code:
[blue]
If ShellExecute(0&, vbNullString, Command, vbNullString, vbNullString, 1) = 31 Then MsgBox "No file association found for " & Command, vbOKOnly[/blue]
Note that this works fine with Excel files on my PC
 
strongm,
Once again, I stand in awe.

Thanks you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top