title: VSTO C# Excel workbook all worksheets
body: <p>I am using C# VSTO to make a text replacement in all Excel files in a directory. Currently, my code works, but only to the active worksheet (first worksheet) of each Excel file. How do I perform this for all the existing worksheets in an Excel file:</p>
  
  <pre><code>public void runFiles(string _path)
          {
              string path = _path;
              object m = Type.Missing;
  
              var xlApp = new Microsoft.Office.Interop.Excel.Application();
  
              DirectoryInfo d = new DirectoryInfo(path);
  
              FileInfo[] listOfFiles_1 = d.GetFiles("*.xlsx*").ToArray();
              FileInfo[] listOfFiles_2 = d.GetFiles("*.xls*").ToArray();
  
              FileInfo[] listOfFiles = listOfFiles_1.Concat(listOfFiles_2).ToArray();
  
              xlApp.DisplayAlerts = false;
              foreach (FileInfo file in listOfFiles)
              {
  
                  var xlWorkBook = xlApp.Workbooks.Open(file.FullName);
                  Excel.Worksheet xlWorkSheet = xlWorkBook.Worksheets;
  
                  // get the used range. 
                  Excel.Range r = (Excel.Range)xlWorkSheet.UsedRange;
  
                  // call the replace method to replace instances. 
                  bool success = (bool)r.Replace(
                      "Engineer",
                      "Designer",
                      Excel.XlLookAt.xlWhole,
                      Excel.XlSearchOrder.xlByRows,
                      true, m, m, m);
  
                  xlWorkBook.Save();
                  xlWorkBook.Close();
              }
  
              xlApp.Quit();
  
              Marshal.ReleaseComObject(xlApp);
          }
  </code></pre>
  
  <p><code>xlWorkBook.ActiveSheet</code> only grabs the only first sheet. I tried <code>xlWorkBook.Worksheets</code>, but I am getting an error of <code>Cannot implicitly convert type 'Microsoft.Office.Interop.Excel.Sheets' to 'Microsoft.Office.Interop.Excel.Worksheet'. An explicit conversion exists (are you missing a cast?)</code></p>