VBA code to open a Text file from mapped drive

Posted on 2004-09-07
Last Modified: 2006-11-17
hello experts,
what is the VBA code to open a Text file from a mapped drive?

i am recording receipt Numbers that the user looks at in a flat text file, and i want to have a button on the form that will open that file up in Notepad if the user needs to look at her history.

in otherwords, if the user clicks the button, i want my code to execute Notepad.exe and open up the .txt file on the user's workstation.

Question by:MacRena
LVL 65

Accepted Solution

Jim Horn earned 100 total points
ID: 12000590
Throw the below two lines of VBA code behind your button.OnClick

Dim dbl As Double
dbl = Shell("c:\windows\notepad.exe {your file.txt}", vbMaximizedFocus)

...where {your file.txt} is the path/file of your text file, without the { }'s.

Hope this helps.
LVL 41

Expert Comment

ID: 12000602
To open a specific text file, assuming you have the path in variable strPath and file in strFile:

First paste this into a new module:
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

Now call it with:
ShellExecute hwnd,"open",strFile,Chr$(0),strPath,False

Author Comment

ID: 12000827
thanks, Jim.  that was exactly what i was needing.
thanks to you also, shanesuebsahakarn., but i'm going w/ the lesser coding.

Expert Comment

ID: 12000937
This works for me.  We use the H drive as our personal storage space on our net.
I created a small file(TextDoc.txt) and tried calling it.

    Dim stAppName As String

    stAppName = "C:\Windows\System32\Notepad.EXE H:\TextDoc.txt"
    Call Shell(stAppName, 1)

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records ( and Building a Custom Filter ( — discuss in some detail how a form can be…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

777 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