views:

94

answers:

1

Hi guys... I am wanting to try something and I'm fairly sure it's possible, but not really sure!!

In MS Excel (2003) can I write a VBA script which will open a location (eg: s://public/marketing/documents/) and list all the documents located within there (filename)?

The ultimate goal would be to have the document name, date last modified, date created and modified by name.

Is this possible? I'd like to return any found values in rows on a sheet. eg: type: FOLDER, type: Word Doc etc.

Thanks for any info!

+1  A: 

Done that recently. Use the DSOFile object. In Excel-VBA you first need to create a reference to Dsofile.dll ("DSO OLE Document Properties Reader 2.1" or similar). Also check you have a reference to the Office library

First you may want to select the file path which you want to examine

Sub MainGetProps()
Dim MyPath As String

    MyPath = GetDirectoryDialog()
    If MyPath = "" Then Exit Sub

    GetFileProps MyPath, "*.*"
End Sub

Let's have a nice Path selection window

Function GetDirectoryDialog() As String
Dim MyFD As FileDialog

    Set MyFD = Application.FileDialog(msoFileDialogFolderPicker)
    With MyFD
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count <> 0 Then
            GetDirectoryDialog = .SelectedItems(1)
        End If
    End With

End Function

Now let's use the DSO object to read out info ... I reduced the code to the bare necessary

Private Sub GetFileProps(MyPath As String, Arg As String)
Dim Idx As Integer, Jdx As Integer, MyFSO As FileSearch, MyRange As Range, MyRow As Integer
Dim DSOProp As DSOFile.OleDocumentProperties

    Set DSOProp = New DSOFile.OleDocumentProperties
    Set MyRange = ActiveSheet.[A2]  ' your output is nailed here and overwrites anything

    Set MyFSO = Application.FileSearch

    With MyFSO
        .NewSearch
        .LookIn = MyPath
        .SearchSubFolders = True ' or false as you like
        .Filename = Arg
        .FileType = msoFileTypeAllFiles
        If .Execute() > 0 Then
            MsgBox .FoundFiles.Count & " file(s) found."  ' to see what you will get
            For Idx = 1 To .FoundFiles.Count

                DSOProp.Open .FoundFiles(Idx) ' examine the DSOProp element in debugger to find all summary property names; not all may be filled though
                Debug.Print .FoundFiles(Idx)
                Debug.Print "Title: "; DSOProp.SummaryProperties.Title
                Debug.Print "Subject: "; DSOProp.SummaryProperties.Subject
                ' etc. etc. write it into MyRange(Idx,...) whatever

                ' now hunt down the custom properties
                For Jdx = 0 To DSOProp.CustomProperties.Count - 1
                    Debug.Print "Custom #"; Jdx; " ";
                    Debug.Print " Name="; DSOProp.CustomProperties(Jdx).Name;
                    If DSOProp.CustomProperties(Jdx).Type <> dsoPropertyTypeUnknown Then
                        Debug.Print " Value="; DSOProp.CustomProperties(Jdx).Value
                    Else
                        Debug.Print " Type=unknowwn; don't know how to print";
                    End If
                    MyRow = MyRow + 1
                Next Jdx
                DSOProp.Close
            Next Idx
        Else
            MsgBox "There were no files found."
        End If
    End With
End Sub

and that should be it

good luck MikeD

MikeD
fantastic! I'll give this a go today and let you know how it goes!
Matt Facer
I've copied the code as you put it and once I confirm the folder to look through, it just crashes excel! I assume its stuck in some loop. I'll try to find out what....
Matt Facer
oops! I ran this thru my debugger before posting. I have Excel 2003-SP2 on XP, and I am referencing MSOffice 11.0 object library, DSO OLE and MS Forms 2.0 (for some other sub's) in addition to default
MikeD
does it crash at once or at the n-th repetition of the loop? .... could you see files in the Dir with no read access rights - my code may not handle restrictions on read permissions.
MikeD
thanks for the reply. I am using Office 2003 SP3 also (on XP). I reference DSO OLE Doc Properties Reader (2.1) and Office 11. I don't see anything on the screen - it just crashes and I have to force quit excel. It hangs straight away. I have a similar script working - but it doesnt list sub-contents of folders. (from a download here: http://ask.metafilter.com/54952/Help-me-output-a-list-of-files-in-a-directory-to-Excel-or-HTML).
Matt Facer
ive been commenting out your code and running it bit by bit and it seems to crash when this is in play... `If .Execute() > 0 then`
Matt Facer
general hint: put a breakpoint at Sub MainGetProps() and cycle through with F8, that saves on commenting out code ;) you can decide to omit individual statements with Ctrl-F9 .... the .Execute() actually fires the search. Cycle the code through just before the .Execute() and examine the MyFSO object in the Locals window to see if there are any oddities. The param's you entered should be all visible somewhere in the object
MikeD
... and try to reference the Windows Script Host Object Model Library, some say there are dependencies betsween this and the FSO. A complete crash highly likely is due to missing references
MikeD
thanks. I'll give that a go later and let you know! cheers
Matt Facer
Tried running through the code and it still crashed .. I'll keep trying!!
Matt Facer
I just left it running and when it normally would crash it kind of worked. It came back after about ten seconds with no files found. Even though there are files in the folder. Also when it's searching, if you click in excel it says it's not responding and will crash.
Matt Facer
very annoying - indeed! I've checked the code against a network drive with files where I don't have read permissions. The filesearch/executes works and brings them back, when I then try to read the perm's of such a file, I get an error but my PC doesn't crash completely at least. Out for some travelling ... will try to scan this thread once a day during the next week to see how's going ... cheers
MikeD
thanks very much! It does seem to be working, but when it's searching and you click anywhere it says [not responding] ... but eventually comes back to say "no files found". The code which I used above will take a folder, display the files all ok with no time out. I'm just going through your code and that one seeing what I can change.... The other code doesnt seem to get properties such as the author etc. (http://ask.metafilter.com/54952/Help-me-output-a-list-of-files-in-a-directory-to-Excel-or-HTML)
Matt Facer
having no luck. I've tried some other code too and it did the same - just hangs then returns no files. I have some code which I uses a different method which is working on FILES in a folder... but won't go and look in other folders. I need to see how to change the code to look in folders so going to post here shortly. I've tried all sorts, but it's just beyond me! Thank you for all your help.
Matt Facer