|
-
Nov 24th, 2010, 04:59 PM
#1
Thread Starter
Member
[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.
Last edited by Manix; Nov 24th, 2010 at 05:25 PM.
-
Nov 24th, 2010, 05:03 PM
#2
Re: Shell .txt or non .exe files
 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.
-
Nov 24th, 2010, 05:07 PM
#3
Thread Starter
Member
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
-
Nov 24th, 2010, 05:18 PM
#4
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).
-
Nov 24th, 2010, 05:24 PM
#5
Thread Starter
Member
Re: Shell .txt or non .exe files
Oh It worked.. Thanks +rep
-
Nov 25th, 2010, 08:08 PM
#6
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
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
-
Nov 26th, 2010, 10:47 PM
#7
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.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Nov 27th, 2010, 04:25 AM
#8
Thread Starter
Member
Re: [RESOLVED] Shell .txt or non .exe files
 Originally Posted by CDRIVE
... then passing parameters to the .txt file of your choice.
How do I do that?
-
Nov 27th, 2010, 04:48 AM
#9
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|