VB.NET-编辑文件属性“;“公司”;等
我想知道如何设置文件的属性?我说的是字段、作者、公司等。我找到了一种通过Word的内置属性来实现的方法。但它有一辆小马车。所以我想知道是否有可能用其他方式来实现这一点 我有这段代码,适用于所有Word文档文件,似乎除了*.doc格式。 这是我到目前为止的代码,一个文本框和一个按钮。该按钮运行findDocLoop()VB.NET-编辑文件属性“;“公司”;等,vb.net,file,file-io,io,Vb.net,File,File Io,Io,我想知道如何设置文件的属性?我说的是字段、作者、公司等。我找到了一种通过Word的内置属性来实现的方法。但它有一辆小马车。所以我想知道是否有可能用其他方式来实现这一点 我有这段代码,适用于所有Word文档文件,似乎除了*.doc格式。 这是我到目前为止的代码,一个文本框和一个按钮。该按钮运行findDocLoop() 我知道也许可以做得更好,但我有点赶时间。我刚刚注意到,当我在代码设置属性值之后设置断点时。我将Word设置为打开,进入其中并检查属性值。事实上是这样的!。但在拯救phaze之后,它
我知道也许可以做得更好,但我有点赶时间。我刚刚注意到,当我在代码设置属性值之后设置断点时。我将Word设置为打开,进入其中并检查属性值。事实上是这样的!。但在拯救phaze之后,它似乎丢失了。或“重置”为某些Office默认值。这是我的公司名称。你认为这就是你需要的吗 基本上所有的Word文件。我尝试打开一个winword.exe并在那里执行,但由于某些原因,扩展名为“doc”的文件无法保存其属性。真奇怪。在同一个循环中,它可以在docx、docm等上工作。你必须使用单词interop来完成这项工作。你不知道为什么“它有点问题”“.嗯,有点小错误,我的意思是我对每个*.doc文件都有这个循环,对于每个文件,运行这个函数。在这个函数中,我打开Word文档并编辑文档属性。我有作者、公司等。对于除.doc文件以外的所有文件,它都在工作。保存文档时会保存该属性。仅不适用于.doc文件。我觉得很奇怪。我可以在第一个帖子中更新我以前的代码,我发现实际上是保存过程重置了文档文件的文档属性。我试图在设置属性后立即暂停代码(我是设置属性的人),并选择手动保存文档。然后这个属性也被重置了,真的很奇怪。即使使用这段代码,同样的事情也会发生。我可以看到我在代码执行期间设置了Company属性。但一旦我保存文档并关闭它,属性就会被重新填充。而且这种情况只发生在.doc文件中!这真的很奇怪。只是删除了这些信息吗?我认为有一种方法可以解决这个问题?嗯,似乎信息被重置了。这很奇怪,它只是将其重置为添加到单词installation中的任何公司信息。就我而言,这可能暂时还可以,因为不管怎么说,这是应该放在那里的公司名称,但我在以后的日子里无法控制它。
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