5

I am using Excel interop object and trying to find a date in a specified range by below method

Excel.Range rngFind = WS.get_Range(strFromRange, strToRange).Find(strFind, Type.Missing,
                Excel.XlFindLookIn.xlFormulas, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows,
                Excel.XlSearchDirection.xlNext, false, false, false);

but i get rngFind as null always my strFind = "Sep-08" i tried with both Excel.XlFindLookIn.xlFormulas and Excel.XlFindLookIn.xlValues

my excel file looks like this

Sep-08  Oct-08  Nov-08  Dec-08  Jan-09  Feb-09  Mar-09  Apr-09  May-09  Jun-09  Jul-09

where as wheni click on Sep-08 cell i get 9/1/2008 in the formula field in Excel i have also tried searching for 9/1/2008 but it varies system by system as per the RegionalSettings Date format...

Please help me. basically i am doing to get the cell address of the finding string

H H
  • 263,252
  • 30
  • 330
  • 514
Sathish
  • 329
  • 3
  • 6
  • 13
  • Are you using the Primary Interop Assemblies provided by Microsoft or simply pointing to the Excel dlls and letting Visual Studio generate the proxy for you? Also what version of Excel are you interoping with? – Joshua Drake Apr 14 '10 at 20:38
  • What values are you passing for strFromRange and strToRange? – Joshua Drake Apr 14 '10 at 20:43

1 Answers1

5

Using Office 2007, Interop generated directly from Visual Studio. I used the following code to find the cell in question:

using System.Reflection;
using Microsoft.Office.Interop.Excel;

object False = false;
object True = true;

_Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();

Workbook wb = excel.Workbooks._Open(@"C:\tmp\StackOverflow.xlsx",False, False,Missing.Value,Missing.Value,False,False,Missing.Value,Missing.Value,False,Missing.Value,Missing.Value,True);

_Worksheet ws = (_Worksheet)wb.Worksheets[1];

string from = "A1";
string to = "B1";

Range rng = ws.get_Range(from,to);

Range findRng = rng.Find("Sep-08",Missing.Value,XlFindLookIn.xlValues,Missing.Value,Missing.Value,XlSearchDirection.xlNext,False,False,Missing.Value);

You can find the Microsoft example at How to automate Excel by using Visual C# to fill or to obtain data in a range by using arrays.

Joshua Drake
  • 2,704
  • 3
  • 35
  • 54