I think the problem here was that the macro did not have permission to write to the registry.
More information in this page.
I could read the key's value using the WScript object just fine:
Debug.Print CreateObject("WScript.Shell").RegRead("HKLMSYSTEMCurrentControlSetServicesUSBSTORStart")
To write (it should work if you have permissions):
CreateObject("WScript.Shell").RegWrite "HKLMSYSTEMCurrentControlSetServicesUSBSTORStart", 4, "REG_DWORD"
How I got it to work (since my script does not seem to have the necessary permissions):
ShellExecute 0, "runas", "C:WindowsSystem32cmd.exe", "/k %windir%System32
eg.exe ADD HKLMSYSTEMCurrentControlSetServicesUSBSTOR /f /v Start /t REG_DWORD /d 4", "C:", 0
In this last example the user will be prompted to provide the necessary permission.
PS: HKLM is an abreviation for HKEY_LOCAL_MACHINE. All other root key names have similar abreviations that can be consulted in the page mentioned at the top.
As a practical example I will post my usage of these expressions to enable/disable USB mass storage (when on disable, when off enable):
Sub DoUSB_Control()
If CreateObject("WScript.Shell").RegRead("HKLMSYSTEMCurrentControlSetServicesUSBSTORStart") = 3 Then
ShellExecute 0, "runas", "C:WindowsSystem32cmd.exe", "/k %windir%System32
eg.exe ADD HKLMSYSTEMCurrentControlSetServicesUSBSTOR /f /v Start /t REG_DWORD /d 4", "C:", 0
Else
ShellExecute 0, "runas", "C:WindowsSystem32cmd.exe", "/k %windir%System32
eg.exe ADD HKLMSYSTEMCurrentControlSetServicesUSBSTOR /f /v Start /t REG_DWORD /d 3", "C:", 0
End If
End Sub
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…