# Open Workbook With ShellExecute

• Hi,

I am trying to using the following method to open an Excel workbook (2008.xls) which did not work.

Code
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub OpenWB

ShellExecute(0, "Open", "C:\2008.xls", vbNullString, vbNullString, 1)

End Sub

After running the procedure OpenWB, Excel will halt and the only way I can close Excel is through the windows task manager. By the way, I have applied service pack 3 for Excel.

Regards.

• Re: Open Workbook With ShellExecute

Why are you using the shell command?
Why not Workbooks.Open

[h4]Cheers
Andy
[/h4]

• Re: Open Workbook With ShellExecute

I am trying to find a method that I can use to open any file types. Then I came across the ShellExecute which seem like the ideal solution. So far, it worked with other file types (e.g *.txt) and I thought it should worked for *.xls file as well.

I can use the Workbooks.Open method but I wish to know why ShellExecute cannot be used on xls file. Just thought that it may be interesting to know.

Regards.

• Re: Open Workbook With ShellExecute

Try this modification.

[vba]
Private Sub OpenWB()

ShellExecute Application.hWnd, "open", "C:\2008.xls", vbNullString, vbNullString, 1

End Sub
[/vba]

[h4]Cheers
Andy
[/h4]

• Re: Open Workbook With ShellExecute

Thanks, Andy. It worked.

Just a slight issue. Can I open 2008.xls in an exisitng Excel window instead of opening another instance of Excel again?

• Re: Open Workbook With ShellExecute

I'm not sure if I understood you correctly, but the way I read your post you just want a common method to open any kind of file. Instead of ShellExecuting the file, you could simply abstract away the logic that is necessary to open any type of file.

This way you could avoid using the kludgy ShellExecute API when not absolutely necessary, and it would give you more freedom in handling special cases. And you could still open any file by calling

Code
OpenFile "C:\path\to\file.ext"
• Re: Open Workbook With ShellExecute

Hi Fencliff,

Your code is working great! Thanks!

Regards.

## Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!