Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open a file in read only using shell command

Status
Not open for further replies.

neemi

Programmer
Joined
May 14, 2002
Messages
519
Location
GB
I am using the shell command to open up a file but i want to know how to open it up in read only?

Any suggestions?

cheers,
neemi
 
Is it an Excel file?
If yes, whay do you open it using shell?
Use this:
Code:
Workbooks.Open Filename:="MyPath\MyFile.xls", ReadOnly:=True
If not, you have to change the attributes of the file before open it... It is dificult to change it like initially after closing...
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("MyPath\MyFile.xxx")
 objFile.Attributes = 1
 '1 for Read-only
 '4 for System file
 '64 for shortcut or link
 '32 for archive
 '2048 for compressed file
 Set objFSO = Nothing: Set objFile = Nothing

Fane Duru
 
iT IS A WORD DOC.

I AM TRYING TO USE SETATTR TO SET IT PRIOR TO OPENING BUT I KEEP GETTING A TYPE MISMATCH AND I CAN'T SEE Y FOR THE LIFE OF ME!!!

THE CODE SAMPLE IS AS FOLLOW:

Code:
Sub test()
Dim strFileName  As String

strFileName = "C:\Test.doc"
SetAttr strFileName, vbReadOnly
End Sub

ANY SUGGESTIONS ON THIS ONE??
 
And this ?
VBA.SetAttr strFileName, vbReadOnly

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
1. Please do not use Full Caps. Thanks.

2. I have to agree with the question - why are you using Shell? Is there a reason you want to change the actual attribute? if you just want to open the file as read only, then:
Code:
Sub OpenReadOnly(sFile As String)
Documents.Open FileName:=sFile, ReadOnly:=True
End Sub

Sub GetFile()
Dim sFile
' NOTE! This would require the user
' to type in the full path!
' There are many other (better) ways to
' do this
sFile = InputBox("Which file?")
OpenReadOnly (sFile)
End Sub

the first sub you could replace the parameter with the file directly if you want. The second sub simply asks for the input of the filename. This could all be done better, but it gives you the idea.

Again....why are you setting the attribute itself to read only?

3. Have you written a function called SetAttrib? The actual syntax to use Shell would be:
Code:
Dim var
var = Shell("C:\WINNT\System32\attrib.exe +R c:\test\Makereadonly.doc")

the above would set the file attribute to ReadOnly (+R).

BUT....what are you doing, and why?



Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top