我一直在尝试找出如何为 RegWrite 正确格式化这些字符串,但一直无法弄清楚。完成后,脚本会修改注册表中的一些区域,以便在不同的窗口中打开多个 Excel 2010 工作簿。
环境:
Windows 7 专业版 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
最佳答案
问题 1:
您必须从下往上删除每个子项。为此使用递归:
Sub DelKey(hive, key)
rc = reg.EnumKey(hive, key, subkeys)
If rc = 0 Then ' only proceed if there were no errors
If Not IsNull(subkeys) Then
For Each subkey In subkeys
DelKey hive, key & "\" & subkey ' <- recurse (descend before delete)
Next
End If
reg.DeleteKey hive, key ' at this point the key doesn't have
' subkeys anymore
End If
End Sub
问题 2:
字符串中的双引号必须通过加倍来转义:
objShell.RegWrite "HKCR\Excel.Sheet.12\shell\Open\command\" _
, """C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE"" ""%1""" _
, "REG_SZ"
问题 3:
如documented RegWrite
不支持类型 REG_MULTI_SZ
。您需要使用SetMultiStringValue
为了这。您也需要在这里转义内部双引号。
关于vbscript 在单独的进程中打开 Excel 2010,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14485583/