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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sigh opening file 1

Status
Not open for further replies.

MrMoocow

Programmer
May 19, 2001
380
US
I feel dumb asking but how do you open a file wiht is defualt viewer I tried

shell (filename)

didn't work

whats the proper way to open a file with its default program. Brad,
Free mp3 player,games and more.
 
The fact that windows opens a .txt file with notepad has to do with MIME type (I think), double clicking a .txt file opens the text file in notepad.
As far as I know putting the .txt file as an argument for shell does not work.
If you start notepad like this:
notepad.exe "c:\text.txt"
a file text.txt will be opened by notepad (if it does not exist notepad will ask you if you want to create it).
You can use this allso in your shell command (see code below).

Dim filename As String
filename = "c:\test.txt"
Shell "notepad " & Chr$(34) & filename & Chr$(34), vbNormalFocus

the Chr$ is a " sign wich you need if your path or .txt file has got a space in it (like c:\some dir\my text file.txt)
 
This is not a dumb question.It is a "Do you know it, go ahead. if you don't ask" question. :)

You have to use the ShellExecute API.
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

Search in MSDN for the topic:
"HOWTO: Use ShellExecute to Launch Associated File (32-bit)"
to acomodate yourself with the function.

Hope this helps, s-)

Blessed is he who in the name of justice and good will, shepards the week through the valley of darkness...
 
I got it to work so.. Thank you all, now another quick question.

It when I select one file to open, it does (Paint SHop Pro Loads) but then the program disapears, when I select another file now psp shows up with both files loaded. How do I get a program 2 just load with out any thing needed further from that program. I just want it ro open and then what the user does with it is up to (he|she).


Thanks

Brad,
Free mp3 player,games and more.
 
Just choose the convenient FsShowCmd.
It should work. If you have problems check the return value of the function to find out more.
{see MSDN sysntax of the function}

If you want more power from this function consider also ShellExecuteEx, but this is not so handy to use.

{Or maybe I didn't understand your question right?}

Hope this helps,

s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Just one more little question, when the chosen program is executed it is totally indepentatn of my app right? Cuz my app is just a file browser. Brad,
Free mp3 player,games and more.
 
Yes, is totally independent.
However if you want to know when the application has finished you should observe that ShellExecute returns a handle (HINSTANCE, can be mapped to Long type from VB).

This argument you can then pass to a call to another API function WaitForSingleObject

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _hHandle As Long, ByVal dwMilliseconds As Long) As Long

The call should be :
WaitForSingleObject(hMyInstance,INFINITE) 'INFINITE is a constant

Also you have an example off coordinating Apps in the article:
"HOWTO: 32-Bit App Can Determine When a Shelled Process Ends" which usess CreateProcess instead ShellExecute.

Hope this helps, s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top