Opening a file using Shell() command


I am trying to open up a file using the shell command. The deal with the file is i will not know its file type/extension. When i open up a command prompt, i can just type in "lookatme.jpg" and the default viewer for .jpg opens up.

But, when i try

process_id = Shell("start " & Chr$(34) & file_name & Chr$(34))


process_id = Shell( file_name)

where file_name is "C:\temp\myfile.jpg", i am getting errors back.

Any thoughts on how to fix this / what i need to do?

Thanks a bunch
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Your problem here is that Shell can only open Executable files this includes .exe,.com,.bat extensions. You will need to use ShellExecute API to open other file formats

Option Explicit

Private Declare Function ShellExecuteW Lib "shell32" ( _
     ByVal hWnd As Long, _
     ByVal lpOPeration As Long, _
     ByVal lpFile As Long, _
     ByVal lpParam As Long, _
     ByVal lpDir As Long, _
     ByVal nShowCmd As Long) As Long

Public Sub myShell(ByVal wFileName As String)
     If ShellExecuteW(0&, StrPtr("Open"), StrPtr(wFileName), 0&, 0&, 1) > 32 Then
         Debug.Print "Success! Preparing to open file..."
     End If
End Sub

Private Sub Command1_Click()
    myShell "C:\WINDOWS\Web\Wallpaper\Wind.jpg"
End Sub
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
You also have questions involving .Net...

If you are working with VB.Net, then simply use the Process.Start() method:

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

darkxennAuthor Commented:
not using .NET, using 6.0

But, is there any way to get a process id, or process handle from shellexecute, so i can keep it in my shellandwait? I need to know when that file gets closed so i can kill it and then return a status to a calling app.

Thanks a bunch
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
You need to use ShellExecuteEx so you can wait on the process handle that gets returned into the SHELLEXECUTEINFO structure. Idle Minds link is the approach you need to accomplish that however if you have GUI or a form then that Form will freeze while your waiting.

This approach allows your GUI or Form to respond to mouse events, button events, etc. while still waiting


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.