如何用VB 设置OFFICE 2003程序 启动时 是否启用宏的提示--设置安全级别

原创 2006年06月21日 14:37:00

'作者:CSDN 许仙
'Homepage : jjweb.126.com
'MSN :Coderxu#hotmail.com
'QQ:19030300
'转载请保持文章完整,保存以上作者信息 请珍惜他人劳动成果

如何用VB 设置OFFICE 2003程序 启动时,是否启用宏! 点击菜单[工具(T)]--[宏(M)]--[安全性(S)].当然这里是如何用代码设置..
'设置注册表 (运行时不适用):
    '[HKEY_CURRENT_USER/Software/Microsoft/Office/11.0/Access/Security]
    '"Level"=1 //低
    '"Level"=2 //中
    '"Level"=3 //高
'以下代码是如何设置注册表的键值


Option Explicit
'直接调用 SetAccessMacro 就可以设置
'Sub Main()
'    '函数在注册表的"HKEY_CURRENT_USER/Software"中建立了
'    '一个SubKey1项并在其中建立了值,并在显示后删除建立
'    '的值,如果你想通过RegEdit看到结果,可以将最后两句
'    '删除,不过要记得手动删除建立的键值
'    CreateNewKey HKEY_CURRENT_USER, "Software/SubKey1/SubKey2"
'    SetKeyValue HKEY_CURRENT_USER, "Software/SubKey1/SubKey2", "Test", "This is just a test", REG_SZ
'    MsgBox QueryValue(HKEY_CURRENT_USER, "Software/SubKey1/SubKey2", "Test")
'    DeleteValue HKEY_CURRENT_USER, "Software/SubKey1/SubKey2", "Test"
'    DeleteKey HKEY_CURRENT_USER, "Software/SubKey1/SubKey2"
'End Sub
Global Const REG_SZ As Long = 1
Global Const REG_DWORD As Long = 4
Global Const HKEY_CLASSES_ROOT = &H80000000
Global Const HKEY_CURRENT_USER = &H80000001
Global Const HKEY_LOCAL_MACHINE = &H80000002
Global Const HKEY_USERS = &H80000003
Global Const ERROR_NONE = 0
Global Const ERROR_BADDB = 1
Global Const ERROR_BADKEY = 2
Global Const ERROR_CANTOPEN = 3
Global Const ERROR_CANTREAD = 4
Global Const ERROR_CANTWRITE = 5
Global Const ERROR_OUTOFMEMORY = 6
Global Const ERROR_INVALID_PARAMETER = 7
Global Const ERROR_ACCESS_DENIED = 8
Global Const ERROR_INVALID_PARAMETERS = 87
Global Const ERROR_NO_MORE_ITEMS = 259
Global Const KEY_ALL_ACCESS = &H3F
Global Const REG_OPTION_NON_VOLATILE = 0
Declare Function RegCloseKey _
        Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKeyEx _
        Lib "advapi32.dll" _
        Alias "RegCreateKeyExA" (ByVal hKey As Long, _
                                 ByVal lpSubKey As String, _
                                 ByVal Reserved As Long, _
                                 ByVal lpClass As String, _
                                 ByVal dwOptions As Long, _
                                 ByVal samDesired As Long, _
                                 ByVal lpSecurityAttributes As Long, _
                                 phkResult As Long, _
                                 lpdwDisposition As Long) As Long
Declare Function RegOpenKeyEx _
        Lib "advapi32.dll" _
        Alias "RegOpenKeyExA" (ByVal hKey As Long, _
                               ByVal lpSubKey As String, _
                               ByVal ulOptions As Long, _
                               ByVal samDesired As Long, _
                               phkResult As Long) As Long
Declare Function RegQueryValueExString _
        Lib "advapi32.dll" _
        Alias "RegQueryValueExA" (ByVal hKey As Long, _
                                  ByVal lpValueName As String, _
                                  ByVal lpReserved As Long, _
                                  lpType As Long, _
                                  ByVal lpData As String, _
                                  lpcbData As Long) As Long
Declare Function RegQueryValueExLong _
        Lib "advapi32.dll" _
        Alias "RegQueryValueExA" (ByVal hKey As Long, _
                                  ByVal lpValueName As String, _
                                  ByVal lpReserved As Long, _
                                  lpType As Long, _
                                  lpData As Long, _
                                  lpcbData As Long) As Long
