Displaying a txt file

I wish to open up a txt file in WordPad (or Notepad) from my Access/VB application.  How do I go about doing this?  I am looking for the same functionality that you get when you double-click a file in Windows Explorer... the file gets loaded with the application that it is associated with.  I wish to get the same functionality here.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Private Sub Command1_Click()
    Dim n As Long
    Dim strApp as string
    Dim strFile as string
    Dim nStyle As VbAppWinStyle

    strApp = ""
    strFile = ""
    nStyle =

    n = Shell(strApp & " " & strFile, nStyle)
End Sub

strApp is the path to and file name of WordPad, Notepad, etc

strFile is the path to and file name of the file you wish to open.

nStyle is an enum that will prompt you for a valid window style constant, ie minimized, maximized, etc AND IS OPTIONAL.

kurtisAuthor Commented:
Edited text of question.
kurtisAuthor Commented:
Thanks for your quick response, but please note the part of my question that is: "I am looking for the same functionality that you get when you double-click a file in Windows Explorer...".  Also, what if an individual's WordPad is not installed in the same place?

The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Give this one a go...given a file of a registered type, this will load it.

'------DECLARES SECTION-------
Option Explicit

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

'hWnd           the window who becomes parent of the spawned process
'lpOperation    one of open, print, explore
'lpFile         the document (is this case) or application to launch
'lpParameters   params for lpFile if and Application, if a document(as shown)--0&
'lpDirectory    the default directory of the spawned process, as shown--0&
'nShowCmd       one of the constants that follows--there are others, but these are
'                the one's of primary interest

Private Const SW_SHOWNORMAL = 1         'based on it's creation style bit
Private Const SW_SHOWMINIMIZED = 2      'opens the window minimized and activated
Private Const SW_SHOWMAXIMIZED = 3      'opens the window maximized and activated
Private Const SW_SHOWMINNOACTIVE = 7    'opens the window minimized and leaves the
                                        'current active window active


Private Sub Command1_Click()
    Dim dbl As Double

    dbl = ShellExecute(Me.hwnd, "open", "C:\Data\Excel8\money.xls", 0&, App.Path, SW_SHOWNORMAL)
End Sub


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
>> Also, what if an individual's WordPad is not installed in the same place

That's why we have file-associations. When you Shell or ShellExecute a file, exactly the same thing happens as when you double-click it in Explorer, it gets opened with the associated application, no matter where that is physically. So don't Shell "Notepad.exe Myfile.txt", but simply Shell "Myfile.txt". It will then open with the vieuwer you associated with .txt files. For some this is notepad, for others it's WordPath or Ultra-edit or whatever.
You might also want to check the return value of the ShellExecute command to see if something went wrong.. here's the return codes that is possible and their corresponding error messages..

  Case 0
    Run = "Insufficent system memory or corrupt program file"
  Case 2
    Run = "File not found"
  Case 3
    Run = "Invalid path"
  Case 5
    Run = "Sharing or Protection Error"
  Case 6
    Run = "Seperate data segments are required for each task"
  Case 8
    Run = "Insufficient memory to run the program"
  Case 10
    Run = "Incorrect Windows version"
  Case 11
    Run = "Invalid program file"
  Case 12
    Run = "Program file requires a different operating system"
  Case 13
    Run = "Program requires MS-DOS 4.0"
  Case 14
    Run = "Unknown program file type"
  Case 15
    Run = "Windows program does not support protected memory mode"
  Case 16
    Run = "Invalid use of data segments when loading a second instance of a program"
  Case 19
    Run = "Attempt to run a compressed program file"
  Case 20
    Run = "Invalid dynamic link library"
  Case 21
    Run = "Program requires Windows 32-bit extensions"
  Case Else
    Run = ""
JeremyD--after kurtis shot down my shell proposal, i tried executing a shell against and Excel8 document(without specifying an app) and got "invalid procedure call[s]..." as a result.  Though I was pretty sure that I'd done that in the past.  Hence the reason I proposed the ShellExecute...it worked against E8...are you certain that the Shell works against binaries when you don't specify the app?

VBMaster--excellent point, good bit of info too!
Marcj1: I *was* pretty sure about it. Just tried it from my debug-window, and it doesn't work. MSDN mentions 'an executable file', so I guess it can't be done with Shell.
I probably forgot because I allways use ShellExecute, and I probably allways use ShellExecute because with Shell the associations won't work :-( Kind of a downward spiral of memory-leaks I guess...

Anyway, after VBMaster's info Kurtis probably doesn't want to use Shell anymore either ;-)
There's a way to launch whatever file you like with the Shell command.  How it is done exactly, I forgot ....

But it comes down to this ....

When you press START, RUN and you type in the file, it also launches the right app for it.

It's something like

Shell "Run myFile.txt"
Shell "Start myFile.txt"

ShellExecute works fine, of course
You can use "start myfile.txt" to get the trick done. The problem is it opens a dos-box to execute the 'start'-command, which doesn't really add to the stability of your app, methinks (I try to avoid 16-bits drivers and apps whenever possible).
Also, consider this: all standard windows apps (like notepad) add a path to their executable to the registry. On a normal windows box you can allways type "notepad myfile.txt", no matter where you are, and it will execute fine. Only people who manually fiddle with their registry (I would be one of those ;-) could get problems when you use this assumption, but those people are probably capable of adding the app-path for notepad to the registry again, so it couldn't give to much problems if you simply assume that "notepad myfile.txt" will work on all systems.
The best answer is of course still to use ShellExecute. It will give you some more flexibility AND is the correct answer to the question ('the same functionality that you get when you double-click a file in Windows Explorer')
kurtisAuthor Commented:
Thank you for all of the responses.  I guess I should have mentioned at the start that I knew about "Shell", but I was unable to get it to work how I wanted to (as others found out with a .txt file).  However, ShellExecute is one that I have never used before, until now.

Thanks again.
kurtisAuthor Commented:
By the way, is there a way to provide additional functionality (like Windows Explorer) that will prompt for what application should start a file type if it is not registered.  For example, if I have a file with extension .jan and it is not associated with anything, the return value is 31 and it does nothing; however in Windows explorer the same sort of thing would result in a "Open with" dialog to open.  Is this possible here?  If someone has an answer to this it is worth another 100 points to me.

....OK, after typing in all of the above, I decided I will post another question out there as to how this would be accomplished.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.