excel - VBA: How can I password protect modules using code? -
i have file saves copy of go recipients, end many files contain recipient-specific information , original maste file contains information. when recipient-specific files made, have code deletes information related recipient , locks down workbook , sheets randomly made password using below function:
function pwd(ilength integer) string dim integer, itemp integer, bok boolean, strtemp string '48-57 = 0 9, 65-90 = z, 97-122 = z 'amend other characters if required = 1 ilength itemp = int((122 - 48 + 1) * rnd + 48) select case itemp case 48 57, 65 90, 97 122: bok = true case else: bok = false end select loop until bok = true bok = false strtemp = strtemp & chr(itemp) next pwd = strtemp end function
is possible lock down modules cannot edited? want same functionality excel provide in visual basic going tools -> vbaproject - project properties -> protection, through code can applied each of recipient specific files.
i can apply protection sheets using code like:
sheets(1).protect password, true, true
and workbook code like:
activeworkbook.protect password, true, false
but there can use lock down modules?
notwithstanding advice given security of excel passwords/protection, , comprehensive (non-sendkeys) solution linked carl colijn, have used dirty sendkeys method myself success - see example below, , here more details on sendkeys. ymmv etc.
note have find option "trust access vba project object model" in excel trust center > macro settings, or equivalent version of excel
sub unprotectvbproj(byref wb workbook, byval pwd string) dim vbproj object set vbproj = wb.vbproject if vbproj.protection <> 1 exit sub ' unprotected set application.vbe.activevbproject = vbproj sendkeys "%te" & pwd & "~~" end sub sub protectvbproj(byref wb workbook, byval pwd string) dim vbproj object set vbproj = wb.vbproject if vbproj.protection = 1 exit sub ' protected set application.vbe.activevbproject = vbproj sendkeys "%te+{tab}{right}%v%p" & pwd & "%c" & pwd & "{tab}{enter}" end sub
Comments
Post a Comment