Declare Function RegQueryValueExNULL _
        Lib "advapi32.dll" _
        Alias "RegQueryValueExA" (ByVal hKey As Long, _
                                  ByVal lpValueName As String, _
                                  ByVal lpReserved As Long, _
                                  lpType As Long, _
                                  ByVal lpData As Long, _
                                  lpcbData As Long) As Long
Declare Function RegSetValueExString _
        Lib "advapi32.dll" _
        Alias "RegSetValueExA" (ByVal hKey As Long, _
                                ByVal lpValueName As String, _
                                ByVal Reserved As Long, _
                                ByVal dwType As Long, _
                                ByVal lpValue As String, _
                                ByVal cbData As Long) As Long
Declare Function RegSetValueExLong _
        Lib "advapi32.dll" _
        Alias "RegSetValueExA" (ByVal hKey As Long, _
                                ByVal lpValueName As String, _
                                ByVal Reserved As Long, _
                                ByVal dwType As Long, _
                                lpValue As Long, _
                                ByVal cbData As Long) As Long
Private Declare Function RegDeleteKey& _
                Lib "advapi32.dll" _
                Alias "RegDeleteKeyA" (ByVal hKey As Long, _
                                       ByVal lpSubKey As String)
Private Declare Function RegDeleteValue& _
                Lib "advapi32.dll" _
                Alias "RegDeleteValueA" (ByVal hKey As Long, _
                                         ByVal lpValueName As String)

Public Function DeleteKey(lPredefinedKey As Long, _
                          sKeyName As String)
    Dim lRetVal As Long
    Dim hKey As Long
    lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
    lRetVal = RegDeleteKey(lPredefinedKey, sKeyName)
    RegCloseKey (hKey)
End Function

Public Function DeleteValue(lPredefinedKey As Long, _
                            sKeyName As String, _
                            sValueName As String)
    Dim lRetVal As Long
    Dim hKey As Long
    lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
    lRetVal = RegDeleteValue(hKey, sValueName)
    RegCloseKey (hKey)
End Function

Public Function SetValueEx(ByVal hKey As Long, _
                           sValueName As String, _
                           lType As Long, _
                           vValue As Variant) As Long
    Dim lValue As Long
    Dim sValue As String
    Select Case lType
        Case REG_SZ
            sValue = vValue
            SetValueEx = RegSetValueExString(hKey, sValueName, 0&, lType, sValue, Len(sValue))
        Case REG_DWORD
            lValue = vValue
            SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, lType, lValue, 4)
    End Select
End Function

Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As String, vValue As Variant) As Long
    Dim cch As Long
    Dim lrc As Long
    Dim lType As Long
    Dim lValue As Long
    Dim sValue As String
    On Error GoTo QueryValueExError
    lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
    If lrc <> ERROR_NONE Then Error 5
    Select Case lType
        Case REG_SZ:
            sValue = String(cch, 0)
            lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)
            If lrc = ERROR_NONE Then
                vValue = Left$(sValue, cch)
            Else
                vValue = Empty
            End If
        Case REG_DWORD:
            lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)
            If lrc = ERROR_NONE Then vValue = lValue
        Case Else
            lrc = -1
    End Select
QueryValueExExit:
    QueryValueEx = lrc
    Exit Function
QueryValueExError:
    Resume QueryValueExExit
End Function

Public Function CreateNewKey(lPredefinedKey As Long, _
                             sNewKeyName As String)
    Dim hNewKey As Long
    Dim lRetVal As Long
    lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hNewKey, lRetVal)
    RegCloseKey (hNewKey)
End Function

Public Function SetKeyValue(lPredefinedKey As Long, _
                            sKeyName As String, _
                            sValueName As String, _
                            vValueSetting As Variant, _
                            lValueType As Long)
    Dim lRetVal As Long
    Dim hKey As Long
    lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
    lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
    RegCloseKey (hKey)
End Function

Public Function QueryValue(lPredefinedKey As Long, _
                           sKeyName As String, _
                           sValueName As String)
    Dim lRetVal As Long
    Dim hKey As Long
    Dim vValue As Variant
    lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
    lRetVal = QueryValueEx(hKey, sValueName, vValue)
    QueryValue = vValue
    RegCloseKey (hKey)
End Function

