Results 1 to 9 of 9

Thread: [RESOLVED] Shell .txt or non .exe files

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    51

    Resolved [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.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Shell .txt or non .exe files

    Quote Originally Posted by Manix View Post
    ... 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.

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    51

    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

  4. #4

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    51

    Re: Shell .txt or non .exe files

    Oh It worked.. Thanks +rep

  6. #6
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    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.

  7. #7
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    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??

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    51

    Re: [RESOLVED] Shell .txt or non .exe files

    Quote Originally Posted by CDRIVE View Post
    ... then passing parameters to the .txt file of your choice.
    How do I do that?

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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
  •  



Click Here to Expand Forum to Full Width