2

我一直在试图弄清楚如何为 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
4

1 回答 1

3

问题一:

必须自下而上删除每个子项。为此使用递归:

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:

文档 RegWrite所述,不支持该类型REG_MULTI_SZ。您需要为此使用SetMultiStringValue。您也需要在这里转义内部双引号。

于 2013-01-23T18:58:27.673 回答