Logo 
Search:

MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

Programatically enabling macro security

  Asked By: Lucas    Date: Oct 03    Category: MS Office    Views: 2352
  

As we all know that we can run a macro in a Excel-Workbook without
seeing any message only if the Macro Security is set to "Low" as well
if we are adding some code programatically we must have "Trust Access
to Visual Basic Object" option checked in Tools-Options-Security-
Macro Security-Trusted Source tab'.

My questions are :
Is there a way to set these options programatically?
How could we add some code programatically to Personal Workbook on
such a system where Macro Security is set to "High" and "Trust Access
to Visual Basic Object" option is Unchecked, without enabling it
manually?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Daw Boonliang     Answered On: Oct 03

Here is some code  and information for you to try to work with,
as I located it for you via Google's Search Engine.


'===============================================================================\
================================================
'Purpose : Enables or disables the macro  virus alert(Excel97)
'Inputs : bDisableVirusChecking
' True, disables macro security  protection
' False, enables macro security protection
'Outputs : Returns True on success
'===============================================================================\
================================================
Function Excel97MacroSecurity(bDisableVirusChecking As Boolean) As Boolean
Dim lData As Long, lRet As Long
Const csPath = "Software\Microsoft\Office\8.0\Excel\Microsoft 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
Excel97MacroSecurity = True

Exit Function

ErrFailed:
Debug.Print Err.Description
Excel97MacroSecurity = False
End Function


'Purpose : Enables or disables the macro virus alert by altering the security
level in the registry(Excel2000, Excel2003, ExcelXP)
'Inputs : lSecurityLevel 1, sets security to "Low" (disable virus
alerts)
' 2, sets security to "Medium"
' 3, sets security to "High"
' 4, sets security to "High" and
disables access to VB Object Model
' AppType The application to set  the security for.
' OfficeVersion The installed version of Office.
'Outputs : Returns True on success


Function OfficeMacroSecurity(lSecurityLevel As Long, AppType As eApplication,
OfficeVersion As eVersion) As Boolean
Dim sData As String, lRet As Long, sAppKey As String
Const csKeyGen As String =
"Software\Microsoft\Office\[VERSION]\[APPLICATION]\Security"
Const csKeyVBOM As String = "AccessVBOM", csKey As String = "Level"

If lSecurityLevel <= 4 And lSecurityLevel > 0 Then
On Error GoTo ErrFailed
Select Case OfficeVersion
Case eOffice2000
sAppKey = Replace$(csKeyGen, "[VERSION]", "9.0")
Case eOfficeXP
sAppKey = Replace$(csKeyGen, "[VERSION]", "10.0")
Case eOffice2003
sAppKey = Replace$(csKeyGen, "[VERSION]", "11.0")
Case Else
Debug.Print "Invalid version"
Debug.Assert False
OfficeMacroSecurity = False
Exit Function
End Select

Select Case AppType
Case eExcel
sAppKey = Replace$(sAppKey, "[APPLICATION]", "Excel")
Case eOutlook
sAppKey = Replace$(sAppKey, "[APPLICATION]", "Outlook")
Case ePowerPoint
sAppKey = Replace$(sAppKey, "[APPLICATION]", "PowerPoint")
Case ePublisher
sAppKey = Replace$(sAppKey, "[APPLICATION]", "Publisher")
Case eWord
sAppKey = Replace$(sAppKey, "[APPLICATION]", "Word")
Case Else
Debug.Print "Invalid application"
Debug.Assert False
OfficeMacroSecurity = False
Exit Function
End Select

RegCreateKey HKEY_CURRENT_USER, sAppKey, lRet
'Set the registry key macro security reg key
If lSecurityLevel = 4 Then
'Set to high
RegSetValueEx lRet, csKey, 0, REG_HEX, 3, 4
Else
'Set to the specified level
RegSetValueEx lRet, csKey, 0, REG_HEX, lSecurityLevel, 4
End If

If lSecurityLevel < 4 Then
'Enable access to VB Object Model
RegSetValueEx lRet, csKeyVBOM, 0, REG_HEX, 1, 4
Else
'Disable access to VB Object Model
RegSetValueEx lRet, csKeyVBOM, 0, REG_HEX, 0, 4
End If

RegCloseKey lRet

OfficeMacroSecurity = True
Else
Debug.Print "Invalid Security Level"
Debug.Assert False
End If

Exit Function

ErrFailed:
Debug.Print Err.Description
OfficeMacroSecurity = False
End Function

'These are the keys in registry(Excel97 or Excel 8.0)
'Macro Virus protection is saved in
'HKEY_USERS\.DEFAULT\Software\Microsoft\Office\8.0\Excel\Microsoft Excel\Option6
'0 x00000008 (8) ' All Files with Macro can run  here..

'ExcelXP or Excel 10.0
'Macro virus protection' in General tab  is saved in..
'HKEY_USERS\.DEFAULT\Software\Microsoft\Office\10.0\Excel\Security
'Level=0x00000001 (1) ' All Files with Macro can run here..
'Level=0x00000002 (2) ' Warn me before openning any File containing Macro..
'Level=0x00000003 (3) ' Do not trust any file with Macro..

 
Didn't find what you were looking for? Find more on Programatically enabling macro security Or get search suggestion and latest updates.