EXCEL VBA: How to set a password to another VBProject programmaticaly -
i have created vba code creates new .xlms file, adding code file , problem cannot protect vbpoject of new file. instead, wrongly password protect current vbproject (not new one, want). here code:
sub create_xlsm_file() dim vbproj vbide.vbproject dim vbcomp vbide.vbcomponent dim codemod vbide.codemodule dim linenum long dim modulename string dim newprocasstring string dim mydir1 string dim filename1 string dim folderpath1 string dim filepath1 string dim pass1 string dim sheetname1filename1 string dim mastername string dim noofsheets integer dim newbook1 workbook mastername = environ("username") mydir1 = "c:\users\" & mastername & "\desktop" filename1 = "lockedvbaproject" pass1 = "123" noofsheets = 1 sheetname1filename1 = "sh1" modulename = "module1" '----creating , save file------------------------------------------- set newbook1 = workbooks.add newbook1.activate filepath1 = mydir1 & "\" & filename1 application.sheetsinnewworkbook = noofsheets activeworkbook.sheets(1).name = sheetname1filename1 newbook1.saveas filename:=filepath1, fileformat:=xlopenxmlworkbookmacroenabled, password:=pass1 workbooks(filename1).close false '----add vba code file------------------------------------- application.workbooks.open (filepath1), password:=pass1 workbooks(filename1).activate '===================================================================== 'this part problem. sendkeys applied current project , ' not desired "lockedvbaproject" '===================================================================== application '//execute controls lock project\\ .vbe.commandbars("menu bar").controls("tools") _ .controls("vbaproject properties...").execute '//activate 'protection'\\ .sendkeys "^{tab}", true '//caution: either checks or unchecks the\\ '//"lock project viewing" checkbox, if it's already\\ '//been locked viewing, unlock it\\ .sendkeys "{ }", true '//enter password (password 123 in example)\\ .sendkeys "{tab}" & "123", true '//confirm password\\ .sendkeys "{tab}" & "123", true '//scroll down ok key\\ .sendkeys "{tab}", true '//click ok key\\ .sendkeys "{enter}", true 'the project locked - takes effect 'the next time book's opened... end '===================================================================== '===================================================================== '===================================================================== set vbproj = activeworkbook.vbproject set vbcomp = vbproj.vbcomponents.add(vbext_ct_stdmodule) vbcomp.name = modulename set vbcomp = vbproj.vbcomponents("thisworkbook") set codemod = vbcomp.codemodule codemod linenum = .createeventproc("open", "workbook") linenum = linenum + 2 newprocasstring = "msgbox ""hola !!!""" codemod.insertlines linenum, newprocasstring end workbooks(filename1).save workbooks(filename1).close false thisworkbook.activate end sub
can please me select , password protect desired new "lockedvbaproject" file? other suggestions? in advance time.
Comments
Post a Comment