[RESOLVED] Open file in read-only (ShellExecute API)
guys how can i open a specific file in read-only state using ShellExecute? this is my code so far, but the SetAttr doesn't work. it opens the file but the user can edit it (coz it's not open in read-only)..
<code for Setting attributes>
Case "Open"
txtFile.Text = "\\192.168.0.215" + "\Public\iKnow attachments\" + frmDM.Label9.Caption
SetAttr txtfile.text,vbReadOnly
txtParameters.Text = ""
txtDirectory.Text = ""
cboShowStyle.ListIndex = STYLE_NORMAL
</code>
<code for opening file using ShellExecute>
result = ShellExecute(Me.hWnd, cboOperation.Text, txtFile.Text, txtParameters.Text, txtDirectory.Text, show_style)
</code>
need some help here... :(
Re: Open file in read-only (ShellExecute API)
This works
vb Code:
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
Private Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" _
(ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
Private Const FILE_ATTRIBUTE_READONLY = &H1
Private Const SW_SHOWNORMAL = 1
Private Sub Command1_Click()
SetFileAttributes "c:\MyFile.txt", FILE_ATTRIBUTE_READONLY
ShellExecute Me.hwnd, vbNullString, "C:\MyFile.txt", vbNullString, "c:\", SW_SHOWNORMAL
End Sub
Re: Open file in read-only (ShellExecute API)
This does the same thing, perhaps quicker as well as shorter because it skips the overhead of Declare-based API calling:
Code:
SetAttr "c:\MyFile.txt", vbReadOnly
'Set reference to Microsoft Shell Controls and Automation:
With New Shell32.Shell
.ShellExecute "c:\MyFile.txt", , "c:\", , vbNormalFocus
End With
Neither one really "opens read-only" though. To do that you need to know what tool you're opening with and pass its command line parameters for a read-only open instead of using ShellExecute.
Re: Open file in read-only (ShellExecute API)
It should be noted that Hack's code will clear any other attributes. You should avoid doing that; I've modified his example so that no other attributes are changed.
vb Code:
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
Private Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" _
(ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
Private Const FILE_ATTRIBUTE_READONLY = &H1
Private Const SW_SHOWNORMAL = 1
Private Sub Command1_Click()
Dim lAt As Long
lAt = GetFileAttributes("c:\MyFile.txt")
lAt = lAt And FILE_ATTRIBUTE_READONLY
SetFileAttributes "c:\MyFile.txt", lAt
ShellExecute Me.hwnd, vbNullString, "C:\MyFile.txt", vbNullString, "c:\", SW_SHOWNORMAL
End Sub
Re: Open file in read-only (ShellExecute API)
thanks! i got it solved! :D thanks for all the concerns.. :))
Re: Open file in read-only (ShellExecute API)
If you're concerned about just turning on the "read only" attribute:
Code:
SetAttr "c:\MyFile.txt", GetAttr("c:\MyFile.txt") Or vbReadOnly
Note the correct oepration is Or, not And which will wipe the other attributes.
Re: [RESOLVED] Open file in read-only (ShellExecute API)
thanks dilettante for the advice.. will consider it also.. :D