英文:
Is there any reliable way to check if powerpoint and word documents are password protected in VBA?
问题
我正在尝试创建一个函数,该函数可以检查docx或pptx文件是否受到密码保护。以下是我的函数:
Public Function isPasswordProtected(ByVal path As String) As Boolean
On Error GoTo ErrorHandler
Dim wordApp As Word.Application
Dim wordFile As Object
Set wordApp = New Word.Application
wordApp.Visible = False
Set wordFile = Documents.Open(fileName:=path, PasswordDocument:="!@#$%&")
If Err = 0 Then ' no error occurred
isPasswordProtected = False
Else
isPasswordProtected = True
End If
wordFile.Close (False)
wordApp.Quit
Set wordApp = Nothing
Set wordFile = Nothing
isPasswordProtected = True
ErrorHandler:
Debug.Print "isPasswordProtected( ):" & Err.Description
End Function
Sub TestProtection()
Dim protected As String
Dim unrpotected As String
protected = "C:\Temp\word\protected.docx"
Debug.Print isPasswordProtected(protected)
unrpotected = "C:\Temp\word\unprotected.docx"
Debug.Print isPasswordProtected(unrpotected)
End Sub
它确实能够工作,但有时会不可靠,偶尔会抛出以下错误:isPasswordProtected( ):The remote server machine does not exist or is unavailable
而不是:
isPasswordProtected( ):The password is incorrect. Word cannot open the document.
(C:\Temp\word\protected.docx)
False
isPasswordProtected( ):
True
当我检查大量文档时,这是一个问题。有没有其他方法可以实现这个功能?
英文:
I am trying to create a function that would check if docx or pptx files are password protected. Here is my function:
Public Function isPasswordProtected(ByVal path As String) As Boolean
On Error GoTo ErrorHandler
Dim wordApp As Word.Application
Dim wordFile As Object
Set wordApp = New Word.Application
wordApp.Visible = False
Set wordFile = Documents.Open(fileName:=path, PasswordDocument:="!@#$%")
If Err = 0 Then ' no error occurred
isPasswordProtected = False
Else
isPasswordProtected = True
End If
wordFile.Close (False)
wordApp.Quit
Set wordApp = Nothing
Set wordFile = Nothing
isPasswordProtected = True
ErrorHandler:
Debug.Print "isPasswordProtected( ):" & Err.Description
End Function
Sub TestProtection()
Dim protected As String
Dim unrpotected As String
protected = "C:\Temp\word\protected.docx"
Debug.Print isPasswordProtected(protected)
unrpotected = "C:\Temp\word\unprotected.docx"
Debug.Print isPasswordProtected(unrpotected)
End Sub
I does work but not reliably from time to time it just throws the following error: isPasswordProtected():The remote server machine does not exist or is unavailable
Rather than:
isPasswordProtected( ):The password is incorrect. Word cannot open the document.
(C:\Temp\word\protected.docx)
False
isPasswordProtected( ):
True
And when I am checking lots of documents its a problem. Are there any alternative ways of doing that?
答案1
得分: 1
尝试这个版本的函数:
Public Function isPasswordProtected(ByVal path As String) As Boolean
On Error GoTo ErrorHandler
Dim wordApp As Word.Application
Dim wordFile As Word.Document
Set wordApp = New Word.Application
wordApp.Visible = False
Set wordFile = wordApp.Documents.Open(Filename:=path, PasswordDocument:="!@#$%")
If Err = 0 Then ' no error occurred
isPasswordProtected = False
Else
isPasswordProtected = True
End If
wordFile.Close (False)
wordApp.Quit
Set wordApp = Nothing
Set wordFile = Nothing
Exit Function
ErrorHandler:
Debug.Print "isPasswordProtected( ): " & Err.Description & vbNewLine
wordApp.Quit
Set wordApp = Nothing
Set wordFile = Nothing
isPasswordProtected = True
End Function
英文:
Try this version of your function
Public Function isPasswordProtected(ByVal path As String) As Boolean
On Error GoTo ErrorHandler
Dim wordApp As Word.Application
Dim wordFile As Word.Document
Set wordApp = New Word.Application
wordApp.Visible = False
Set wordFile = wordApp.Documents.Open(Filename:=path, PasswordDocument:="!@#$%")
If Err = 0 Then ' no error occurred
isPasswordProtected = False
Else
isPasswordProtected = True
End If
wordFile.Close (False)
wordApp.Quit
Set wordApp = Nothing
Set wordFile = Nothing
Exit Function
ErrorHandler:
Debug.Print "isPasswordProtected( ):" & Err.Description & vbNewLine
wordApp.Quit
Set wordApp = Nothing
Set wordFile = Nothing
isPasswordProtected = True
End Function
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论