博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

.net 开发office应用程序-宏安全等级设置

Posted on 2006-07-03 13:15  james.dong  阅读(386)  评论(0)    收藏  举报

我在用.net 开发 office应用程序的过程中,遇到了一个关于宏运行的问题,一直困牢着我,这几天,我上google,搜索了一下,找到了一个解决的方法,在这里,我把这个方法写在这里,希望对有类似问题的朋友有帮助。
我是通过修改注册表的值来控制宏的安全等级的,代码如下:

using System;
using Microsoft.Win321;
using System.Runtime.InteropServices;

public class RegisterMrg
{
    public RegisterMrg()
    {
    }

    public void ModifySecurityLevel()
    {
        try
           {
                string wordPath = @"Software\Microsoft\Office\9.0\Word\Security";   //office2000
                Microsoft.Win32.RegistryKey Root = Registry.CurrentUser;
                Microsoft.Win32.RegistryKey sub = Root.OpenSubKey( wordPath , true );
                if( sub != null )
                {
                     sub.SetValue( "DontTrustInstalledFiles" , 0);   //信任所有安装的加载项和模板
                     sub.SetValue( "Level" , 1 );                             //加载安全性等级为最低
                }

                wordPath = @"Software\Microsoft\Office\10.0\Word\Security";   //officexp
                Root = Registry.CurrentUser;
                sub = Root.OpenSubKey( wordPath , true);
                if( sub != null )
                {
                     sub.SetValue( "Level" ,1 );
                     sub.SetValue( "AccessVBOM" , 1);       //信任vb project工程
                }
                wordPath = @"Software\Microsoft\Office\11.0\Word\Security";  //office2003
                Root = Registry.CurrentUser;
                sub = Root.OpenSubKey( wordPath , true);
                if( sub != null )
                {
                     sub.SetValue( "Level" , System.Convert.ToInt32( 1) );
                     sub.SetValue( "AccessVBOM" , 1 );
                }
           }
           catch( System.Exception ex )
           {
                LogMrg.WriteLog( ex.Message ); 
               // throw( ex );
           }
           finally
           {
           }
     }
}