[RESOLVED] Shell .txt or non .exe files
Hey again.. As you probably have already figured out I'm having trouble with shell-ing a non .exe file, basically I need to make a program open a .txt file for the user and I read a couple of threads saying I should use ShellExecute and some other kind of functions and stuff and everything I tried failed.. I'm going to be really greatful if someone could assist me with my issue.
Re: Shell .txt or non .exe files
Quote:
Originally Posted by
Manix
... I read a couple of threads saying I should use ShellExecute and some other kind of functions....
ShellExecute is the right thing to do. Show us how you did it - we will correct what's necessary.
Re: Shell .txt or non .exe files
Well the last thing I tried and is still in my program is this
some code...
ShellExecute 0&, vbNullString, "C:\Documents and Settings\Admin\Desktop\Readme.txt", vbNullString, vbNullString, vbNormalFocus
some code..
and it gives me Sub or Function not defined..
If I declare the function it says "only comments can be placed after end sub, end function" no matter where I place it in the code
Re: Shell .txt or non .exe files
ShelExecute is windows api function and must be declared in the General section as Private inside your form or in the module (with Public scope so it can be reused elsewhere).
Re: Shell .txt or non .exe files
Oh It worked.. Thanks +rep
Re: [RESOLVED] Shell .txt or non .exe files
Its already been explained but I would forget using Shell on anything, you can only use it with *.bat, *.com and *.exe files.
ShellExecute is one of the most powerful API's you can use in VB. You can Open any file thats registered in Windows, Print and file and Email anyone you want too. ;)
Code:
'Open
ShellExecute Me.hwnd, "open", "C:\Documents.exe", vbNullString, vbNullString, vbNormalFocus
ShellExecute Me.hwnd, "open", "C:\Readme.txt", vbNullString, vbNullString, vbNormalFocus
'Print
ShellExecute Me.hwnd, "print", "C:\Readme.txt", vbNullString, vbNullString, vbNormalFocus
'Email
ShellExecute Me.hwnd, "open", "mailto:" & "[email protected]" & "?subject=" & "your subject", vbNullString, vbNullString, vbNormalFocus
Re: [RESOLVED] Shell .txt or non .exe files
You can indirectly Shell a .txt file by Shelling Notepad, which is an .exe and then passing parameters to the .txt file of your choice. As an alternative you can also Shell Explorer.exe and basically do the same thing.
Re: [RESOLVED] Shell .txt or non .exe files
Quote:
Originally Posted by
CDRIVE
... then passing parameters to the .txt file of your choice.
How do I do that?
Re: [RESOLVED] Shell .txt or non .exe files
With Notepad you can do it like this:
Code:
Shell "NotePad ""C:\folder\file.txt"" "
(the "" are to put one double-quote into the string)
However, using this method assumes that Windows knows how to find the program (it should for Notepad, but not for most programs), and that the program is the right one to use (for example, many people use a different program to open text files). Both of those issues can be dealt with, but in a far more complex way than just using ShellExecute.
If you use the ShellExecute method, you only rely on the file type being registered on the computer (which basically means Windows has been told at some point which program to use for the file type), which is very likely to be the case for many file types (especially .txt / .bmp / .jpg / .doc / etc). You can check the result of the ShellExecute function to see if it succeeded, eg:
Code:
If ShellExecute (0&, vbNullString, "C:\Documents and Settings\Admin\Desktop\Readme.txt", vbNullString, vbNullString, vbNormalFocus) <= 32 Then
MsgBox "oops, couldn't open the file!"
End If
(if you look at the ShellExecute documentation, you will see that 32 or less means it failed)