View Single Post
 
Old 01-11-2018, 10:09 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If you don't have the right password then you cannot open the document with VBA, however you can test it with a list of passwords e.g.

Code:
Sub Macro1()
Const strPassword As String = "Test|password|Lorem|another" 'List the passwords separated with '|'
Const strName As String = "E:\Path\Forum\Password is lorem test.docx" 'The document to open
Dim oDoc As Document
Dim vPass As Variant
Dim i As Integer
    vPass = Split(strPassword, "|")
    On Error Resume Next
    For i = 0 To UBound(vPass)
Debug.Print vPass(i)
        Set oDoc = Documents.Open(FileName:=strName, _
                                  PasswordDocument:=vPass(i), _
                                  ReadOnly:=True)
        If Not oDoc Is Nothing Then Exit For
    Next i
lbl_Exit:
    Set oDoc = Nothing
    Exit Sub
End Sub
If the correct password is in the list the document will be opened and the password for the document is the last one listed in the immediate window of the VBA editor (Ctrl+G),
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote