我的应用程序将在文件夹中搜索 ms office 文档,例如 word、excel 等,然后读取元数据中的标签。
但是当函数尝试打开受密码保护的(需要密码)文件时,我的函数将挂起/停止。
如何检测文件已受密码保护?如果是,我跳过这个文件并继续下一个文件。
'Read Word metadata
Public Sub ReadWord(WordFileName As String)
Dim Wapp As New Microsoft.Office.Interop.Word.Application
Dim docWord As New Microsoft.Office.Interop.Word.Document
If Wapp Is Nothing Then
Wapp = New Microsoft.Office.Interop.Word.Application
End If
If docWord Is Nothing Then
docWord = New Microsoft.Office.Interop.Word.Document
Else
docWord.Close()
End If
docWord = Wapp.Documents.Open(WordFileName)
Dim _BuiltInProperties As Object = docWord.BuiltInDocumentProperties
If Not _BuiltInProperties Is Nothing Then
word_keyword = _BuiltInProperties("Keywords").Value
End If
If Not docWord Is Nothing Then
docWord.Close()
End If
If Not Wapp Is Nothing Then
Wapp.Quit()
End If
End Sub
'Read Excel metadata
Public Sub ReadExcel(ExcelFileName As String)
Dim Wapp As New Microsoft.Office.Interop.Excel.Application
Dim excelbook As Microsoft.Office.Interop.Excel.Workbook
If Wapp Is Nothing Then
Wapp = New Microsoft.Office.Interop.Excel.Application
End If
excelbook = Wapp.Workbooks.Open(ExcelFileName)
Dim _BuiltInProperties As Object = excelbook.BuiltinDocumentProperties
If Not _BuiltInProperties Is Nothing Then
excel_keyword = _BuiltInProperties("Keywords").Value
End If
If Not excelbook Is Nothing Then
excelbook.Close()
End If
If Not Wapp Is Nothing Then
Wapp.Quit()
End If
End Sub