Erweiterte Dateieigenschaften für .docx-Dateien (Ab Office 2007)

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von Michdi.

    Erweiterte Dateieigenschaften für .docx-Dateien (Ab Office 2007)

    Guten Abend,
    ich arbeite momentan an einem Programm, welches die erweiterten Dateieigenschaften von Dateien verändern kann.
    Es soll speziell für Office-Dateien funktionieren (doc, docx, ppt, pptx, xls, xlsx).
    Bis jetzt funktioniert es ganz gut, aber leider nur für die alte Dateiversion, also doc, ppt und xls. Bei dem neuen Dateiformat mit x scheitert es mit folgendem Fehler: Unable to get writable property store for this property.

    Mein Code (Funktion):

    VB.NET-Quellcode

    1. Dim verzeichnis As String = "E:\Documents\"
    2. Public Function applyProperty(ByRef author As String, ByRef title As String, ByRef comment As String, ByRef subject As String, ByRef company As String, ByRef category As String, ByRef keywords As String) As Boolean
    3. Dim di As New DirectoryInfo(verzeichnis)
    4. Dim fiArr As FileInfo() = di.GetFiles()
    5. Dim fri As FileInfo
    6. For Each fri In fiArr
    7. If (fri.Extension.ToLower = ".doc") Then
    8. Try
    9. Dim filePath As String = fri.FullName
    10. Dim file As ShellFile = ShellFile.FromFilePath(filePath)
    11. Dim oldAuthors() As String = file.Properties.System.Author.Value
    12. Dim oldTitle As String = file.Properties.System.Title.Value
    13. Dim propertyWriter As ShellPropertyWriter = file.Properties.GetPropertyWriter
    14. propertyWriter.WriteProperty(SystemProperties.System.Author, New String() {author})
    15. propertyWriter.WriteProperty(SystemProperties.System.Title, New String() {title})
    16. propertyWriter.WriteProperty(SystemProperties.System.Comment, New String() {comment})
    17. propertyWriter.WriteProperty(SystemProperties.System.Subject, New String() {subject})
    18. propertyWriter.WriteProperty(SystemProperties.System.Company, New String() {company})
    19. propertyWriter.WriteProperty(SystemProperties.System.Category, New String() {category})
    20. propertyWriter.WriteProperty(SystemProperties.System.Keywords, New String() {keywords})
    21. propertyWriter.Close()
    22. Catch ex As Exception
    23. MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
    24. Clipboard.SetText(ex.Message)
    25. End Try
    26. End If
    27. Next fri
    28. Return True
    29. End Function
    30. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    31. applyProperty(title:=tbTitle.Text, author:=tbAuthor.Text, category:=tbCategory.Text, comment:=tbCommentary.Text, company:=tbCompany.Text, keywords:=tbKeyword.Text)
    32. End Sub


    Kann mir jemand helfen, wie man das ganze auch für .docx und .pptx etc Dateien zum Laufen bringt? Danke.

    Grüße,
    Michdi


    Gesamter Code:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports Microsoft.WindowsAPICodePack.Shell
    2. Imports Microsoft.WindowsAPICodePack.Shell.PropertySystem
    3. Imports Microsoft.WindowsAPICodePack
    4. Imports System.IO
    5. Public Class Form1
    6. Dim verzeichnis As String = "E:\Documents\"
    7. Public Function applyProperty(ByRef author As String, ByRef title As String, ByRef comment As String, ByRef subject As String, ByRef company As String, ByRef category As String, ByRef keywords As String, ByRef fileformat As String) As Boolean
    8. Dim di As New DirectoryInfo(verzeichnis)
    9. Dim fiArr As FileInfo() = di.GetFiles()
    10. Dim fri As FileInfo
    11. For Each fri In fiArr
    12. If (fri.Extension.ToLower = ".docx") Then
    13. 'MsgBox(fri.FullName)
    14. Try
    15. Dim filePath As String = fri.FullName
    16. Dim file As ShellFile = ShellFile.FromFilePath(filePath)
    17. Dim oldAuthors() As String = file.Properties.System.Author.Value
    18. Dim oldTitle As String = file.Properties.System.Title.Value
    19. Dim propertyWriter As ShellPropertyWriter = file.Properties.GetPropertyWriter
    20. propertyWriter.WriteProperty(SystemProperties.System.Author, New String() {author})
    21. propertyWriter.WriteProperty(SystemProperties.System.Title, New String() {title})
    22. propertyWriter.WriteProperty(SystemProperties.System.Comment, New String() {comment})
    23. propertyWriter.WriteProperty(SystemProperties.System.Subject, New String() {subject})
    24. propertyWriter.WriteProperty(SystemProperties.System.Company, New String() {company})
    25. propertyWriter.WriteProperty(SystemProperties.System.Category, New String() {category})
    26. propertyWriter.WriteProperty(SystemProperties.System.Keywords, New String() {keywords})
    27. propertyWriter.Close()
    28. Catch ex As Exception
    29. MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
    30. Clipboard.SetText(ex.Message)
    31. End Try
    32. End If
    33. Next fri
    34. Return True
    35. End Function
    36. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    37. lblStatus.Text = "Arbeitet..."
    38. If (applyProperty(title:=tbTitle.Text, author:=tbAuthor.Text, category:=tbCategory.Text, comment:=tbCommentary.Text, company:=tbCompany.Text, fileformat:="doc", keywords:=tbKeyword.Text, subject:=tbSubject.Text) = True) Then
    39. lblStatus.Text = "Fertig!"
    40. End If
    41. End Sub
    42. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    43. FolderBrowserDialog1.ShowDialog()
    44. verzeichnis = FolderBrowserDialog1.SelectedPath
    45. pfadBox.Text = verzeichnis
    46. End Sub
    47. End Class
    Die beste maschinelle Übersetzung der Welt - DeepL Übersetzer
    Alle Zitate, die ich seit dem 1.9.2017 übersetzt habe, wurden vollautomatisch mit DeepL übersetzt.



    Danke, scheint ganz interessant zu sein. Bei dem ersten aus msdn gibt's bei mir immer die Fehlermeldung, dass es Globals.This als Unterklasse nicht gibt, habe mich damit aber auch noch nicht zu 100% auseinander gesetzt. DocX scheint auch dafür nützlich zu sein, werde ich mir mal anschauen. Danke!
    Die beste maschinelle Übersetzung der Welt - DeepL Übersetzer
    Alle Zitate, die ich seit dem 1.9.2017 übersetzt habe, wurden vollautomatisch mit DeepL übersetzt.