VB.NET - 编辑文件属性"Company"等?



我想知道如何设置文件的属性?我说的是领域、作者、公司等。我通过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默认值。这是我的公司名称。

您认为这能满足您的需求吗?http://msdn.microsoft.com/en-us/magazine/cc163637.aspx

最新更新