关闭

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

3630人阅读 评论(0) 收藏 举报

'作者: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

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:56145次
    • 积分:868
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:9篇
    • 译文:0篇
    • 评论:12条
    文章分类