使用VBA获取扩展文件属性

Jim*_*eth 21 vba file-attributes fso

尝试使用Excel VBA捕获磁盘上文件的所有文件属性,包括扩展属性.能够让它循环遍历文件并捕获基本属性(来自文件系统):

  • 文件路径
  • 文件名
  • 文件大小
  • 创建日期
  • 上次访问日期
  • 上次修改日期
  • 文件类型

还想捕获来自文件本身的扩展属性:

  • 作者
  • 关键词
  • 评论
  • 最后的作者
  • 类别
  • 学科

以及右键单击文件时可见的其他属性.

目标是创建文件服务器上所有文件的详细列表.

Ale*_* K. 24

你说循环..所以如果你想为dir而不是当前文件这样做;

Dim sFile As Variant
Dim oShell: Set oShell = CreateObject("Shell.Application")
Dim oDir:   Set oDir = oShell.Namespace("c:\foo")

For Each sFile In oDir.Items
   Debug.Print oDir.GetDetailsOf(sFile, XXX) 
Next
Run Code Online (Sandbox Code Playgroud)

其中XXX是一个属性列索引,例如作者为9.要列出可供您参考的索引,可以将for循环替换为;

for i = 0 To 40
   debug.? i, oDir.GetDetailsOf(oDir.Items, i)
Next
Run Code Online (Sandbox Code Playgroud)

快速获取单个文件/属性:

Const PROP_COMPUTER As Long = 56

With CreateObject("Shell.Application").Namespace("C:\HOSTDIRECTORY")
    MsgBox .GetDetailsOf(.Items.Item("FILE.NAME"), PROP_COMPUTER)
End With
Run Code Online (Sandbox Code Playgroud)

  • 对于那里的人来说,这已经过时了.您现在可以使用288个属性.例如,文件版本271.更新for循环以查看它们.http://msdn.microsoft.com/en-us/library/windows/desktop/bb787870(v=vs.85).aspx (2认同)

Tod*_*ain 8

你可以得到这个.BuiltInDocmementProperties.

例如:

Public Sub PrintDocumentProperties()
    Dim oApp As New Excel.Application
    Dim oWB As Workbook
    Set oWB = ActiveWorkbook

    Dim title As String
    title = oWB.BuiltinDocumentProperties("Title")

    Dim lastauthor As String
    lastauthor = oWB.BuiltinDocumentProperties("Last Author")

    Debug.Print title
    Debug.Print lastauthor
End Sub
Run Code Online (Sandbox Code Playgroud)

有关您可以访问的所有字段,请参阅此页面:http://msdn.microsoft.com/en-us/library/bb220896.aspx

如果您尝试在客户端之外执行此操作(例如,关闭Excel并运行代码,例如,.NET程序),则需要使用DSOFile.dll.