2010-01-13 76 views
0

我想尝试一些东西,我确信这是可能的,但不是真正肯定从excel中读取文件夹和任何文档属性?

在MS Excel(2003年),我可以写一个脚本,VBA,这将打开一个位置(例如:S://公共/营销/文件/),并列出位于那里(文件名)内的所有文件?

的最终目标将是有文件名,最后修改日期,创建和名称修改日期。

这可能吗?我想返回表单中行中找到的任何值。例如:类型:文件夹,键入:Word文档等

感谢您的信息!

回答

1

最近做了。使用DSOFile对象。在Excel-VBA中,首先需要创建对Dsofile.dll(“DSO OLE文档属性阅读器2.1”或类似文档)的引用。另外,请检查你的办公室图书馆

首先引用您可能要选择要检查的文件路径

Sub MainGetProps() 
Dim MyPath As String 

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

    GetFileProps MyPath, "*.*" 
End Sub 

让我们有一个很好的路径选择窗口

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 

现在让我们使用DSO对象读出信息...我将代码缩减为必要的代码

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 

那就应该是吧

祝你好运MikeD

+0

太棒了!今天我会告诉你一切,让你知道它是怎么回事! – 2010-01-14 10:27:17

+0

我已经复制了代码,因为你把它和一旦我确认文件夹来查看,它只是崩溃的Excel!我认为它陷入了一些循环。我会试图找出什么.... – 2010-01-14 13:27:15

+0

哎呀!在发布之前,我通过我的调试器运行了这个程序。我有XP的Excel 2003 SP2和我引用的MSOffice 11.0对象库,DSO OLE和MS除了默认 – MikeD 2010-01-14 14:12:00