简体   繁体   中英

Connecting password-protected Excel to Access

I want to link a password-protected Excel document to Access - this is not working due to the it being password-protected.

Does anyone have an alternative method to do this?

It has to be password-protected unfortunately and cannot be moved to a secured folder so not sure how to do it.

Any advice?

Try using a hidden form to automatically open the Excel file when the database file is opened.

Add this to a new Module:

Option Compare Database
Public xl As Object

Function OpenExcelFile()
    xl.Workbooks.Open "path to file.xlsx", , , , "password"
End Function

Function CloseExcelFile()
    xl.Quit
    set xl=nothing
End Function

Create a blank form and set the HasModule property to true. then add the following to the form's code module.

Private Sub Form_Load()
    OpenExcelFile
End Sub

Private Sub Form_Close()
    CloseExcelFile
End Sub

Now create a new macro with an OpenForm task to open your form. Set the window mode to 'Hidden'. Save the macro with the name 'AutoExec'. This makes it run when the db is opened.

When the db is opened the macro will run and open the form hidden. The form load event will fire, creating a public Excel.Application object that opens your excel file (you should be able to remove the password from the code if you want the user to be prompted for it). The Excel application will remain open until the hidden form closes (when you close the database). At that point the form close event will fire, causing the Excel Application to quit. As long as the Excel file is open, you should be able to use linked tables and queries.

**You could add xl.Visible=true to the OpenExcelFile function if you want it to be visible to the user.

This link as a similar idea: https://www.connectionstrings.com/how-to-open-password-protected-excel-workbook/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM