【问题标题】:VBA Write file tags from excelVBA从excel写入文件标签
【发布时间】:2021-07-13 12:55:33
【问题描述】:

我有一些代码可以列出文件夹中的文件并获取它们的标签:

Option explicit

'Declare variables
Dim ws As Worksheet
Dim i As Long
Dim FolderPath As String
Dim objShell, objFolder, objFolderItem As Object
Dim FSO, oFolder, oFile As Object
 
Application.ScreenUpdating = False
Set objShell = CreateObject("Shell.Application")

Set ws = ActiveWorkbook.Worksheets("Sheet1") 'Set sheet name

Worksheets("Sheet1").UsedRange.ClearContents
ws.Range("A1:D1").Value = Array("FileName", "Tags", "Subgroup", "Group")

Set FSO = CreateObject("scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(FolderLocation_TextBox.Value)

i = 2 'First row to print result

For Each oFile In oFolder.Files

'If any attribute is not retrievable ignore and continue
On Error Resume Next
    Set objFolder = objShell.Namespace(oFolder.Path)
    Set objFolderItem = objFolder.ParseName(oFile.Name)
    
    ws.Cells(i, 1) = oFile.Name
    ws.Cells(i, 2).Value = objFolder.GetDetailsOf(objFolderItem, 18) 'Tags
    ws.Cells(i, 5).Value = objFolder.GetDetailsOf(objFolderItem, 277)   'Description
    i = i + 1
On Error Resume Next
Next

现在我想知道如何将它们写入列表中的那些文件。我基本上是在尝试从 excel 中编写标签。

我在A 列中有一个完整的文件名,而我试图作为每个文件的标记写入的字符串在B 列中。
文件夹的地址在文本框的值中:UserForm_Tag.FolderLocation_TextBox.value

【问题讨论】:

  • This 不工作? ..尝试用FSO.GetFolder(UserForm_Tag.FolderLocation_TextBox.value)替换FSO.GetFolder(FolderLocation_TextBox.Value)
  • 它正在工作,朋友!我可以得到标签,但现在我需要把它们写回来,因为有些文件可能没有标签,而且我有一个代码可以为 B 列中的每个单独文件组成一个正确的标签,但现在我正在寻找一种方法将它们从 excel 写入每个文件的元数据。所以我在“A”中有一个文件名列表,在“B”中有一个标签,在一个文本框中有文件位置。我希望将 B 列中的这些值写入“A”列中提到的相应文件,你知道吗?
  • 哦。搞错了。
  • 最好在此处标记 PowerShell .. 参考 thisthis
  • 有一组Workbook.BuiltindocumentProperties可以通过VBA进行更改。您还可以将自定义属性添加到 CustomDocumentProperties。那是你要的吗?注意:内置属性显示在文件属性(文件资源管理器)中。

标签: excel vba tags


【解决方案1】:

您可以通过一组 Workbook.BuiltindocumentProperties 进行更改 VBA。您还可以将自定义属性添加到 CustomDocumentProperties。是 那你想要什么?注意:内置属性显示在文件中 属性(文件资源管理器)。 – Maciej Los ... 几小时前

是的,但是如何从 excel 中写入属性? ——爱德华兹……几小时前

嗯……

我很确定不可能通过标准 VBA 方法更改扩展文件属性。我见过可以做到这一点的ActiveX对象,例如:在How can I change extended file properties using vba问题的答案中,用户jac确实建议使用dsofile.dll

注意: 此库仅限于 32 位 WinOS,请参阅:64 Bit Application Cannot Use DSOfile。有关dsofile.dll 的更多详细信息,您可以在此处找到:How to set file details using VBA。最重要的信息是:

使用 VBA (DSOFile),您只能设置基本文件属性,并且只能在 NTFS。微软已经停止了存储文件的做法 辅助 NTFS 流中的属性(随 Windows 引入 Vista),因为保存在这些流上的属性不会随 文件作为附件发送或存储在 USB 磁盘上时的文件 FAT32 不是 NTFS。

正如我在对问题的评论中提到的,如果您想更改基本(最常用的)扩展文件属性Excel/Word 文件,我建议使用BuiltinDocumentProperties。一些内置属性对应于extended file properties。例如:

BuiltinDocumentProperty Extended Property (EP) EP Index
Title Title 10
Subject Subject 11
Author Author 9
Comments Comments 14
Creation Date Date created 4
Category Category 12
Company Company 30
and so on...

枚举所有内置属性:

Sub GetBuiltinProperties()
    Dim wsh As Worksheet, bdc As DocumentProperty
    Dim i As Long
    
    Set wsh = ThisWorkbook.Worksheets(2)
    On Error Resume Next
    i = 2
    For Each bdc In ThisWorkbook.BuiltinDocumentProperties
        wsh.Range("A" & i) = bdc.Name
        wsh.Range("B" & i) = bdc.Value
        i = i + 1
    Next
    Set wsh = Nothing

End Sub

设置内置属性:

Sub SetBuiltinProperties()

    With ThisWorkbook.BuiltinDocumentProperties
        .Item("Keywords") = "My custom tag"
        .Item("Comments") = "My custom description"
    End With

End Sub

所以...如果您想更改特定工作簿的内置属性,您必须:

  • 打开它,
  • chage/set 内置属性,
  • 保存,
  • 并关闭它。

【讨论】:

  • 谢谢!我正在尝试设置从文件夹中获取的列表中文件的标签,而不是工作簿的标签。
  • 感谢您的澄清。在这种情况下,你必须使用dsofile.dll,即使它的使用非常有限。
  • 是的,就像在 OP 中解释的那样,我基本上是在获取一个文件夹地址并在 excel 中制作一个列表(我还获得了每个文件的标签列表)。然后,如果特定文件中不存在标签字符串,我将连接它们,现在我需要将这些标签写回列表中的这些文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 1970-01-01
  • 2018-05-22
相关资源
最近更新 更多