Public Sub SetAccessMacro()
    'ACCESS2003版本加了宏安全性级别的设置,设置de用处在此不表。方法:
    '操作菜单
    '可以自定义菜单,或直接调用菜单上的操作:
    'CommandBars("menu bar").Controls("工具(&T)").Controls("宏(&M)").Controls("安全性(&S)...").Execute
    '设置注册表 (运行时不适用):
    '[HKEY_CURRENT_USER/Software/Microsoft/Office/11.0/Access/Security]
    '"Level"=1 //低
    '"Level"=2 //中
    '"Level"=3 //高
    '设置ACCESS的安全级别 有宏的MDB运行时,弹出的是否启用宏
    If InStr(QueryValue(HKEY_CURRENT_USER, "Software/Microsoft/Office/11.0/Access/Security", "Level"), "1") = 0 Then
        SetKeyValue HKEY_CURRENT_USER, "Software/Microsoft/Office/11.0/Access/Security", "Level", "1", REG_DWORD
    End If
End Sub

 

[VBA]用VBA宏自动改变Excel宏安全级别设置

Option ExplicitSub SetExcelVBA()练习功能:改变Excel的安全级别使用:Wscript,FileSystemObject,创建txt文件,注册表操作,VBS文件自我删除...
  • szwangdf
  • szwangdf
  • 2007年02月12日 09:11
  • 5477

Office EXCEL 2010如何启用宏编辑器,打开VB编辑器

文件-选项-主选项卡,勾选开发工具 然后在开发工具中找到Visual Basic编辑器,打开代码...
  • acetaohai123123
  • acetaohai123123
  • 2017年10月17日 21:52
  • 408

解决打开PPT 提示:Windows 正在设置 Microsoft office professional Edition 2003

直接如下图:
  • li0531
  • li0531
  • 2014年05月23日 17:18
  • 3820

EXECL2003宏被禁用的解决办法

如果您的EXECL2003宏被禁用,有以下几种解决办法 利用EXCEL的宏安全性问题 在菜单中选择:工具--选项--宏--安全性,设置成“低”或“中”,再打开文件,宏会提示使用“宏”,按“启动...
  • sxycgxj
  • sxycgxj
  • 2011年09月30日 12:15
  • 5518

图文详细解决office2010弹出:“此错误通常是由宏安全 性设置造成。。。。”窗口的问题

我用的office 2010,先前一直很破烦,每次用着用着,就弹出一个对话框,提示“此错误通常是由宏安全性设置造成的。如果您知道宏来自您信任的来源,则可将宏安全。。。。”...
  • rl529014
  • rl529014
  • 2015年11月29日 20:12
  • 2015

如果宏安全性设置为“禁用所有宏,并且不通知”,将自动禁用所有宏。使用下列步骤可启用宏。

打开world2007,出现如下错误提示
  • zhangbest2009
  • zhangbest2009
  • 2014年09月03日 16:12
  • 2895

VB、Office与VBA

在VB程序设计中我们经常需要用到其他的程序比如Offic产品中的Word、Excel等。下面我们就来说一下VB与Office。        比如我们需要在VB中应用Excel,首先打开VB—工程—...
  • kanglix1an
  • kanglix1an
  • 2012年08月19日 20:35
  • 3542

如何设置office2003为默认打开方式

如何设置office2003为默认打开方式 当系统同时安装 office 2003和 office 2007(或2010)两个版本的 office办公软件的时候,双击打开一个office文档(W...
  • lyc_daniel
  • lyc_daniel
  • 2013年06月25日 14:48
  • 28530

windows 2003中IE无法更改安全级别,如何解决?

Win2003上网一直提示安全问题,但在IE中的“Internet选项”对话框中选择“安全”标签,并把“该区域的安全级别”滚动条设置为“中”却一直不成功,始终处在高级别,不知怎样才能把它拉下来到中的位...
  • cnssk
  • cnssk
  • 2008年12月04日 16:18
  • 4268

如何实现打开有宏的EXCEL时不提示

测试环境:  OS : Windows98 SE 简体中文版  App : Excel 2000 简体中文版 对于Excel中包含VBA的文档,大家最恐惧的一件事情便是是否病毒,因此往往把Excel...
  • lang_csdn
  • lang_csdn
  • 2008年10月22日 17:48
  • 858
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何用VB 设置OFFICE 2003程序 启动时 是否启用宏的提示--设置安全级别
举报原因:
原因补充:

(最多只允许输入30个字)