title: vbscript Open Excel 2010 in a separate process
body: <p>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.</p>
  
  <p>Environment: <br />
  Windows 7 Professional x64 <br />
  Excel 2010</p>
  
  <pre><code>Option Explicit
  ' =======================================================
  ' Purpose: This will let you open each Excel Spreadsheet in a seperate window.
  ' =======================================================
  
  Const HKCR = &amp;H80000000
  
  dim objShell, objReg, strComputer
  
  strComputer = "."
  
  set objShell = WScript.CreateObject("WScript.Shell")
  Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &amp; strComputer &amp; "\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&gt;VijqBof(Y8'w!FId1gLQ "%1"", "REG_MULTI_SZ"
  
  set objShell = Nothing
  </code></pre>
  
  <p>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 <code>objShell.RegDelete</code> so I was trying to use <code>objReg.DeleteKey</code> however neither were successful.  I haven't been able to find anything on a work-around that doesn't involve deleting every subkey individually.</p>
  
  <p>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 <code>"C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE" "%1"</code></p>
  
  <p>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 <code>xb'BV5!!!!!!!!!MKKSkEXCELFiles&gt;VijqBof(Y8'w!FId1gLQ "%1"</code></p>
  
  <p>I've tried using different things like <code>&amp; chr(34) &amp;</code> and also using <code>""""</code> but haven't come up with the right combo yet.</p>
  
  <p>Thanks!</p>
  
  <p>Edit: Final solution.</p>
  
  <pre><code>Option Explicit
  ' ============================================
  ' Purpose: This will let you open each Excel Spreadsheet in a seperate window.
  ' =======================================================
  
  Const HKEY_CLASSES_ROOT = &amp;H80000000
  
  Dim strComputer, objReg, strKeyPath
  
  ' Create WMI registry object
  strComputer = "."
  Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &amp; strComputer &amp; "\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&gt;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 &amp; "\" &amp; strSubkey
          Next
      End If
  
      objReg.DeleteKey HKEY, strKeyPath
  End Sub
  </code></pre>