我在C#中看到了这一行,我正在尝试使它适应VBA:
Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 4,Microsoft.Win32.RegistryValueKind.DWord);我在这里完全迷失了方向,有一些错误:
运行时:5-无效的过程调用)
当我使用默认的i_Type字符串"REG_SZ“而不是"Start”时,我会得到一个与regkey相关的错误:
运行时- -214702489180070005无效根目录
我的代码:
Dim i_RegKey As String, i_Value As String, i_Type As String
Dim myWS As Object
i_Type = "REG_SZ" ' Optional
'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'write registry key
i_RegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start"
i_Value = "4"
i_Type = "REG_DWORD"
myWS.RegWrite i_RegKey, i_Value, i_Type发布于 2015-09-03 07:47:55
我认为这里的问题是宏没有写入注册表的权限。
此页中有更多信息。我可以很好地使用WScript对象读取键的值:
Debug.Print CreateObject("WScript.Shell").RegRead("HKLM\SYSTEM\CurrentControlSet\Services\USBSTOR\Start")要编写(如果您有权限,它应该工作):
CreateObject("WScript.Shell").RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\USBSTOR\Start", 4, "REG_DWORD"我是如何让它工作的(因为我的脚本似乎没有必要的权限):
ShellExecute 0, "runas", "C:\Windows\System32\cmd.exe", "/k %windir%\System32\reg.exe ADD HKLM\SYSTEM\CurrentControlSet\Services\USBSTOR /f /v Start /t REG_DWORD /d 4", "C:\", 0在最后一个示例中,将提示用户提供必要的权限。
PS: HKLM是HKEY_LOCAL_MACHINE的缩写。所有其他根键名称都有类似的缩写,可以在上面提到的一页中查阅。
作为一个实际示例,我将使用这些表达式来启用/禁用USB大容量存储(当禁用时,关闭启用时):
Sub DoUSB_Control()
If CreateObject("WScript.Shell").RegRead("HKLM\SYSTEM\CurrentControlSet\Services\USBSTOR\Start") = 3 Then
ShellExecute 0, "runas", "C:\Windows\System32\cmd.exe", "/k %windir%\System32\reg.exe ADD HKLM\SYSTEM\CurrentControlSet\Services\USBSTOR /f /v Start /t REG_DWORD /d 4", "C:\", 0
Else
ShellExecute 0, "runas", "C:\Windows\System32\cmd.exe", "/k %windir%\System32\reg.exe ADD HKLM\SYSTEM\CurrentControlSet\Services\USBSTOR /f /v Start /t REG_DWORD /d 3", "C:\", 0
End If
End Sub发布于 2021-07-01 21:54:17
更新:
虽然下面的代码有利于学习,但是有一个内置的用于工作w/ Registry的VBA函数,但我认为它只对存储/保存与VBA项目相关的注册表中的设置很有用,而不是从“其他程序”/“Registry中的位置”中设置/检索设置。
见GetSetting、SaveSetting和DeleteSetting
我构建了一个函数来接受/利用这三个函数,如下所示,但是它并不需要。我打开了RegEdit,并在执行代码时使用F5刷新和查看。
Option Explicit
Public Sub Test_RegKeyFunc()
Dim appname As String, section As String, key As String, default, KeyVal, GetSettingBool As Boolean, SaveSettingBool As Boolean, DelSettingBool As Boolean
appname = "MyApp"
section = "MySettings"
key = "AutoDoThisBool"
KeyVal = "TRUE"
Call RegKeyFunc(appname, section, key, , KeyVal) ' Call Func without setting Save = True Returns ""
Debug.Print RegKeyFunc(appname, section, key, , KeyVal)
If RegKeyFunc(appname, section, key, , KeyVal) = "" Then
Stop
End If
Call RegKeyFunc(appname, section, key, , KeyVal, , True) ' Call Func and Save Setting
Debug.Print RegKeyFunc(appname, section, key, , KeyVal)
If RegKeyFunc(appname, section, key, , KeyVal) = "TRUE" Then
Stop
End If
Call RegKeyFunc(appname, section, key, , KeyVal, , , True) ' Call Func and Del Key/Setting
Debug.Print RegKeyFunc(appname, section, key, , KeyVal)
If RegKeyFunc(appname, section, key, , KeyVal) = "" Then
Stop
End If
Stop
Call RegKeyFunc(appname, section, key, , KeyVal, , , , True) ' Call Func and Del SubFolder/Section
Debug.Print RegKeyFunc(appname, section, key, , KeyVal)
If RegKeyFunc(appname, section, key, , KeyVal) = "" Then
Stop
End If
Stop
Call RegKeyFunc(appname, section, key, , KeyVal, , , , , True) ' Call Func and Del Folder
Debug.Print RegKeyFunc(appname, section, key, , KeyVal)
If RegKeyFunc(appname, section, key, , KeyVal) = "" Then
Stop
End If
Stop
End Sub
Public Function RegKeyFunc(appname As String, section As String, Optional key As String, Optional default, Optional KeyVal, Optional GetSettingBool As Boolean, Optional SaveSettingBool As Boolean, Optional DelSettingBool As Boolean, Optional DelSectionBool As Boolean, Optional DelAppBool As Boolean)
'HKCU\SOFTWARE\VB and VBA Program Settings
If SaveSettingBool = True Then
SaveSetting appname, section, key, KeyVal
End If
If DelSettingBool = True Then
DeleteSetting appname, section, key
End If
If DelSectionBool = True Then
DeleteSetting appname, section
End If
If DelAppBool = True Then
DeleteSetting appname
End If '
RegKeyFunc = GetSetting(appname, section, key, default)
End Function结束更新
这是我使用w/ Windows注册表的通用VBA代码。
Public Function ReadRegKeyVal(RegKeyStr As String) As Integer
ReadRegKeyVal = CreateObject("WScript.Shell").RegRead(RegKeyStr)
End FunctionPublic Function RegKeyExists(RegKeyStr As String) As Boolean
On Error GoTo ErrorHandler
CreateObject("WScript.Shell").RegRead (RegKeyStr)
RegKeyExists = True
Exit Function
ErrorHandler:
RegKeyExists = False
End FunctionPublic Sub SaveRegKey(RegKeyStr As String, RegKeyDesiredStateInt As Integer, Optional RegKeyType As String = "REG_DWORD")
CreateObject("WScript.Shell").RegWrite RegKeyStr, RegKeyDesiredStateInt, RegKeyType
Debug.Print "Generated --> " & RegKeyStr & "," & RegKeyDesiredStateInt & "," & RegKeyType
End Sub一个调用Sub的示例:
Public Const DWordRegKeyEnabled As Integer = 1
Public Const DWordRegKeyDisabled As Integer = 0
Public RegKeyStr As String, RegKeyLocStr As String, RegKeyNameStr As String
Public RegKeyDesiredStateInt As Integer, RegKeyCurrentStateInt As Integer
Public RegKeyFoundBool As BooleanPublic Sub SetMinMaxEnabledInExcelStatusBar()
RegKeyDesiredStateInt = DWordRegKeyEnabled
RegKeyLocStr = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\" & Application.Version & "\Excel\StatusBar\"
RegKeyNameStr = "MaxValue"
RegKeyStr = RegKeyLocStr & RegKeyNameStr
Debug.Print "RegKeyStr = " & RegKeyStr
Call SetRegKey(RegKeyStr, RegKeyDesiredStateInt)
End SubPublic Sub SetRegKey(RegKeyStr As String, RegKeyDesiredStateInt As Integer)
RegKeyFoundBool = RegKeyExists(RegKeyStr)
Debug.Print "RegKeyFoundBool = " & RegKeyFoundBool
If RegKeyFoundBool = False Then
Debug.Print "RegKeyFoundBool = False"
Call SaveRegKey(RegKeyStr, RegKeyDesiredStateInt)
Else
Debug.Print "RegKeyFoundBool = True"
RegKeyCurrentStateInt = ReadRegKeyVal(RegKeyStr)
Debug.Print "RegKeyCurrentStateInt = " & RegKeyCurrentStateInt
If RegKeyCurrentStateInt <> RegKeyDesiredStateInt Then
Debug.Print "RegKeyCurrentStateInt <> RegKeyDesiredStateInt"
Call SaveRegKey(RegKeyStr, RegKeyDesiredStateInt)
Else
Debug.Print "RegKeyCurrentStateInt = RegKeyDesiredStateInt"
End If
End If
End Sub发布于 2021-03-22 12:43:54
在注册表项中的单词开始后必须有一个"\“。
https://stackoverflow.com/questions/32345238
复制相似问题