Enable Disable Macro Protection
Prep for a pre-employment Excel test with Job Test Prep
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 8 of 8

Thread: Enable Disable Macro Protection

  1. #1
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Christchurch New Zealand
    Posts
    1,030
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

     
    I have some code for turning off Macro protection in 2000 does any one have any code for office xp?


    Option Compare Text
    Option Explicit

    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal lhKey As Long) As Long
    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal lhKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal lhKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    Private Const REG_HEX As Long = 4 'Hex Key
    Private Const HKEY_CURRENT_USER As Long = &H80000001


    'Purpose : Enables or disables the macro virus alert by altering the security level in the registry.
    'Inputs : lSecurityLevel 1, sets security to "Low" (disable virus alerts)
    ' 2, sets security to "Medium"
    ' 3, sets security to "High"

    Function MacroSecurity2000(lSecurityLevel As Long) As Boolean
    Dim sData As String, lRet As Long
    Const csPath = "SoftwareMicrosoftOffice9.0ExcelSecurity", csValue = "Level"

    If lSecurityLevel <= 3 And lSecurityLevel > 0 Then
    On Error GoTo ErrFailed
    RegCreateKey HKEY_CURRENT_USER, csPath, lRet
    RegSetValueEx lRet, csValue, 0, REG_HEX, lSecurityLevel, 4
    RegCloseKey lRet
    MacroSecurity2000 = True
    End If

    Exit Function
    ErrFailed:
    MacroSecurity2000 = False
    End Function

    [ This Message was edited by: brettvba on 2002-02-21 11:52 ]

  2. #2
    New Member
    Join Date
    May 2002
    Location
    Carol Maas
    Posts
    20
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Does this change the Security Level as though the user was doing it manually? I didn't think that was possible through VBA code. Does this mean you could change the users settings to Low Security so that it would automatically enable macros?

    Thx.

  3. #3
    Board Regular
    Join Date
    Mar 2002
    Location
    Sydney/Brisbane , Australia
    Posts
    538
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    BrettVBA. have you been keeping this a neat little secret ?

    does it work in excel 97? or is there something similar?

  4. #4
    New Member
    Join Date
    Jun 2002
    Posts
    3
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    One problem buddy boy.
    The user will need to disable macros protection in order to run your code to disable macros protection. DUH!

  5. #5
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    On 2002-06-18 22:58, Qroozn wrote:
    BrettVBA. have you been keeping this a neat little secret ?

    does it work in excel 97? or is there something similar?
    No, it won't work in excel97...the Regestration key and the Bit to change are different.
    Kind Regards,
    Ivan F Moala From the City of Sails

  6. #6
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    On 2002-06-18 23:28, Mo~yu wrote:
    One problem buddy boy.
    The user will need to disable macros protection in order to run your code to disable macros protection. DUH!
    You can overcome this in a number of ways.
    Kind Regards,
    Ivan F Moala From the City of Sails

  7. #7
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Christchurch New Zealand
    Posts
    1,030
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    This is the code for XP
    and your right its not made to hack ppls computers so that they can't choose if they want macros enabled its just a useful piece of code!

    I can lookup my 97 ver if you need it!

    Rgds Brett

    Option Compare Text
    Option Explicit

    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal lhKey As Long) As Long
    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal lhKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal lhKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    Private Const REG_HEX As Long = 4 'Hex Key
    Private Const HKEY_CURRENT_USER As Long = &H80000001


    Function MacroSecurity2002(lSecurityLevel As Long) As Boolean
    Dim sData As String, lRet As Long
    Const csPath = "SoftwareMicrosoftOffice10.0ExcelSecurity", csValue = "Level"

    If lSecurityLevel <= 3 And lSecurityLevel > 0 Then
    On Error GoTo ErrFailed
    RegCreateKey HKEY_CURRENT_USER, csPath, lRet
    RegSetValueEx lRet, csValue, 0, REG_HEX, lSecurityLevel, 4
    RegCloseKey lRet
    MacroSecurity2002 = True
    End If

    Exit Function
    ErrFailed:
    MacroSecurity2002 = False
    End Function

    Sub changeit()
    MacroSecurity2002 (1) 'changes to low
    'Also note that 1,2 or 3 can be used low, medium and high security!
    End Sub


    [ This Message was edited by: brettvba on 2002-06-19 13:26 ]

  8. #8
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Christchurch New Zealand
    Posts
    1,030
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

      
    This should do it for 97 untested as I don't have 97

    Regards Brett

    Option Compare Text
    Option Explicit

    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal lhKey As Long) As Long
    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal lhKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal lhKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    Private Const REG_HEX As Long = 4 'Hex Key
    Private Const HKEY_CURRENT_USER As Long = &H80000001
    Function MacroSecurity97(bDisableVirusChecking As Boolean) As Boolean
    Dim lData As Long, lRet As Long
    Const csPath = "SoftwareMicrosoftOffice8.0ExcelMicrosoft Excel", csValue = "Options6"

    On Error GoTo ErrFailed
    If bDisableVirusChecking Then
    lData = 0 'Disabled
    Else
    lData = 8 'Enabled
    End If
    RegCreateKey HKEY_CURRENT_USER, csPath, lRet
    RegSetValueEx lRet, csValue, 0, REG_HEX, lData, 4
    RegCloseKey lRet
    MacroSecurity97 = True
    Exit Function
    ErrFailed:
    MacroSecurity97 = False
    End Function

    Sub Change97()
    MacroSecurity97 (True)
    End Sub

    _________________



    [ This Message was edited by: brettvba on 2002-06-19 13:23 ]

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 

 
DMCA.com