VB.NET - 编辑文件属性“公司” ETC?

发布于 2024-11-10 02:24:25 字数 2941 浏览 3 评论 0原文

我想知道如何设置文件的属性?我说的是字段、作者、公司等。我找到了一种通过 Word 的内置属性来实现的方法。但这有点麻烦。所以我想知道是否可以通过其他方式做到这一点?

我有这段代码适用于除 *.doc 格式之外的所有 Word 文档文件。 这是我到目前为止的代码,一个文本框和一个按钮。该按钮运行 findDocLoop()

    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim oBuiltInProps As Object

  Public Sub findDocLoop()
        Dim strRootPath As String
        strRootPath = txtBoxRootpath.Text

        Dim di As New IO.DirectoryInfo(strRootPath)
        Dim aryFi As IO.FileInfo() = di.GetFiles("*.doc")
        Dim aryFi2 As IO.FileInfo() = di.GetFiles("*.dot")
        Dim aryFi3 As IO.FileInfo() = di.GetFiles("*.doc*")

        Dim fi As IO.FileInfo


        For Each fi In aryFi
            If Not fi.FullName.Contains("~$") Then
                RunRenameProcess(txtBoxRootpath.Text & "\" & fi.ToString)
            End If
        Next

        For Each fi In aryFi2
            If Not fi.FullName.Contains("~$") Then
                RunRenameProcess(txtBoxRootpath.Text & "\" & fi.ToString)
            End If
        Next

        For Each fi In aryFi3
            If Not fi.FullName.Contains("~$") Then
                RunRenameProcess(txtBoxRootpath.Text & "\" & fi.ToString)
            End If
        Next

        oDoc = Nothing

        lblDone.Text = "Finished"

    End Sub

    Public Function FileInUse(ByVal sFile As String) As Boolean
        Dim thisFileInUse As Boolean = False
        If System.IO.File.Exists(sFile) Then
            Try
                Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                    thisFileInUse = False
                End Using
            Catch
                thisFileInUse = True
                writeToLog(sFile)
            End Try
        End If
        Return thisFileInUse
    End Function

    Public Sub writeToLog(ByVal strFile As String)
        Dim sContents As String
        sContents = strFile & " - " & DateTime.Now.ToLongTimeString
        SaveTextToFile(sContents, Directory.GetCurrentDirectory & "\errorlog.txt")
    End Sub

    Private Sub RunRenameProcess(ByVal strFile)

        If FileInUse(strFile) = False Then
            'Create instance of Word and make it visible
            'On Error Resume Next
            oDoc = oWord.Documents.Open(strFile)

            'Get the properties collection in file
            oBuiltInProps = oDoc.BuiltInDocumentProperties

            'Set the value of the properties
            oBuiltInProps.Item("Company").Value = txtBoxCompany.Text
            ' AT THIS POINT, THE PROPERTY IS ACTUALLY SET (IF I CHECK IN WORD)

            oDoc.Save()
            ' AT THIS POINT, THE PROPERTY IS RESET TO THE DEFAULT WORD COMPANY VALUE(DOMAIN)
            oDoc.Close()
        End If

    End Sub

我知道它可能可以做得更好,但我有点着急。我刚刚注意到当我在代码设置属性值之后设置断点时。我将 Word 设置为打开状态,进入其中并检查属性值。事实上它已经被设定了!但是保存之后,好像就丢失了。或“重置”为某些 Office 默认设置。这是我的公司名称。

I was wondering how I can set a file's properties? I'm talking about the fields, author, company etc. I found a way of doing it through Word's builtin properties. But it's a little buggy. So I was wondering if it's possible to do that in other ways?

I have this code that works for all Word document files except *.doc format it seems.
This is the code I have so far, one text box and one button. The button runs findDocLoop()

    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim oBuiltInProps As Object

  Public Sub findDocLoop()
        Dim strRootPath As String
        strRootPath = txtBoxRootpath.Text

        Dim di As New IO.DirectoryInfo(strRootPath)
        Dim aryFi As IO.FileInfo() = di.GetFiles("*.doc")
        Dim aryFi2 As IO.FileInfo() = di.GetFiles("*.dot")
        Dim aryFi3 As IO.FileInfo() = di.GetFiles("*.doc*")

        Dim fi As IO.FileInfo


        For Each fi In aryFi
            If Not fi.FullName.Contains("~$") Then
                RunRenameProcess(txtBoxRootpath.Text & "\" & fi.ToString)
            End If
        Next

        For Each fi In aryFi2
            If Not fi.FullName.Contains("~$") Then
                RunRenameProcess(txtBoxRootpath.Text & "\" & fi.ToString)
            End If
        Next

        For Each fi In aryFi3
            If Not fi.FullName.Contains("~$") Then
                RunRenameProcess(txtBoxRootpath.Text & "\" & fi.ToString)
            End If
        Next

        oDoc = Nothing

        lblDone.Text = "Finished"

    End Sub

    Public Function FileInUse(ByVal sFile As String) As Boolean
        Dim thisFileInUse As Boolean = False
        If System.IO.File.Exists(sFile) Then
            Try
                Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                    thisFileInUse = False
                End Using
            Catch
                thisFileInUse = True
                writeToLog(sFile)
            End Try
        End If
        Return thisFileInUse
    End Function

    Public Sub writeToLog(ByVal strFile As String)
        Dim sContents As String
        sContents = strFile & " - " & DateTime.Now.ToLongTimeString
        SaveTextToFile(sContents, Directory.GetCurrentDirectory & "\errorlog.txt")
    End Sub

    Private Sub RunRenameProcess(ByVal strFile)

        If FileInUse(strFile) = False Then
            'Create instance of Word and make it visible
            'On Error Resume Next
            oDoc = oWord.Documents.Open(strFile)

            'Get the properties collection in file
            oBuiltInProps = oDoc.BuiltInDocumentProperties

            'Set the value of the properties
            oBuiltInProps.Item("Company").Value = txtBoxCompany.Text
            ' AT THIS POINT, THE PROPERTY IS ACTUALLY SET (IF I CHECK IN WORD)

            oDoc.Save()
            ' AT THIS POINT, THE PROPERTY IS RESET TO THE DEFAULT WORD COMPANY VALUE(DOMAIN)
            oDoc.Close()
        End If

    End Sub

I know it can probably be done better, but I'm kinda in a hurry. I just noticed when I set a break point right after the code sets the value of the property. I set Word to be open, got in there and checked the property value. And it was actually set!. But after the save phaze, it seems to be lost. Or "reset" to some Office defaults. Which is my company name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文