title: how to check if file is opened in excel using OLE (leaves excel process open)
body: <p>how may I check if a file is already opened in some Excel instance?</p>
  
  <p>I use DXL (DOORS) language but it should be independent from this language.</p>
  
  <p>Is there any OLE method that I can call to check which file is opened and compare that with the path/filename?</p>
  
  <p>and if that is possible, can I close only that worksheet/file in that Excel application?</p>
  
  <p>edit:
  this is what I got till now, this works but only once. DXL leaves an Excel.exe process open and in next checks that instance is used which has no open workbooks or even no window at all.</p>
  
  <pre><code>        if (confirm "File \"" fPath "\" already exists. Do you want to overwrite it?") {
  
          // check if file is opened in any Excel instance
          OleAutoObj oleWorkbooks     = null;
          OleAutoObj oleExcel         = null;
          OleAutoObj oleWorkbook      = null;
          OleAutoArgs autoArgs = create;
  
          oleExcel = oleGetAutoObject("Excel.Application");
          bool opened = false;
          // if excel is opened
          if(oleExcel != null){
              d("Excel is opened");
              // Get workbooks and open file
              oleGet(oleExcel,"Workbooks", oleWorkbooks);
  
              // compare each open workbook
              int count = 0;
              oleGet(oleWorkbooks,"Count", count);
              string workbookname = "";
              string sPath = replace(fPath, "\\", "/");
              sPath = stripPath(sPath, true);
  
              while (count &gt; 0) {
                  d("checking opened document");
                  clear(autoArgs);
                  put(autoArgs, count);
                  oleGet(oleWorkbooks,"Item", autoArgs, oleWorkbook);
                  oleGet(oleWorkbook, "Name", workbookname);
                  opened = sPath == workbookname;
                  if(opened) {
                      if(confirm "The file is currently opened in Excel. It must be closed. Do you want to close the document?") {
                          clear(autoArgs);
                          oleMethod(oleWorkbook,"Close",autoArgs);
                      }
                      break;  
                  }
                  count--;
              }
          }
          oleCloseAutoObject(oleExcel);
          oleCloseAutoObject(oleWorkbooks);
          oleCloseAutoObject(oleWorkbook);
          // todo leaves excel process open
  
          if(!opened) {
              streamOutputData = write fPath;
              streamOutputData &lt;&lt; sOutput;
              close streamOutputData;
              return true;    
          }
      }
  </code></pre>