Displaying a txt file

Posted on 1999-11-16
Medium Priority
Last Modified: 2010-05-02
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.

Question by:kurtis
  • 4
  • 3
  • 3
  • +2

Expert Comment

ID: 2212980
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.


Author Comment

ID: 2212996
Edited text of question.

Author Comment

ID: 2212997
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?

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.


Accepted Solution

marcj1 earned 400 total points
ID: 2213024
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


Expert Comment

ID: 2213416
>> 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.

Expert Comment

ID: 2213545
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 = ""

Expert Comment

ID: 2213717
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!

Expert Comment

ID: 2213928
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 ;-)

Expert Comment

ID: 2213965
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

Expert Comment

ID: 2214123
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')

Author Comment

ID: 2215755
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.

Author Comment

ID: 2215762
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.

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month3 days, 15 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question