我一直在试图弄清楚如何为 RegWrite 正确格式化这些字符串,但一直无法弄清楚。完成后,脚本会修改注册表中的一些区域,以便在不同的窗口中打开多个 Excel 2010 工作簿。
环境:
Windows 7 Professional x64
Excel 2010
Option Explicit
' =======================================================
' Purpose: This will let you open each Excel Spreadsheet in a seperate window.
' =======================================================
Const HKCR = &H80000000
dim objShell, objReg, strComputer
strComputer = "."
set objShell = WScript.CreateObject("WScript.Shell")
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
' objShell.RegDelete "HKCR\Excel.Sheet.12\shell\Open\ddeexec\"
objReg.DeleteKey HKCR, "Excel.Sheet.12\shell\Open\ddeexec"
objShell.RegWrite "HKCR\Excel.Sheet.12\shell\Open\command\", "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE" "%1", "REG_SZ"
objShell.RegWrite "HKCR\Excel.Sheet.12\shell\Open\command\command", "xb'BV5!!!!!!!!!MKKSkEXCELFiles>VijqBof(Y8'w!FId1gLQ "%1"", "REG_MULTI_SZ"
set objShell = Nothing
问题 1) 出于某种原因,我无法删除 ddeexec 注册表项。它确实有子键,并且通过一些阅读发现你不能删除也有子键的键,objShell.RegDelete
所以我试图使用objReg.DeleteKey
但都没有成功。在不涉及单独删除每个子项的解决方法中,我找不到任何东西。
问题 2) 我尝试写入 HKCR\Excel.Sheet.12\shell\open\command 的文本包含很多引号和空格,我无法正确转义。注册表中的值应该是"C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE" "%1"
问题 3)我试图在命令键中输入的疯狂字符串包含一个“(”,这会导致错误“)预期”,我已经尝试过尝试使其格式正确,就像在问题 2 中一样. 注册表中的值应该是xb'BV5!!!!!!!!!MKKSkEXCELFiles>VijqBof(Y8'w!FId1gLQ "%1"
我尝试过使用不同的东西,比如& chr(34) &
也使用""""
过,但还没有想出正确的组合。
谢谢!
编辑:最终解决方案。
Option Explicit
' ============================================
' Purpose: This will let you open each Excel Spreadsheet in a seperate window.
' =======================================================
Const HKEY_CLASSES_ROOT = &H80000000
Dim strComputer, objReg, strKeyPath
' Create WMI registry object
strComputer = "."
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
' Delete DDE stuff
strKeyPath = "Excel.Sheet.12\shell\Open\ddeexec"
DeleteSubKeys HKEY_CLASSES_ROOT, strKeyPath
' Modify launch commands
strKeyPath = "Excel.Sheet.12\shell\Open\command\"
objReg.SetStringValue HKEY_CLASSES_ROOT, strKeyPath, "", """C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE"" ""%1"""
objReg.SetMultiStringValue HKEY_CLASSES_ROOT, strKeyPath, "command", Array("xb'BV5!!!!!!!!!MKKSkEXCELFiles>VijqBof(Y8'w!FId1gLQ", """%1""")
' =====================================================================
' Sub: DeleteSubKeys
' HKEY: The specific root Hive you're editing.
' strKeyPath: Path to specific Key under HKEY.
' Purpose: Iterate through all subkeys under strKeyPath and delete them.
' http://technet.microsoft.com/en-us/magazine/2006.08.scriptingguy.aspx
' =====================================================================
Sub DeleteSubKeys(HKEY, strKeyPath)
Dim arrSubkeys, strSubkey
objReg.EnumKey HKEY, strKeyPath, arrSubkeys
If IsArray(arrSubkeys) Then
For Each strSubKey In arrSubkeys
DeleteSubKeys HKEY, strKeyPath & "\" & strSubkey
Next
End If
objReg.DeleteKey HKEY, strKeyPath
End Sub