Problem with the Excel OPEN and FIND methods using C#

P

Peer754

Hi,

I'm currently working with Visual Studio 2005 producing a Windows
Form-application in C#. In one location in the program I need to access many
existing Excel files (search within each file for a text and replace that
with another text). I have done the same search and replace pattern using
Word and also plain text files without problem but Excel in particular seems
harder to get right.
Can someone show me, or point to examples using
Microsoft.Office.Interop.Excel (office 2003)? Or even better, take a look at
my code and correct it ;)

One of the problem found by the compiler is that in the OPEN method, it
fails to convert from object to string. I've also tried swapping the object
FILE with string filePath without success.

Here is my compiled log together with code part:

All the best,
peer

Warning 1 Ambiguity between method
'Microsoft.Office.Interop.Word._Document.Close(ref object, ref object, ref
object)' and non-method
'Microsoft.Office.Interop.Word.DocumentEvents2_Event.Close'. Using method
group. C:\Data\MainForm.cs 227 34 SearchReplace

Error 2 The best overloaded method match for
'Microsoft.Office.Interop.Excel.Workbooks._Open(string, object, object,
object, object, object, object, object, object, object, object, object,
object)' has some invalid arguments C:\Data\MainForm.cs 279 37 SearchReplace

Error 3 Argument '1': cannot convert from 'object' to
'string' C:\Data\\MainForm.cs 279 58 SearchReplace

Warning 4 Ambiguity between method
'Microsoft.Office.Interop.Excel._Workbook.Activate()' and non-method
'Microsoft.Office.Interop.Excel.WorkbookEvents_Event.Activate'. Using method
group. C:\Data\MainForm.cs 284 35 SearchReplace

Warning 5 Ambiguity between method
'Microsoft.Office.Interop.Excel._Worksheet.Activate()' and non-method
'Microsoft.Office.Interop.Excel.DocEvents_Event.Activate'. Using method
group. C:\Data\MainForm.cs 302 42 SearchReplace

Error 6 The best overloaded method match for
'Microsoft.Office.Interop.Excel._Worksheet.get_Range(object, object)' has
some invalid arguments C:\Data\MainForm.cs 304 42 SearchReplace

Error 7 Argument '1': cannot convert from 'void' to
'object' C:\Data\MainForm.cs 304 61 SearchReplace

Error 8 Argument '2': cannot convert from 'void' to
'object' C:\Data\MainForm.cs 304 99 SearchReplace

Error 9 The best overloaded method match for
'Microsoft.Office.Interop.Excel.Range.Find(object, object, object, object,
object, Microsoft.Office.Interop.Excel.XlSearchDirection, object, object,
object)' has some invalid arguments C:\Data\MainForm.cs 307 33 SearchReplace

Error 10 Argument '6': cannot convert from 'object' to
'Microsoft.Office.Interop.Excel.XlSearchDirection' C:\Data\MainForm.cs 308 45 SearchReplace

Error 11 'Microsoft.Office.Interop.Excel.Range.Find(object, object, object,
object, object, Microsoft.Office.Interop.Excel.XlSearchDirection, object,
object, object)' is a 'method', which is not valid in the given
context C:\Data\MainForm.cs 316 40 SearchReplace

Error 12 The name 'replaceAll' does not exist in the current
context C:\Data\MainForm.cs 318 84 SearchReplace



Excel.Application eApp;

Excel.Workbook eBook;

Excel.Worksheet eSheet;

Excel.Range eRange;



object file = filePath;

object nullobj = System.Reflection.Missing.Value;

object readOk = false;



try

{

eApp = new Excel.Application();

eApp.Visible = true;



/*

Open(

* Filename As String,

* [UpdateLinks],

* [ReadOnly],

* [Format],

* [Password],

* [WriteResPassword],

* [IgnoreReadOnlyRecommended],

* [Origin],

* [Delimiter],

* [Editable],

* [Notify],

* [Converter],

* [AddToMenuRecentlyUsed])

*

*/



eBook = eApp.Workbooks._Open(file, nullobj,
nullobj, nullobj,

nullobj,
nullobj, nullobj, nullobj,

nullobj,
nullobj, nullobj, nullobj,

nullobj);



eBook.Activate();

foreach (Excel.Worksheet tmpSheet in
eBook.Worksheets)

{

object searchT = searchText;

object replaceT = replaceText;

object useDefaultValue = Type.Missing;



/*

*

Cells.Find(What:="asd",

* After:=ActiveCell,

* LookIn:=xlFormulas,

* LookAt:=xlPart,

* SearchOrder:=xlByRows,

* SearchDirection:=xlNext,

* MatchCase:=True,

* SearchFormat:=False)

*/

tmpSheet.Activate();



eRange =
tmpSheet.get_Range(tmpSheet.Cells.set_Item(1,1,nullobj),
tmpSheet.Cells.set_Item(tmpSheet.Rows.Count, tmpSheet.Columns.Count,
nullobj));





eRange.Find(searchT, useDefaultValue,
useDefaultValue, useDefaultValue, useDefaultValue,

useDefaultValue,
useDefaultValue, useDefaultValue, useDefaultValue);



eRange.Replace(replaceT, useDefaultValue,
useDefaultValue, useDefaultValue, useDefaultValue,

useDefaultValue,
useDefaultValue, useDefaultValue);

//tmpRange.Find.Wrap =
Word.WdFindWrap.wdFindContinue;



//object replaceAll =
Word.WdReplace.wdReplaceAll;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top