VBA in Excel Open properties

M

mccann_mj

I have an Excel 2007 file that when opened, asks for a password, but if you
don't have the password, will open as a read only (using the "Save As",
tools, General Options). I have a userform that opens upon opening the
workbook. I'm trying to find a way that if the password is not used, the form
does not open up; if the password is used, the form pops up. Is there an open
property I can "read" to determine if the password was used?
 
S

Simon Lloyd

You can't as far as i know check if the password has been used to ope
the workbook but you can check for read only like this

Code
-------------------
Private Sub Workbook_Open(
If (GetAttr(ThisWorkbook.Path & "\" & ThisWorkbook.Name) And vbReadOnly) The
MsgBox "The Workbook is Read-Only!!", vbInformatio
End I
End Su
-------------------

*How to Save a Workbook Event Macro*
1. *Copy* the macro above placing the cursor to the left of th
code box hold the *CTRL & Left Click,* then *Right Click* selected cod
and *Copy.*
2. Open your Workbook and *Right Click* on any *Worksheet's Name Tab*
3. *Left Click* on *View Code* in the pop up menu.
4. Press *ALT+F11* keys to open the *Visual Basic Editor.*
5. Press *CTRL+R* keys to shift the focus to the *Project Explore
Window*
6. Press the *Down Arrow Key* until *ThisWorkbook* is highlighted i
blue.
7. *Press* the *Enter* key to move the cursor to the *Code Window*
8. *Paste* the macro code using *CTRL+V*
9. *Save* the macro in your Workbook using *CTRL+S

mccann_mj;609975 said:
I have an Excel 2007 file that when opened, asks for a password, but i
yo
don't have the password, will open as a read only (using the "Save As"
tools, General Options). I have a userform that opens upon opening th
workbook. I'm trying to find a way that if the password is not used
the for
does not open up; if the password is used, the form pops up. Is ther
an ope
property I can "read" to determine if the password was used

-
Michael J

--
Simon Lloy

Regards
Simon Lloy
'Microsoft Office Help' (http://www.thecodecage.com
 
M

Matthew Herbert

Michael,

The .ReadOnly property should tell you if the file is in a ReadOnly state or
not.

So, your Workbook_Open event could look something like the following:

Private Sub Workbook_Open()
If ThisWorkbook.ReadOnly Then
'add code for the file being in a ReadOnly state
End If
End Sub

Best,

Matthew Herbert
 
Top