0
votes

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:

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);
        }

xlWorkBook.ActiveSheet only grabs the only first sheet. I tried xlWorkBook.Worksheets, but I am getting an error of Cannot implicitly convert type 'Microsoft.Office.Interop.Excel.Sheets' to 'Microsoft.Office.Interop.Excel.Worksheet'. An explicit conversion exists (are you missing a cast?)

2

2 Answers

0
votes

Worksheets property is a collection of worksheet. So you just need iterate through it.

foreach (Excel.Worksheet xlWorkSheet in 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);
}
0
votes

The Worksheets method returns an array of worksheets. I have to edit each element in the array by doing this

                foreach (Excel.Worksheet xlWorkSheet in 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();