Launch a text file

T

Toby

Hi
How do you launch a text file using a button in VBA?
I want to click on a button to open a log file - I would
actually like to open a dialog box that starts up in a
particular directory (eg C:\logfile) - the user can then
double click on a txt file which then opens in notepad.
Thanks
 
J

John Nurick

Hi Toby,

There's code at http://www.mvps.org/access/api/api0001.htm to display
the standard File Open dialog so the user can choose a file. Then pass
the filespec returned by the dialog to Application.FollowHyperlink() to
open it. Something like this, oerhaps:

Private Sub cmdOpenTextFile_Click()
Dim strFilter As String
Dim strInputFileName as string

strFilter = ahtAddFilterItem(strFilter, "Text Files (*.TXT)", "*.TXT")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)
If Len(strInputFileName) > 0 Then
Application.FollowHyperlink strInputFileName
End If
End Sub
 
J

John Nurick

My recommendation would be to use the ShellExecute API call, in case the
user would rather have the text file open in some other editor. There's a
complete sample of how to use that API at "The Access Web" as
http://www.mvps.org/access/api/api0018.htm

AIUI the functional difference between this and
Application.FollowHyperlink only shows up if the file type is not
registered, when ShellExecute lets the user select an application to
open the file with, while FollowHyperlink produces error 486, "no
program is registered to open this file".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top