我想知道如何设置文件的属性?我说的是字段,作者,公司等。我找到了一种通过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默认值。这是我的公司名称。