Link to home
Start Free TrialLog in
Avatar of kurtis
kurtis

asked on

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.

Thanks
Avatar of marcj1
marcj1

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.



Avatar of kurtis

ASKER

Edited text of question.
Avatar of kurtis

ASKER

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?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of marcj1
marcj1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start 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"
or
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')
Avatar of kurtis

ASKER

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.
Avatar of kurtis

ASKER

marcj1,
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.