开发者

VB.NET - Edit file property "Company" etc?

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
开发者_StackOverflow
        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.


Think this does what you need? http://msdn.microsoft.com/en-us/magazine/cc163637.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