如何使用VBA编辑(默认)或(标准)RegKey

时间:2019-06-06 18:01:05

标签: excel vba registrykey

我正在创建一个需要始终在新实例中打开的Excel。为此,我决定在VBA打开时通过VBA编辑一些RegKey。

我的问题是编辑一个没有名称,(默认)或(标准)的RegKey。

要添加普通的RegKey,我没有问题:

RegKeyPath = "HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Excel\Options\DisableMergeInstance"

默认的RegKey根本不起作用:

RegKeyPath = "HKEY_CLASSES_ROOT\Excel.Sheet.12\shell\Open\command\(Default)"

我的代码:

Private Sub RegKeyUpdate()

    Dim RegKeyPath As String
    Dim RegKeyValue As String
    Dim RegKeyType As String

    RegKeyPath = "HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Excel\Options\DisableMergeInstance"
    RegKeyValue = "1"
    RegKeyType = "REG_DWORD"

    RegKeySave RegKeyPath, RegKeyValue, RegKeyType

    RegKeyPath = "HKEY_CLASSES_ROOT\Excel.Sheet.12\shell\Open\command\(Default)"
    RegKeyValue = RegKeyRead(RegKeyPath) & " /x"
    RegKeyType = "REG_SZ"

    RegKeySave RegKeyPath, RegKeyValue, RegKeyType

End Sub
Function RegKeyRead(RegKeyPath As String) As String

    Dim objRegistryKey As Object

    On Error GoTo ErrorHandler

    Set objRegistryKey = CreateObject("WScript.Shell")

    RegKeyRead = objRegistryKey.RegRead(RegKeyPath)

    If 1 <> 1 Then
ErrorHandler:
        On Error GoTo 0
        On Error GoTo ErrorHandler
    End If

End Function
Public Sub RegKeySave(RegKeyPath As String, RegKeyValue As String, Optional RegKeyType As String)

    Dim objRegistryKey As Object

    On Error GoTo ErrorHandler

    Set objRegistryKey = CreateObject("WScript.Shell")

    objRegistryKey.RegWrite RegKeyPath, RegKeyValue, RegKeyType

    If 1 <> 1 Then
ErrorHandler:
        On Error GoTo 0
        On Error GoTo ErrorHandler
    End If

End Sub

我希望发现一种方法,不仅可以添加新的RegKey,还可以添加新的RegKeys“文件夹”并编辑其默认值。

0 个答案:

没有答案