Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Setting VBA passwords from within code

I have found that it doesn't seem to be easy to set a VBA password
easily from within code. I figure that I probably have to go down the
send keys route and so have set up the following sub

Sub SetVBAPassword()
Dim strPassword As String
strPassword = "monkey"

Excel.SendKeys "%TE^{TAB}VP", False
Excel.SendKeys strPassword & "{TAB}" & strPassword, False
Excel.SendKeys "{Enter}", False
End Sub

My problem is that this code will only work if it is in the same
project as the one you want to set the password for. I want to be able
to set the password from another workbook but don't know how to do it.
Can someone please help me.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Setting VBA passwords from within code

code previously posted by Bill Manville:

Sub TestProtect()
Workbooks.Add.SaveAs "C:\Temp\Book1.xls"
ProtectVBProject Workbooks("Book1.xls"), "Jack"
Workbooks("Book1.xls").Close True
End Sub

Sub TestUnprotect()
Workbooks.Open "C:\Temp\Book1.xls"
UnprotectVBProject Workbooks("Book1.xls"), "Jack"
End Sub

Sub UnprotectVBProject(WB As Workbook, ByVal Password As String)
Dim vbProj As Object

Set vbProj = WB.VBProject

'can't do it if already unlocked!
If vbProj.Protection < 1 Then Exit Sub

Set Application.VBE.ActiveVBProject = vbProj

' now use lovely SendKeys to quote the project password
SendKeys Password & "~~"
Application.VBE.CommandBars(1).FindControl(Id:=257 8,
recursive:=True).Execute
End Sub

Sub ProtectVBProject(WB As Workbook, ByVal Password As String)
Dim vbProj As Object

Set vbProj = WB.VBProject

'can't do it if already locked!
If vbProj.Protection = 1 Then Exit Sub

Set Application.VBE.ActiveVBProject = vbProj

' now use lovely SendKeys to set the project password
SendKeys "+{TAB}{RIGHT}%V{+}{TAB}" & Password & "{TAB}" & _
Password & "~"

Application.VBE.CommandBars(1).FindControl(Id:=257 8, _
recursive:=True).Execute

WB.Save
End Sub


Bill Manville
MVP - Microsoft Excel, Oxford, England
No email replies please - reply in newsgroup

--
Regards,
Tom Ogilvyl

wrote in message
om...
I have found that it doesn't seem to be easy to set a VBA password
easily from within code. I figure that I probably have to go down the
send keys route and so have set up the following sub

Sub SetVBAPassword()
Dim strPassword As String
strPassword = "monkey"

Excel.SendKeys "%TE^{TAB}VP", False
Excel.SendKeys strPassword & "{TAB}" & strPassword, False
Excel.SendKeys "{Enter}", False
End Sub

My problem is that this code will only work if it is in the same
project as the one you want to set the password for. I want to be able
to set the password from another workbook but don't know how to do it.
Can someone please help me.



Reply
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
setting excel formula from code Ralf Excel Discussion (Misc queries) 4 July 14th 09 10:31 PM
Code about setting Y Axes scale. fujing1003[_2_] Charts and Charting in Excel 3 December 11th 07 11:29 AM
change language setting using VBA code ub Excel Worksheet Functions 0 April 3rd 07 05:34 PM
setting tab order by code dlrauh Excel Programming 1 October 25th 03 08:38 PM
Setting Decimal in code David W[_3_] Excel Programming 0 August 22nd 03 05:16 PM


All times are GMT +1. The time now is 09:00 AM.

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

About Us

"It's about Microsoft Excel"