Link to home
Start Free TrialLog in
Avatar of MacRena
MacRenaFlag for United States of America

asked on

VBA code to open a Text file from mapped drive

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.

thanks,
mac
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

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
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
Avatar of MacRena

ASKER

thanks, Jim.  that was exactly what i was needing.
thanks to you also, shanesuebsahakarn., but i'm going w/ the lesser coding.
mac
Avatar of pmcelhany
pmcelhany

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)