excel vba - VBA Project Password-Protect with SendKeys not Working Correctly -
i've spent last 2 days working on problem. of content i've found on topic doesn't address issue i'm having, i'm hopeful here can me.
i've been working on code following "master scorecard" workbook:
- takes each "student" sheet in workbook , copies sheet new workbook,
- does few minor manipulations of new workbook,
- imports module of code new workbook,
- adds
workbook_open
event ,workbook_beforeclose
event new workbook (to make sheetsxlveryhidden
depending on level of access), - runs subprocedure newly imported module,
- saves , closes workbook.
each scorecard uses code ensure person name on scorecard can access it. i've used environ("username")
in workbook events ensure security, know, if 1 , understands how run macros, he/she merely open vbeditor
, unhide xlveryhidden
sheets in workbook easily.
so, thought password protect new workbook's vbaproject
programmatically (see above: step number five). found few sources online of how use sendkeys
achieve goal (see below), sendkeys
unreliable (at best) , isn't cooperating code. code works charm if run itself, if call project using run macro:="filename!macroname"
doesn't set protection. after code has run , workbooks have been created, vbaproject
properties window(s) earlier code open , try execute @ same time crashes excel.
sub lockvbaproject() const vbaprojectpassword string = "123" dim vbp vbproject, openwin vbide.window dim wbactive workbook dim integer set wbactive = activeworkbook set vbp = wbactive.vbproject application.screenupdating = false ' close code windows ensure hit right project each openwin in vbp.vbe.windows if instr(openwin.caption, "(") > 0 openwin.close next openwin wbactive.activate application '//execute controls lock project\\ .vbe.commandbars("menu bar").controls("tools") _ .controls("vbaproject properties...").execute '//activate 'protection'\\ .sendkeys "^{tab}" '//caution: either checks or unchecks the\\ '//"lock project viewing" checkbox, if it's already\\ '//been locked viewing, unlock it\\ .sendkeys "{ }" '//enter password\\ .sendkeys "{tab}" & vbaprojectpassword '//confirm password\\ .sendkeys "{tab}" & vbaprojectpassword '//scroll down ok key\\ .sendkeys "{tab}" '//click ok key\\ .sendkeys "{enter}" 'the project locked - takes effect 'the next time book's opened... end thisworkbook.saveas filename:=sheets(sheets.count).name, fileformat:=xlopenxmlworkbookmacroenabled debug.print "it worked " & now() end sub
i'm not sure why happening; said, code works fine when run on own. found this post this link non-sendkeys
approach outlined, written several years ago , i'm not sure how i'd need modify purposes since i've never coded in vb6...
are there thoughts why sendkeys
method bunching after code has run instead of executing when it's supposed during code? should abandon sendkeys
in favor of other method? i'm @ loss, appreciated!
edit: think reason code isn't working because correct project isn't activated @ time sendkeys
code executed. had hoped activating proper workbook solve issue, doesn't appear have helped.
ok, after couple of hours of searching web alternative methods achieve goal, stumbled across this post.
i created template workbook (with event code in thisworkbook
), password protected project, , modified code use template workbook each new sheet. when sheets created, project locked viewing , requires password. while realize security under approach isn't secure, "keep honest people honest" say.
for stumble across post , still wish programmatically lock/unlock vba project, see these resources:
both great resources walk through way in vba.
I was so happy to discover this great website. I want to thank you for taking the time to
ReplyDeleteread! ! I have definitely tried every part and saved my favorites to see new information on your blog.
jetbrains phpstorm crack
progdvb professional
easeus partition master 13 5 crack
gamemaker studio 2 crack
cyberlink powerdvd crack