[VBA] Use VBA macro to automatically change the Excel macro security level settings

Source: Internet
Author: User
Option explicit
Sub setexcelvba ()
'Exercise
'Function: Change the Excel Security Level
'Use: wscript, FileSystemObject, create TXT file, registry operation, vbs file self-deletion, change Excel file read/write attributes, etc.
By Keanjeason@Officefans.net
Dim wsh as object, RET as string, regstr as string
Dim strfullname as string, strvbs as string
Dim TF, FSO, retval
'The program only applies to Excel 2003 (11.0). Exit if the current version is not 2003
If application. version <> "11.0" then msgbox "this code is only available in Excel 2003! ", Vbokonly + vbcritical," keanjeason ": Exit sub
Strfullname = thisworkbook. fullname 'get the full name of the current workbook
Strvbs = Replace (ucase (strfullname), ". xls", ". vbs") 'temp file name of vbs
Set wsh = Createobject ("wscript. Shell") 'to create a wscript object
Err. Clear
On Error resume next
Regstr = "HKEY_CURRENT_USER/software/Microsoft/office/11.0/EXCEL/security/level" 'excel VBA security level position in the Registry
Ret = wsh. regread (regstr) 'reads the current security level
If err. Number <> 0 then
'Judge whether the read is successful
Msgbox "An error occurred while reading the current Excel VBA security level settings from the Registry. This program will exit! ", Vbokonly + vbcritical," keanjeason"
Exit sub
Else
'If the current Excel VBA security level is not "low", it is set to "low". values 1-4 correspond to: low, medium, high, and very high.
If Val (RET) <> 1 then ret = wsh. regwrite (regstr, "1", "REG_DWORD ")
End if
Set FSO = Createobject ("scripting. FileSystemObject ")
Set TF = FSO. createtextfile (strvbs, true) 'create the temp file vbs File
With TF
'Write vbs File Content
. Writeline ("dim oexcel, FSO, delme ")
. Writeline ("set FSO = Createobject (" "scripting. FileSystemObject "")")
. Writeline ("set oexcel = Createobject (" "Excel. Application "")")
. Writeline ("oexcel. workbooks. Open" & CHR (34) & strfullname & CHR (34 ))
. Writeline ("oexcel. Visible = true ")
. Writeline ("set oexcel = nothing ")
. Writeline ("delme = FSO. deletefile (" & CHR (34) & strvbs & CHR (34 )&")")
. Close
End
With thisworkbook
'Set the attribute of the current file to "read-only" to facilitate re-opening
. Changefileaccess mode: = xlreadonly
. Saved = true
End
Retval = wsh. Run (CHR (34) & strvbs & CHR (34), 1, true) 'Run the created vbs file and start a new EXCEL program
Application. Quit 'exit the current Excel File
Set wsh = nothing
Set FSO = nothing
End sub

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.