Hi J_J,
J_J wrote:
Can we list the *.doc and *.xls files in all dir/sub directories of
drive D: which ARE protected by filename, loacation, size via VBA?
I'm not sure if there's a way to tell if a workbook is protected
without trying to open it. Here is some code that will do what you
are looking for *for Excel files only*. If you want to add Word
docs, you'll have to modify it a bit.
For this code to work, you have to set a reference to "Microsoft
Scripting Runtime" via Tools | References in the VBE. If you end up
adding Word to this, you'll have to set a reference to the Microsoft
Word library as well. Also, I don't retrieve the size, but that
would be pretty easy through the FileSystemObject - I believe there
is a Size property to the File object.
One caveat - this is pretty slow, especially if you're looking at
lots of folders/files. Even more so if a lot of the files are Excel
workbooks, as you have to attempt to open each one programmatically.
It would probably make sense to modify this to use *one* instance of
Excel to attempt to open the workbooks instead of opening/closing
the Excel app each time. But I'll leave that up to you - if you try
and need more help, let us know.
Public Sub DEMO()
gGetProtectedWBs "c:\temp\"
End Sub
Public Sub gGetProtectedWBs(rsInitialPath As String, _
Optional rbIncludeSubFolders As Boolean = False)
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
If fso.FolderExists(rsInitialPath) Then
mGetProtectedWBs fso, rsInitialPath
End If
End Sub
Private Sub mGetProtectedWBs(rfso As Scripting.FileSystemObject, _
rsPath As String)
Dim fil As Scripting.File
Dim fol As Scripting.Folder
'/ get files
For Each fil In rfso.GetFolder(rsPath).Files
If fil.Type = "Microsoft Excel Worksheet" Then
If gbIsWBProtected(fil.Path) Then _
Debug.Print fil.Path
End If
Next fil
'/ get subfolder, recursively call this sub
For Each fol In rfso.GetFolder(rsPath).SubFolders
mGetProtectedWBs rfso, fol.Path
Next fol
End Sub
Public Function gbIsWBProtected(rsPath As String) As Boolean
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
On Error GoTo ErrHandler
gbIsWBProtected = True
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open(Filename:=rsPath, _
Password:="")
gbIsWBProtected = xlWB Is Nothing
ExitRoutine:
If Not xlWB Is Nothing Then
xlWB.Close False
Set xlWB = Nothing
End If
If Not xlApp Is Nothing Then
xlApp.Quit
Set xlApp = Nothing
End If
Exit Function
ErrHandler:
Resume ExitRoutine
End Function
--
Regards,
Jake Marx
MS MVP - Excel
www.longhead.com
[please keep replies in the newsgroup - email address unmonitored]