PDA

View Full Version : Read Only Files



dodonohoe
09-22-2014, 12:50 AM
I would like to automatically make all files in a folder read only. I have two file types, Excel and PDF's. No problem to do this for the Excel files (see my code below). However for the PDF files I am wondering what the best approach is. I have read that using the JavaScript facility in Adobe PDF is popular. I would prefer to use VBA for the entire process but at a minimum am wondering if I can call the JavaScript from within VBA?

Any thoughts would be appreciated.


Sub PasswordMaster()
Dim FileName, FileDir, FileSearch As String

FileDir = "C:\Password Protect\" 'Change to suit
FileSearch = "*.xlsx"
FileName = Dir(FileDir & FileSearch)
Do While FileName <> ""
Workbooks.Open FileName:=FileDir & FileName

Application.DisplayAlerts = False

ActiveWorkbook.SaveAs FileName:= _
FileName, FileFormat:= _
xlOpenXMLWorkbook, Password:="password", WriteResPassword:="", ReadOnlyRecommended:= _
True, CreateBackup:=False



Workbooks(FileName).Save
Workbooks(FileName).Close

Application.DisplayAlerts = True

FileName = Dir()
Loop
End Sub

Thanks,

Des

lecxe
09-23-2014, 09:48 AM
Hi Des

Instead of opening and saving files you can just set the ReadOnly attribute of the file.

This is an example of a code to set the ReadOnly attribute of the file directly given its pathname:



' Set the Read Only attribute of a file
Sub SetReadOnly(sFilePathName As String)

With CreateObject("Scripting.FileSystemObject").GetFile(sFilePathName)
.Attributes = .Attributes Or vbReadOnly
End With
End Sub


THis is an example that uses the code. To make the file "Example.zip" in the folder "c:\tmp" read only:


SetReadOnly "c:\tmp\example.zip"

mancubus
09-24-2014, 12:05 AM
Sub Make_Files_ReadOnly()
Dim fPath As String
Dim fsoFile As Object

fPath = "C:\ReadOnlyFiles\"
With CreateObject("Scripting.FileSystemObject")
For Each fsoFile In .GetFolder(fPath).Files
SetAttr fPath & fsoFile.Name, vbReadOnly
Next fsoFile
End With
End Sub

snb
09-24-2014, 01:02 AM
or


Sub M_snb()
For Each fl In CreateObject("scripting.filesystemobject").getfolder("G:\OF").Files
fl.Attributes = 1
Next
End Sub

lecxe
09-24-2014, 01:22 AM
or


Sub M_snb()
For Each fl In CreateObject("scripting.filesystemobject").getfolder("G:\OF").Files
fl.Attributes = 1
Next
End Sub

Hi snb

Remark: in this case you are resetting all other file attributes

lecxe
09-24-2014, 01:36 AM
Sub Make_Files_ReadOnly()
Dim fPath As String
Dim fsoFile As Object

fPath = "C:\ReadOnlyFiles\"
With CreateObject("Scripting.FileSystemObject")
For Each fsoFile In .GetFolder(fPath).Files
SetAttr fPath & fsoFile.Name, vbReadOnly
Next fsoFile
End With
End Sub



Hi mancubus

Your code is also resetting other attributes of the file.

dodonohoe
09-24-2014, 02:09 AM
Hi all,

Thanks very much for all of your responses the first part of my question is solved. In relation to the predicament of password protecting PDFs, any thoughts? As I mentioned in my original post I had read that using the JavaScript facility in Adobe PDF is popular. I would prefer to use VBA for the entire process and I would just like to confirm if that is not an option before I jump into the wonderful world of Java Script?

Cheers,

Des

mancubus
09-24-2014, 02:36 AM
@lecxe

is that what the OP asks for?

lecxe
09-24-2014, 02:42 AM
@lecxe

is that what the OP asks for?

I don't think so. If I understood correctly the OP just wants to make the file readonly, not change other attributes of the file.
For ex., if the file has the Hidden attribute set before the code runs then that attribute should still be set after the code runs.
That's why I use the OR in my code, so that the code does not change other attributes.