Open text file as read-only

Open text file as read-only

Anonymous
Not applicable
1,021 Views
4 Replies
Message 1 of 5

Open text file as read-only

Anonymous
Not applicable
Hi all! I was wondering if there's a way, using ShellExecute, to open a file (now here's where it gets tricky) in Notepad with an extension of EXE as read-only. This "dummy" executable file contains strictly text. -- Matt W There are 3 kinds of people: Those who can count, and those who can't.
0 Likes
1,022 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
The ShellExecute API by it's nature works out what program to run based on the extension of the document, so you wouldn't be able to use that. You would have to use the Shell statement instead and probably have to provide the path to Notepad yourself.

As for read-only, that is indeed tricky. There is no native way to do this with Notepad. The only alternatives I can think of are to use some other (more sophisticated) editor that lets you pass a command-line parameter to indicate the file should be opened in read-only mode, or to change the permissions of the file itself to read-only prior to shelling the editor. The trick is knowing when to change the permissions back. A better way might be to make a temporary copy of the file and make that read-only instead.

Regards

Wayne Ivory
IT Analyst Programmer
Wespine Industries Pty Ltd
0 Likes
Message 3 of 5

Anonymous
Not applicable
I ran with Wayne's ideas to create the following code. The code sets the file attributes to readonly, opens the file in notepad, uses some API functions to hold control until the user closes notepad and then sets the file attributes to normal.
**********
Option Explicit
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32.dll" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long

Public Sub HoldControl()
Dim dwProcessId As Long
Dim hProcess As Long
Dim lpExitCode As Long
Dim intDoEvents As Integer
SetAttr "C:\Set\Test.exe", vbReadOnly
dwProcessId = Shell("C:\WINNT\NOTEPAD.exe C:\Set\Test.exe")
hProcess = OpenProcess(&H400, False, dwProcessId)
lpExitCode = &H103&
Do While lpExitCode = &H103&
Call GetExitCodeProcess(hProcess, lpExitCode)
intDoEvents = DoEvents
Loop
Call CloseHandle(hProcess)
SetAttr "C:\Set\Test.exe", vbNormal
End Sub
**********
Regards - Nathan
0 Likes
Message 4 of 5

Anonymous
Not applicable
Nice work Nathan!

It occurred to me just now when I saw your hard-coded path to Notepad (as I had recommened) that because Notepad resides in the Windows directory, you actually *don't* have to hard-code the path - you don't even need to specify the extension for it! Just Shell("Notepad") works fine (on my XP system it does anyway).

Regards

Wayne
0 Likes
Message 5 of 5

Anonymous
Not applicable
Thanks Wayne,

I recently worked out the hold control part for one of my projects so I'm glad I could share it.

Regards - Nathan
0 Likes