Search code examples
excelvbapasswords

Open ThisWorkbook by password without prompt


This may sound like a wierd question but I need to get it done. When ThisWorkbook is opened manually Open it with the password "1234" without prompting the user on opening using Excel VBA.

The prompt appears even when a password is suppost to be passed by vba.

The code is saved in ThisWorkbook.

Private Sub Workbook_Open()
    Application.DisplayAlerts = False   
    Workbooks.Open fileName:=ThisWorkbook.FullName, Password:="1234", WriteResPassword:=""
Application.DisplayAlerts = True
End Sub

Solution

  • The code tries to reopen the same file (reopen itself) (ThisWorkbook.FullName)

    Create another excel file, copy the code in a module, and call it on open event:

        Private Sub OpenAnotherWorkBook()
            Application.EnableEvents = False   'disable Events
            Workbooks.Open Filename:="PathToPasswordProtectedFile.xlsm", Password:="1234", WriteResPassword:="", ReadOnly:=False
            Application.EnableEvents = True    'enable Events
        End Sub
    
    Private Sub Workbook_Open()
      call OpenAnotherWorkBook()
    End Sub
    

    Change the "PathToPasswordProtectedFile.xlsm" to the fullpath of the protected file