vba 如何获取工作簿文件的“上次保存者”属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41275072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:44:26  来源:igfitidea点击:

How to get the "Last Saved By" property for workbook file

vbaexcel-vbafilewinapiexcel

提问by Greg Lovern

In Windows Explorer, if I right-click a file and choose Properties to get the file's Properties window, and then select the Details tab, there is a property listed there called "Last Saved By". It seems to be the Windows account name that was logged in when the file was last modified.

在 Windows 资源管理器中,如果我右键单击一个文件并选择“属性”以获取该文件的“属性”窗口,然后选择“详细信息”选项卡,则会在其中列出一个名为“上次保存者”的属性。它似乎是上次修改文件时登录的 Windows 帐户名。

I've looked in FileSystemObject but I don't see that the File object has such a property.

我查看了 FileSystemObject 但我没有看到 File 对象具有这样的属性。

How do I get that property in VBA? Is there a Windows API for it?

如何在 VBA 中获得该属性?是否有适用于它的 Windows API?

UPDATE:

更新:

There are 3 attempts in this thread to do it with Shell's GetDetailsOf. I appreciate the effort but it seems pretty clear to me after trying them all (especially the code sample by OssieMac) that the text stored in the file system's "Last Saved By" field is not to be found in GetDetailsOf.

在这个线程中有 3 次尝试使用 Shell 的 GetDetailsOf 来做到这一点。我很欣赏这些努力,但在尝试了所有这些(尤其是 OssieMac 的代码示例)之后,我似乎很清楚在 GetDetailsOf 中找不到存储在文件系统的“上次保存者”字段中的文本。

Scratching my head. How does Windows Explorer do it??

挠我的头。Windows 资源管理器是如何做到的??

回答by Robin Mackenzie

Try this - the code uses the BuiltinDocumentPropertiesclass:

试试这个 - 代码使用BuiltinDocumentProperties类:

Option Explicit

Sub Test()
    MsgBox LastAuthor
End Sub

Function LastAuthor() As String
    LastAuthor = ThisWorkbook.BuiltinDocumentProperties("Last Author")
End Function

EDIT

编辑

Using extended file properties - Microsoftreports that the magic number for Authoris 9. However, this number changes over time with Windows releases and since Vista has been 20- see this link. Further to some testing, you might also try 10for Windows 10.

使用扩展文件属性 - Microsoft报告说 的幻数Author9. 但是,这个数字会随着 Windows 的发布而变化,并且自 Vista 以来20- 请参阅此链接。除了一些测试之外,您还可以尝试10使用 Windows 10。

Option Explicit

Sub Test()

    Dim varPath As Variant
    Dim varFileName As Variant

    varPath = "C:\Users\foo\bar\" '<~~ ensure final \
    varFileName = "lol.xlsx"


    'depending on OS version, try 9, 10 and 20
    Debug.Print GetAuthorFromShell(varPath, varFileName, 9)
    Debug.Print GetAuthorFromShell(varPath, varFileName, 10)
    Debug.Print GetAuthorFromShell(varPath, varFileName, 20)

End Sub

Function GetAuthorFromShell(varPath As Variant, varFileName As Variant, intProperty As Integer) As String

    Dim objShell As Object
    Dim objFolder As Object
    Dim strAuthor As String

    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(varPath) 

    With objFolder
        strAuthor = .getdetailsof(.Items.Item(varFileName), intProperty)
    End With

    GetAuthorFromShell = strAuthor

End Function

回答by Ari0nhh

This data is called Extended File Propertiesand stored in the NTFS metadata. You can read them using this script:

该数据被调用Extended File Properties并存储在 NTFS 元数据中。您可以使用此脚本阅读它们:

Dim arrHeaders(266)
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace("C:\Test.xtx")
For i = 0 to 265
 arrHeaders(i) = objFolder.GetDetailsOf(objFolder.Items, i)
Next
For Each strFileName in objFolder.Items
 For i = 0 to 265
 Wscript.Echo i & vbtab & arrHeaders(i) _
 & ": " & objFolder.GetDetailsOf(strFileName, i)
 Next
Next

Note, that different OS versions has different amount of extended file properties. Windows 2000 allowed 35, Windows Vista extended property count to the 266.

请注意,不同的操作系统版本具有不同数量的扩展文件属性。Windows 2000 允许 35 个,Windows Vista 将属性计数扩展到 266 个。

References:

参考:

1) Borrowing from Windows Explorer in PowerShell part 2: extended properties

1)在 PowerShell 中借用 Windows 资源管理器第 2 部分:扩展属性

2) Retrieving Extended File Properties

2)检索扩展文件属性