-
I want to be able to open a file from within my VB app, based on the file's extension (e.g. if the file is file.doc,the file would open in Word). This is basically the same behavior you get if you click on a file who has his extension registered in the file types.
Shell and CreateProcess both require the app name (in my example above, I'd have to use 'word.exe file.doc'). I'd like a call that would simply tell Windows to open the file with the app that it thinks should be used with that file.
Any ideas? Thanks.
-
Use the ShellExecute API, which uses the associated application to open a file.
http://www.vbapi.com/ref/s/shellexecute.html
-
'Yes, you need to use the ShellExecute API.
'The declaration is:
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
'Constants which are used to determine the state of the app when it loads...
Private Const SW_SHOWNORMAL = 1
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_SHOWNOACTIVATE = 4
Private Const SW_SHOWMINNOACTIVE = 7
Private Const SW_SHOWNA = 8
Private Const SW_SHOWDEFAULT = 10
'Then call it in code like this:
ShellExecute hwnd, "open", "http://www.vb-world.net", "", "", SW_SHOWNORMAL
'The last variable can be replaced with any of the constants above. It can be used to open websites with the default browser, and email messages with the current email program, or replace with a path and filename to open a document with its' associated application.
'Enjoy.
-
Thanks, both JFrench and Seawood, your advice was exactly what I needed. I can't believe I overlooked ShellExecute before.