I write a c# program for copy sheet. I got the exception error(0x800A03EC) when I call WorkSheet.Copy method until 105 times.
This is my snippet code: using Microsoft.Office.Interop.Excel;
private void CreateSheet(string dst_fileName)
{
object cell1 = "A2";
ApplicationClass app = null;
Workbook book = null;
Worksheet sheet = null;
Worksheet sheet_to_copy = null;
int i=0;
try
{
app = new ApplicationClass();
app.Visible = false;
app.ScreenUpdating = false;
app.DisplayAlerts = false;
book = app.Workbooks.Open(dst_fileName, 0, false, 5, "", "", true,
XlPlatform.xlWindows,
"\t", false, false, 0, true, 1, 0);
// Reference to the worksheet
sheet_to_copy = (Worksheet)book.Worksheets[1];
for(;i<listViewPrg.Items.Count;i++)
{
sheet = (Worksheet)book.Worksheets[book.Worksheets.Count];
// Copy the worksheet to the end of the worksheets
sheet_to_copy.Copy(Missing.Value, sheet);
sheet.Name = "NewSheet(" + book.Worksheets.Count + ")";
}
book.SaveAs(dst_fileName, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
QuitExcel(app);
}
}
I can't found any solution about this. Could someone give me a hint to solve this problem?
Thank you so much.
Possibly you have some corrupted Excel registry entries or so?
Why don't you try to use an open source reader of excel files and do everything using them? The speed would skyrocket and i guarantee you would not get these errors!
Related
I am using the below mentioned code for finding the row count of an Excel table but it seems there is something wrong since I am not getting the desired output. Please provide some solution to it.
The code is as follows:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp = null;
Excel.Workbook wb = null;
Excel.Worksheet worksheet = null;
int lastUsedRow = 0;
string srcFile = #"Path to your XLSX file";
xlApp = new Excel.ApplicationClass();
xlApp.Visible = false;
wb = xlApp.Workbooks.Open(srcFile,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
worksheet = (Excel.Worksheet)wb.Worksheets[1];
Excel.Range range
// Find the last real row
lastUsedRow = worksheet.Cells.Find("*",System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
Excel.XlSearchOrder.xlByRows,Excel.XlSearchDirection.xlPrevious,
false,System.Reflection.Missing.Value,System.Reflection.Missing.Value).Row;
xlApp.Workbooks.Close();
xlApp.Quit();
Marshal.ReleaseComObject(worksheet);
Marshal.ReleaseComObject(wb);
Marshal.ReleaseComObject(xlApp);
You can try:
wb = xlApp.Workbooks.Open(srcFile,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
worksheet = (Excel.Worksheet)wb.Worksheets[1];
object[,] values = worksheet .UsedRange.Value2;
int rowCount = values.GetLength(0);
int colCount = values.GetLength(1);
I'm working with Excel sheet of .xls format and writing data into it. The problem is that whenever the data doesn't start with alphabets it is prompting me different errors.
For example: When my column data started with =?us-ascii?Q?Google Keywords?= it threw me an exception:
Exception from HRESULT: 0x800A03EC
and when my data is like -------- Original Message --------Subject: the error was:
Not enough storage is available to complete this operation. (Exception from HRESULT: 0x8007000E (E_OUTOFMEMORY))
This is how I'm writing data:
foreach (AllCasesReplies infoList in allCasesReplies)
{
n = 0;
mWorkSheet.Cells[l + m, ++n] = infoList.id;
mWorkSheet.Cells[l + m, ++n] = infoList.replies;
m++;
}
This is how I clean my objects:
private static void SaveAndCollecttheGarbage(Microsoft.Office.Interop.Excel.Application excelApp, string path, Microsoft.Office.Interop.Excel.Sheets sheet)
{
excelApp.Columns.AutoFit();
mWorkBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
mWorkBook.Close(true, Missing.Value, Missing.Value);
sheet = null;
mWorkBook = null;
excelApplication.Quit();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
So I have tried omitting these data and they are working great.
Are there any rules that the column has to start with specific characters and if so, what are these?
if you type "?" in excel cell that has general format, the excel automaticall interprete that as a formula. You need to change the cell format before inserting the text into the cell.
something like:
foreach (AllCasesReplies infoList in allCasesReplies)
{
n = 0;
Microsoft.Office.Interop.Excel.Range range1 = mWorkSheet.Cells[l + m, ++n] as Range;
range1.NumberFormat = "#";
range1.Value2 = infoList.id;
Microsoft.Office.Interop.Excel.Range range2 = mWorkSheet.Cells[l + m, ++n] as Range;
range2.NumberFormat = "#";
range2.Value2 = infoList.replies;
m++;
}
I have a C# program that is creating, writing, and saving an excel file using the Excel Interop. The problem is that if I don't have the application quit immediately after saving and closing the excel file then the c# application gets an unhandled exception and crashes. Has anyone found a way to do this while being able to keep the host c# application open and running.
Here is the code that handles everything involving the Excel Interop
class CreateExcelDoc
{
string newFormString = trialReportForm.newFormString;
string fileString=trialReportForm.fileString;
int sheetCount;
string trialString = trialReportForm.trialString;
string dateString = trialReportForm.dateString;
string saveString = trialReportForm.saveSting;
System.Windows.Forms.Timer excelTimer = new System.Windows.Forms.Timer();
private Excel.Application app = null;
private Excel.Workbook workbook = null;
private Excel.Worksheet worksheet = null;
private Excel.Range workSheet_range = null;
public CreateExcelDoc()
{
createDoc();
}
public void createDoc()
{
try
{
app = new Excel.Application();
//app.Visible = false;
if (startForm.exportOwnerString == "Yes")
{
app.Visible = true;
startForm.exportOwnerString = " ";
}
else
{
app.Visible = false;
}
if (startForm.excelActionFlag=="addNewTrialReport")
{
workbook = (Excel.Workbook) app.Workbooks.Add(1);
//workbook.SaveAs(newFileForm.desktopPath + "\\" + "OB "+trialReportForm.otrClubNameString+" - "+trialReportForm.otrDateString);
worksheet = (Excel.Worksheet)workbook.Worksheets[1];
//fileNameString = newFileForm.desktopPath + "\\OB " + trialReportForm.otrClubNameString + " " + trialReportForm.otrDateString;
workbook.Worksheets[1].Name = trialReportForm.trialReportDate+" Trial "+trialReportForm.trialReportTrialNumber;
}
else if (startForm.excelActionFlag == "ownerContacts")
{
workbook = (Excel.Workbook)app.Workbooks.Add(1);
worksheet = (Excel.Worksheet)workbook.Worksheets[1];
workbook.Worksheets[1].Name = "Owner Contacts";
}
else if (startForm.excelActionFlag == "newExcelResults")
{
string testFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\test";
workbook = (Excel.Workbook)app.Workbooks.Add(1);
worksheet = (Excel.Worksheet)workbook.Worksheets[1];
workbook.Worksheets[1].Name = "Event 1 Results";
//workbook.SaveAs(testFile, Missing.Value, Missing.Value, Missing.Value, false);
//workbook.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"\\Results", Missing.Value, Missing.Value, Missing.Value, false);
//workbook.SaveAs(startForm.excelFileLocation, Missing.Value, Missing.Value, Missing.Value, false);
//fileNameString = registrationForm.regFileLocation + "\\OB " + registrationForm.regClubName + " " + registrationForm.regDateString;
}
else if (dogForm.dogRegistrationExcel == "Yes")
{
workbook = (Excel.Workbook)app.Workbooks.Add(1);
workbook.SaveAs(dogForm.filePath, Missing.Value, Missing.Value, Missing.Value, false);
}
else if (newFormString == "No")
{
//workbook = app.Workbooks.Open(fileString, Missing.Value, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, true, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
// workbook.Close(true,fileString,Missing.Value);
workbook = (Excel.Workbook)app.Workbooks.Open(fileString, Missing.Value, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, true, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
if (newFormString=="Yes"®istrationForm.formString=="OTR")
{
//string sheetName = "Trial Report - " + dateString + " " + trialString;
worksheet = (Excel.Worksheet)workbook.Worksheets[1];
workbook.Worksheets[1].Name =trialReportForm.otrDateString+" Trial " + trialReportForm.otrTrialString;
//workbook.Worksheets[1].Name = "Trial Report - " + dateString + " " + trialString;
//workbook.Worksheets[1].Name = "Hello";
//Excel.Name name1 = worksheet.Names.Add("Trial Report - " + dateString + " " + trialString, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//worksheet.Name = "Trial Report - " + dateString + " " + trialString;
}
else if (newFormString == "Yes" & registrationForm.formString == "Registration")
{
//string sheetName = "Trial Report - " + dateString + " " + trialString;
worksheet = (Excel.Worksheet)workbook.Worksheets[1];
workbook.Worksheets[1].Name = "Results: "+registrationForm.selectedEvent;
//workbook.Worksheets[1].Name = "Trial Report - " + dateString + " " + trialString;
//workbook.Worksheets[1].Name = "Hello";
//Excel.Name name1 = worksheet.Names.Add("Trial Report - " + dateString + " " + trialString, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//worksheet.Name = "Trial Report - " + dateString + " " + trialString;
}
else if (startForm.excelActionFlag == "addExistingTrialReport")
{
workbook = (Excel.Workbook)app.Workbooks.Open(startForm.excelFileLocation, Missing.Value, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, true, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
sheetCount = workbook.Worksheets.Count;
int sheetCountPlusONe=sheetCount+1;
worksheet = (Excel.Worksheet)workbook.Worksheets.Add(Missing.Value,workbook.Worksheets[sheetCount],Missing.Value,Missing.Value);
workbook.Worksheets[sheetCountPlusONe].Name = trialReportForm.trialReportDate + " Trial " + trialReportForm.trialReportTrialNumber;
//worksheet.Move(Missing.Value, workbook.Worksheets[sheetCount]);
}
else if (startForm.excelActionFlag == "existingExcelResults")
{
workbook = (Excel.Workbook)app.Workbooks.Open(startForm.excelFileLocation, Missing.Value, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, true, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
sheetCount = workbook.Worksheets.Count;
int sheetCountPlusONe = sheetCount + 1;
worksheet = (Excel.Worksheet)workbook.Worksheets.Add(Missing.Value, workbook.Worksheets[sheetCount], Missing.Value, Missing.Value);
workbook.Worksheets[sheetCountPlusONe].Name = "Event " + sheetCountPlusONe.ToString() + " Results";
//worksheet.Move(Missing.Value, workbook.Worksheets[sheetCount]);
}
else if (dogForm.dogRegistrationExcel == "Yes")
{
worksheet = (Excel.Worksheet)workbook.Worksheets[1];
workbook.Worksheets[1].Name = dogForm.dogUKCNumber;
}
}
catch (Exception e)
{
Console.Write("Error");
}
/* if (trialReportForm.saveMe=="Yes")
{
workbook.Save();
workbook.Close();
}
*/
}
public void createHeaders(int row, int col, string htext, string cell1,
string cell2, int mergeColumns, string b, bool font, int size, string
fcolor)
{
worksheet.Cells[row, col] = htext;
workSheet_range = worksheet.get_Range(cell1, cell2);
workSheet_range.Merge(mergeColumns);
switch (b)
{
case "BLUE":
workSheet_range.Interior.Color = System.Drawing.Color.Red.ToArgb();
break;
case "GAINSBORO":
workSheet_range.Interior.Color =
System.Drawing.Color.Gainsboro.ToArgb();
break;
//case "Turquoise":
// workSheet_range.Interior.Color =
//System.Drawing.Color.Turquoise.ToArgb();
//break;
case "PeachPuff":
workSheet_range.Interior.Color =
System.Drawing.Color.PeachPuff.ToArgb();
break;
default:
// workSheet_range.Interior.Color = System.Drawing.Color..ToArgb();
break;
}
//workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
//workSheet_range.Borders = null;
workSheet_range.Font.Bold = font;
workSheet_range.ColumnWidth = size;
//workSheet_range.HorizontalAlignment = ContentAlignment.BottomCenter;
if (startForm.excelActionFlag == "existingExcelResults" | startForm.excelActionFlag=="newExcelResults")
{
workSheet_range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
}
//workSheet_range.Cells.HorizontalAlignment = ContentAlignment.MiddleCenter;
workSheet_range.Font.Color = System.Drawing.Color.FloralWhite.ToArgb();
}
public void addData(int row, int col, string data,
string cell1, string cell2, string format)
{
worksheet.Cells[row, col] = data;
workSheet_range = worksheet.get_Range(cell1, cell2);
//workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
workSheet_range.NumberFormat = format;
workSheet_range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
excelTimer.Tick += new EventHandler(excelTimer_Tick);
excelTimer.Interval = 6000;
excelTimer.Start();
}
void excelTimer_Tick(object sender, EventArgs e)
{
if (startForm.excelActionFlag != "ownerContacts")
{
if (startForm.excelActionFlag == "existingExcelResults" | startForm.excelActionFlag == "newExcelResults")
{
/* Excel.Range sortRange;
sortRange = worksheet.get_Range("A14", "K32");
Excel.Range scoreColumn;
scoreColumn = worksheet.get_Range("C14", "C32");
sortRange.Sort(scoreColumn, Excel.XlSortOrder.xlDescending);*/
Excel.Range valueRange;
Excel.Range placeRange;
placeRange = worksheet.get_Range("A14", "A" + (14 + (registrationForm.numberofCompetitors - 1)).ToString());
valueRange = worksheet.get_Range("A14", "K"+(14+(registrationForm.numberofCompetitors-1)).ToString());
valueRange.Sort(valueRange.Columns[3, Type.Missing], Excel.XlSortOrder.xlDescending, Type.Missing, Type.Missing, Excel.XlSortOrder.xlAscending, Type.Missing, Excel.XlSortOrder.xlAscending, Excel.XlYesNoGuess.xlGuess, Type.Missing, Type.Missing, Excel.XlSortOrientation.xlSortColumns, Excel.XlSortMethod.xlPinYin, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal);
placeRange.Sort(placeRange.Columns[1, Type.Missing], Excel.XlSortOrder.xlAscending, Type.Missing, Type.Missing, Excel.XlSortOrder.xlAscending, Type.Missing, Excel.XlSortOrder.xlAscending, Excel.XlYesNoGuess.xlGuess, Type.Missing, Type.Missing, Excel.XlSortOrientation.xlSortColumns, Excel.XlSortMethod.xlPinYin, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal);
}
// workbook.Close(true, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Results.xls", Missing.Value);
workbook.Close(true, startForm.excelFileLocation, Missing.Value);
app.Quit();
Application.Exit();
/* workSheet_range = null;
worksheet = null;
workbook = null;
app = null;*/
//Thread.Sleep(5000);
// File.Move(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Results.xls", startForm.excelFileLocation);
}
}
}
I find that when saving with excel, I have more luck using the SaveAs method. For example:
private static Microsoft.Office.Interop.Excel.Application xlApp = null;
private static Microsoft.Office.Interop.Excel.Workbook xlWb = null;
private static Microsoft.Office.Interop.Excel.Worksheet xlWs = null;
//Your code and operations
xlWb.SaveAs(filePath, XlFileFormat.xlExcel8, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xlApp.Quit();
Marshal.ReleaseComObject(xlWs);
Marshal.ReleaseComObject(xlWb);
Marshal.ReleaseComObject(xlApp);
Several solutions including mine are presented How do I properly clean up Excel interop objects?
You can choose among the most sever (Killing Excel process) to more tender solutions (releasing COM object)
void excelsave()
{
try
{
ApplicationClass app = new ApplicationClass(); // the Excel application.
Workbook book = null;
Worksheet sheet = null;
Range range = null;
// the range object is used to hold the data
app.Visible = false;
app.ScreenUpdating = false;
app.DisplayAlerts = false;
string execPath =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
book = app.Workbooks.Open(#"E:\SSIS\ABC\Book1.xls",
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value);
sheet = (Worksheet)book.Worksheets[1];
range = sheet.get_Range("A1", Missing.Value);
range.Columns.ColumnWidth = 22.34;
range = sheet.get_Range("B1", Missing.Value);
range.Columns.ColumnWidth = 22.34;
book.SaveAs(#"E:\SSIS\ABC\Book1.xls", Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch (Exception ex)
{
}
}
Here I am opening an excel sheet trying to increase the column width and need to make the column headers as bold and save the document, right now the document is not getting saved. I am using vs 2008, c# 3.5
Is There anything that I am doing wrong here? any help on this would be great
looking an for solution
I ran the following using VS 2010 and .NET 4, but this code should still work in your environment. Also, I simplified your code a bit. Hopefully this will get you going in the right direction.
static void excelsave()
{
try
{
Application app = new Application();
string execPath =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
Workbook book = app.Workbooks.Open(#"c:\test.xls");
Worksheet sheet = (Worksheet)book.Worksheets[1];
Range range = sheet.get_Range("A1");
range.Columns.ColumnWidth = 22.34;
range = sheet.get_Range("B1");
range.Columns.ColumnWidth = 22.34;
sheet.get_Range("A1", "B1").Font.Bold = true;
book.SaveAs(#"c:\test2.xls"); // or book.Save();
book.Close();
}
catch (Exception ex)
{
}
}
UPDATE
You can find a similar explanation/example of what you are doing at:
http://www.dotnetperls.com/excel
Marshal.ReleaseComObject(book); // do this after the close
Also, there is a good discussion on cleaning up Excel/COM ...
How To Properly Clean Up Excel Interop Objects In c#
I'm trying to get C# to examine whatever workbook the user has selected and find any sheets which would
contain stock data. Concretely this would mean looking at a range of cells (say r<6, c<10) for "Close", "close" or
"CLOSE".
The following code shows the point at which the user has selected an .xls file.
I'm not sure how to loop through the sheets in the workbook to look for the desired text.
I'm assuming it involves creating a collection of sheets and assigning it to those in the
current workbook, but my attempts so far haven't worked.
private void button1_Click(object sender, System.EventArgs e)
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Excel Files (*.xls)|*.XLS";
if (dlg.ShowDialog() == DialogResult.OK)
{
// MessageBox.Show(dlg.FileName, "My Application", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
Excel.Application xlApp = new Excel.ApplicationClass();
xlApp.Visible = true;
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(dlg.FileName,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
}
}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);
MessageBox.Show(errorMessage, "Error");
}
}
Thanks for any ideas.
Jeff
Always take extra care to clean up when using the Interop libraries. Otherwise, you're likely to end up with a couple dozen EXCEL.EXE processes running in the background while you debug (or when a user hits an error).
private static bool IsStockDataWorkbook(string fileName)
{
Excel.Application application = null;
Excel.Workbook workbook = null;
try
{
application = new Excel.ApplicationClass();
application.Visible = true;
workbook = application.Workbooks.Open(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
foreach (Excel.Worksheet sheet in workbook.Worksheets)
{
if (IsStockWorksheet(sheet))
{
return true;
}
}
return false;
}
finally
{
if (workbook != null)
{
workbook.Close(false, Missing.Value, Missing.Value);
}
if (application != null)
{
application.Quit();
}
}
}
private static bool IsStockWorksheet(Excel.Worksheet workSheet)
{
Excel.Range testRange = workSheet.get_Range("C10", Missing.Value);
string value = testRange.get_Value(Missing.Value).ToString();
return value.Equals("close", StringComparison.InvariantCultureIgnoreCase);
}
You'll need to assign objSheets to something, most likely:
Excel.Sheets objSheets = xlWorkbook.Sheets;
Your foreach statement should look more like this (with no prior declaration of the ws variable):
foreach(Excel.Worksheet ws in objSheets)
{
rng = ws.get_Range(ws.Cells[1,1], ws.Cells[5,9]);
}
Obviously, you'll want to do something more substantial in that loop.
This is an easy one :) use the sheets collection in the workbook object.
Workbooks workbooks = xlApp.Workbooks;
foreach(Workbook wb in workbooks)
{
Worksheets worksheets = wb.Worksheets;
foreach(Worksheet ws in worksheets)
{
Range range = ws.get_Range(ws.Cells[1,1], ws.Cells[5,9]);
Range match = range.Find("close", ws.Cells[1,1],
xlFindLookIn.xlValues, xlLookAt.xlPart,
xlSearchOrder.xlByColumns, xlSearchDirection.xlNext,
false, false, false); //that first false means ignore case
// do something with your match here
// this will only return the first match; to return all
// you'll need to run the match in a while loop
}
}