MSOffice小知识:宏安全级别的更改

Author:水如烟

因为安全审核,一般用Access打开一个文件时显示安全警告,作了选择后才进行下去,用代码打开也一样。因此,为取消这个选择,不让它显示出来,需要将安全级别设置为最低,但是,程序退出之前,必须记住设回默认值。

在Office2003,可以直接读取和设置安全级别。

Namespace  uOffice

    
Public   Enum  MsoAutomationSecurityEnum
        SecurityLow 
=   1
        SecurityByUI 
=   2
        SecurityForceDisable 
=   3
    
End Enum

End Namespace

ApplicationBaseCommon.vb
         Public   Property  AutomationSecurity()  As  MsoAutomationSecurityEnum
            
Get
                
Return   Me .gApplicationObject.AutomationSecurity
            
End   Get
            
Set ( ByVal  value  As  MsoAutomationSecurityEnum)
                
Me .gApplicationObject.AutomationSecurity  =  value
            
End   Set
        
End Property

而在之前的版本,不知是否有直接设置的属性。这里提供操作注册表的方法:
         ' '' <summary>
         ' '' 宏安全级别
         ' '' </summary>
         ' '' <remarks>低1,中2,高3,非常高4</remarks>
         Public   Property  MacroSecurityLevel()  As   Integer
            
Get
                
Return  SecurityLevelKey.GetValue( " Level " )
            
End   Get
            
Set ( ByVal  value  As   Integer )
                SecurityLevelKey.SetValue(
" Level " , value, Microsoft.Win32.RegistryValueKind.DWord)
            
End   Set
        
End Property

        
' '' <summary>
         ' '' 宏安全级别注册表键
         ' '' </summary>
         Private   Function  SecurityLevelKey()  As  Microsoft.Win32.RegistryKey
            
Dim  strKey  As   String   =   " Software\Microsoft\Office\{0}\{1}\Security "   ' {0}版本号
            strKey  =   String .Format(strKey,  Me .Version,  Me .gOfficeApplication.ToString)
            
Dim  rk  As  Microsoft.Win32.RegistryKey
            rk 
=  Microsoft.Win32.Registry.CurrentUser.OpenSubKey(strKey,  True )

            
If  rk  Is   Nothing   Then
                rk 
=  Microsoft.Win32.Registry.CurrentUser.CreateSubKey(strKey, Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree)
            
End   If

            
If  Array.IndexOf(rk.GetValueNames,  " Level " =   - 1   Then
                rk.SetValue(
" Level " 2 , Microsoft.Win32.RegistryValueKind.DWord)  ' 默认为中级
             End   If

            
Return  rk
        
End Function

这里要注意的,注册表的值在更改后,只有重新启动Access才生效。
所以,CreateInstance也要修改一下:
         Private   Sub  CreateInstance()
            
' 实例一,用于取默认参数,并保存到有关变量,再初始某些参数。多作用于注册表。

            gBeforeProcessStartTime 
=  Now
            
Select   Case  gOfficeApplication
                
Case  ApplicationEnum.Access
                    gApplicationObject 
=   CreateObject (SR.GetString( " Office_Application_Access " ), gServer)
                
Case  ApplicationEnum.Excel
                    gApplicationObject 
=   CreateObject (SR.GetString( " Office_Application_Excel " ), gServer)
                
Case  ApplicationEnum.Word
                    gApplicationObject 
=   CreateObject (SR.GetString( " Office_Application_Word " ), gServer)
            
End   Select
            gAfterProcessStartTime 
=  Now

            
' 读取和保留默认配置
            SaveDefaultPropertiesWhenApplicationInitialize()

            
' 退出本参考实例
            Application_Quit()
            
Try
                ApplicationRelease()
            
Catch  ex  As  Exception
            
End   Try

            
' 初始有关参数,多作用于注册表
            InitializeDefaultPropertyiesWhenApplicationStart()

            
' 实例二
             ' 取实例前时间
            gBeforeProcessStartTime  =  Now

            
' 实例
             Select   Case  gOfficeApplication
                
Case  ApplicationEnum.Access
                    gApplicationObject 
=   CreateObject (SR.GetString( " Office_Application_Access " ), gServer)
                
Case  ApplicationEnum.Excel
                    gApplicationObject 
=   CreateObject (SR.GetString( " Office_Application_Excel " ), gServer)
                
Case  ApplicationEnum.Word
                    gApplicationObject 
=   CreateObject (SR.GetString( " Office_Application_Word " ), gServer)
            
End   Select

            
' 取实例后时间
            gAfterProcessStartTime  =  Now
        
End Sub

还好,我的是2003,不必来个“实例一”。仅供参考。

你可能感兴趣的:(Office)