Check if excel workbook is protected before opening it

accessrookie2

Registered User.
Local time
Today, 12:02
Joined
Jul 23, 2009
Messages
16
Hello, I need to check it a workbook is protected before it opens.

Right now I basically have this:

Set xlApp = CreateObject("Excel.Application")
Set xlWb = xlApp.Workbooks.Open(WbPath)

Wbpath is the path to the excel file. As soon as it hits the second line of code it opens the file and asks me for a password. I need to check if it is protected before it opens so I can display a message.

Thanks
 
Hi,

This will work:

Code:
Public Sub TestExcel()
On Error GoTo TestExcelErr
 
    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim strFileName
 
    Set xlApp = CreateObject("Excel.Application")
 
    strFileName = "C:\ADSB\my_excel_file.xls"
 
    Set xlBook = xlApp.Workbooks.Open(strFileName, , , , "")
 
'   Insert code here
 
    xlBook.Close
    xlApp.Quit
 
TestExcelErr:
 
'    Check for an error returned by the Open statement (password needed)
    If Err() = 1004 Then
        MsgBox "Whatever..."
    End If
 
End Sub

Simon B.
 

Users who are viewing this thread

Back
Top Bottom