vbscript在单独的进程中打开Excel 2010

时间:2013-01-23 17:21:11

标签: vbscript registry excel-2010 windows-7-x64

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

答案 0 :(得分:3)

问题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。你也需要在这里逃避内部双引号。