title: Looping through Excel workbooks, copying a range of cells from each and pasting to a master workbook
body: <p>HI: I am so glad you all are out there because what should be so easy appears not to be for me.</p>
  
  <p>Background: I am trying to use C# to open an Excel Workbook (using OpenFileDialog to select it - called "Gradebook"). Then I loop through multiple Excel workbooks (in a folder I select with FolderBrowserDialog - which saves the filenames in a string[]). I wish to open each workbook one by one, extract a range of data from each workbook and paste it in the "Gradebook". So far capturing the filenames in the array is working and so is the ability to select single workbook and open it.</p>
  
  <p>The Question:  I am having problems with the foreach statement. As I loop through the workbooks, the line: <code>Excel.Workbook stdWorkbook = excelApp.Workbooks.Open(stdFile)</code> has an error: "the name stdFile does not exist in the context". </p>
  
  <p>Also, I am rather new to programming and seem to be having problems finding anything that explains interop with Excel and C# in simple enough terms for me to understand. I would greatly appreciate any direction to a good source. </p>
  
  <pre><code>using System;
  using System.IO;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using Excel = Microsoft.Office.Interop.Excel;  //Use Project add reference to add the       Microsoft Excel Object library, then add this statement
  using System.Reflection;
  
  namespace ExcelLoadStdDataToGradingSheet
  {
      public partial class Form1 : Form
      {
          public string[] fileNames;
          public Form1()
          {
              InitializeComponent();
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              // select the folder with the student assignment 
              FolderBrowserDialog fbd = new FolderBrowserDialog();
              fbd.Description = "Browse to find Folder with student assignments to open";
              fbd.SelectedPath = "C:\\Users\\Robert\\Google Drive\\Programming";
              DialogResult result = fbd.ShowDialog();
              if (result == DialogResult.OK)
              {
                  string[] fileNames = Directory.GetFiles(fbd.SelectedPath);
                  System.Windows.Forms.MessageBox.Show("Files found: " +   fileNames.Length.ToString(), "Message");
              }
          }
  
          private void button2_Click(object sender, EventArgs e)
          {
              // Select the Excel file used for grading and open it
              OpenFileDialog dialog = new OpenFileDialog(); // Open dialog box to select   desired Excel file.  Next 3 line adjust that browser dialog
              dialog.Title = "Browse to find Grade Sheet to open";
              dialog.InitialDirectory = @"c:\\Users\\Robert\\Google Drive\\Programming";
              dialog.ShowDialog();
              string GradeExcel = dialog.FileName;
              Excel.Application excelApp = new Excel.Application();   //This creates the Excel application instance "excelApp"
              excelApp.Visible = true;
              Excel.Workbook GradeBook = excelApp.Workbooks.Add(GradeExcel);
  
              // Loop to open every student file, extract filename, name, Red ID, and creation date
              foreach (string stdFile in fileNames);
              {
                  // Open student Workbook and copy data from "Hidden" Worksheet
                  //Excel.Application newApp = new Excel.Application();  //This creates the Excel application instance "newApp"
                  Excel.Workbook stdWorkbook = excelApp.Workbooks.Open(stdFile);  //Open student Workbooks
                  Excel.Worksheet xlWorksheet = stdWorkbook.Sheets["Hidden"];
                  Excel.Range xlRange = xlWorksheet.Range["A2:E2"];
  
  
  
                  // Paste student information to the Grade Sheet
                  //  Workbooks(GradeBook).Sheets("GradingSheet").Range("SetActiveCell").Select.Paste;  //' start  pasting in "A5";
                  // SetActiveCell = ActiveCell.Offset(1, 0).Select;
                  // workbooks("StdWorkbook").close savechanges:=false;
              }
              // workbooks("Gradingbook").close savechanges:=true;
          }
      }
  }
  </code></pre>