在VBA中,有没有可靠的方法来检查PowerPoint和Word文档是否受密码保护?

huangapple go评论51阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月24日 01:28:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定