LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default protect / unprotect VBA project by macro

Hello everybody,

I'm interested by setting-up / unsetting a password protection of
my VBA project by macro, but it doesn't seem to be easy.

I know that I can test the protection mode using :
Application.ActiveVBProject.Protection
= 0 (vbext_pp_none) if not protected
= 1 (vbext_pp_locked) if protected

But how setting-up the protection ?
The problem is to use the SendKeys method with the appropriate windows.

I tried unsuccessfully many ways, see below, and any help is granted.
Thanks in advance,
sylvain

-------------

Sub UnlockedVBAProject()

If Val(Application.Version) 8 Then
SendKeys _
"%{F11}%xi+{TAB}{RIGHT}{TAB} {TAB}" & _
"{BACKSPACE}{TAB}{BACKSPACE}{TAB}{ENTER}%{q}"
End If
End Sub


Sub LockedVBAProject()
If Val(Application.Version) 8 Then
SendKeys _
"%{F11}%xi+{TAB}{RIGHT}{TAB} {TAB}" & _
PASSWORD & "{TAB}" & "spi2006" & "{TAB}{ENTER}%{q}"
End If
End Sub

-------------

Sub DeprotegerProjetVB3()

Dim XLhWnd As Long, VBEhWnd As Long, CurhWnd As Long
Dim Wbk As Workbook

Dim Classeur As String
Const MdP As String = PASSWORD

Classeur = ActiveWorkbook.FullName

On Error Resume Next
Set Wbk = Workbooks(Dir$(Classeur))
On Error GoTo Fin
If Not Wbk Is Nothing Then
If Wbk.FullName < Classeur Then Exit Sub
If Not Wbk.Saved Then Wbk.Save
Else: Application.ScreenUpdating = False
End If

CurhWnd = GetForegroundWindow
XLhWnd = FindWindowA(vbNullString, Application.Caption)

With Application.VBE
VBEhWnd = FindWindowA(vbNullString, .MainWindow.Caption)
If CurhWnd = XLhWnd Then SetForegroundWindow VBEhWnd
.CommandBars.FindControl(ID:=2557).Execute
' NE PAS EFFACER, même si le classeur est déjà ouvert !!!!!!
'Workbooks.Open Classeur
If ActiveWorkbook.VBProject.Protection = 1 Then
SendKeys "~" & MdP & "~", True
.ActiveCodePane.Window.Close
End If
End With

SetForegroundWindow CurhWnd
' Déprotège = True
Exit Sub

Fin:
End Sub
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to stop Excel remembering/loading macro from previously opened Workbook Norman Yuan Excel Discussion (Misc queries) 4 June 17th 06 04:13 PM
Protect macro ST Excel Discussion (Misc queries) 1 June 6th 06 06:26 PM
Vlookup to Return a Range of Data Rob Excel Discussion (Misc queries) 13 June 1st 06 04:02 AM
Add protect worksheet password in macro Kelly Excel Worksheet Functions 1 January 10th 06 02:08 AM
Search, Copy, Paste Macro in Excel [email protected] Excel Worksheet Functions 0 January 3rd 06 06:51 PM


All times are GMT +1. The time now is 05:04 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"