![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Feb 2002
Location: Winnipeg, Manitoba, CANADA
Posts: 130
|
My Excel program is protected, but I have a problem. I have to Unprotect the project when I do a certain task and then I would like to re-protect it from unwanted eyes/fingers. Can I do this in code? I tried this and it doesn't seem to work...
' We have to unprotect the project VBAProject.ThisWorkbook.Unprotect password:="test123" ' do some stuff here ' Now re-protect the project VBAProject.ThisWorkbook.Protect password:="test123"
__________________
Thanx. |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
Well, never done/tried this, but, one question came to mind, WHY ??
I don't understand what you're trying to do... |
|
|
|
|
|
#3 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
Well, I've been looking for some property/method/event, or something, but I can't seem to find a way to do that... which would seem logical to me, to avoid creation of crack subs directly in VBA (Yes, yes, I know there are many programs that can do that, but no of them is built in VBA, as far as I know...)
One, not very good way and don't know how reliable, would be using SendKeys. |
|
|
|
|
|
#4 |
|
Board Regular
Join Date: Feb 2002
Location: Winnipeg, Manitoba, CANADA
Posts: 130
|
WHY? Good question...
I am creating and removing userforms on the fly. You can't create a userform if the Project is protected! I've used SendKeys before where would there be an example?
__________________
Thanx. |
|
|
|
|
|
#5 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
Found this on Google.
http://groups.google.com/groups?q=*e...ome.com&rnum=2 I saw what you meant after I posted my first response... |
|
|
|
|
|
#6 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
|
Here is a method I used some time ago
that protects and unprotects with a paasword. Option Explicit Const BreakIt As String = "%{F11}%TE+{TAB}{RIGHT}%V{+}{TAB}" Sub Change_VBA_PW() Dim WB As Workbook Dim Password As String Set WB = ActiveWorkbook Password = "ivan" Call SetVBProjectPassword(WB, Password) End Sub Sub SetVBProjectPassword(WB As Workbook, ByVal Password As String) 'Needs reference to Visual Basic for Applications Extensibility Library Dim VBP As VBProject Dim OpenWin As VBIDE.Window Dim i As Integer Set VBP = WB.VBProject Application.ScreenUpdating = False ' close any code windows to ensure we are in the right project For Each OpenWin In VBP.VBE.Windows If InStr(OpenWin.Caption, "(") > 0 Then OpenWin.Close Next OpenWin WB.Activate SendKeys BreakIt & Password & "{tab}" & Password & "~" & "%{F11}~", True 'SendKeys "enter", True WB.Activate SendKeys "%{F11}", True Application.ScreenUpdating = True End Sub Ivan |
|
|
|
|
|
#7 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
Very nice code Ivan !
|
|
|
|
|
|
#8 |
|
Board Regular
Join Date: Feb 2002
Location: Winnipeg, Manitoba, CANADA
Posts: 130
|
Looks good, thanx for your help and everyone else. Cheers.
__________________
Thanx. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|