Lucretius
Lucretius

Reputation: 1069

vbscript Open Excel 2010 in a separate process

I've been trying to figure out how to properly format these strings for RegWrite but haven't been able to figure it out. When finished the script modifies a few areas in the registry so that multiple Excel 2010 workbooks are opened in different windows.

Environment:
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

Issue 1) For some reason I can't delete the ddeexec registry key. It does have subkeys and through some reading found out that you can't delete keys which also have subkeys using objShell.RegDelete so I was trying to use objReg.DeleteKey however neither were successful. I haven't been able to find anything on a work-around that doesn't involve deleting every subkey individually.

Issue 2) The text I'm trying to write to HKCR\Excel.Sheet.12\shell\open\command contains a lot of quotes and spaces that I'm having trouble properly escaping. The value in the registry should be "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE" "%1"

Issue 3) The crazy string of characters I'm trying to put in the command key contains a "(" which is resulting in an error ") expected", I've played around with trying to get it formatted correctly like in issue 2. The value in the registry should be xb'BV5!!!!!!!!!MKKSkEXCELFiles>VijqBof(Y8'w!FId1gLQ "%1"

I've tried using different things like & chr(34) & and also using """" but haven't come up with the right combo yet.

Thanks!

Edit: Final solution.

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

Upvotes: 2

Views: 1851

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200393

Issue 1:

You must delete every subkey from the bottom up. Use recursion for that:

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

Issue 2:

Double quotes within a string must be escaped by doubling them:

objShell.RegWrite "HKCR\Excel.Sheet.12\shell\Open\command\" _
  , """C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE"" ""%1""" _
  , "REG_SZ"

Issue 3:

As documented RegWrite does not support the type REG_MULTI_SZ. You need to use SetMultiStringValue for this. You need to escape inner double quotes here too.

Upvotes: 3

Related Questions