title: AutoHotkey's ComObjActive handle to specific Worksheet
body: <p>The simplest way to create a handle to your currently active Excel sheets in your .ahk script is:</p>
  
  <pre><code>Xl := ComObjActive("Excel.Application")
  </code></pre>
  
  <p>Nonetheless, if one switches to another workbook, this becomes the new "<em>currently active sheet</em>" and AutoHotkey tries to use methods for sheets and cells on the new workbook through COM: of course scripts designed to work with specific sheets and cells don't work anymore on a different workbook.</p>
  
  <p>Do you know how to create COM handles to specific workbooks instead of currently active sheet?</p>
  
  <p>The goal should be to allow the user to loop between workbooks without <code>Xl</code> object losing its previous handle and going to a new one.</p>
  
  <p><strong>Example</strong></p>
  
  <p>Open an Excel workbook from scratch and type <code>1234</code> in cell A1 of sheet named "<em>Sheet1</em>"; then create a new .ahk script with the following content:</p>
  
  <pre><code>#Persistent
  
  Xl := ComObjActive("Excel.Application")
  SetTimer, xlRead, 5000
  Return
  
  xlRead:
  {
    value := Xl.Sheets("Sheets1").Range("A1").Value
    MsgBox, %value%
  }
  Return
  </code></pre>
  
  <p>Script above should display "1234" in a message box every 5 seconds.</p>
  
  <p>While that is running, open a new workbook and type <code>5678</code> in cell A1 of sheet named "<em>Sheet1</em>" and wait for 5 seconds: according to my trials, AutoHotkey should just switch the handle to the new active sheet and show a message box whose content is "5678".</p>
  
  <p>Any way to keep it linked to the first sheet? Of course assume one can save Excel files to hard disk with proper names which COM can refer to.</p>