Results 1 to 3 of 3

Thread: [RESOLVED] Searching Excel Document For String

  1. #1

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Resolved [RESOLVED] Searching Excel Document For String

    I am trying to use C# and VS2005 to find a string in the document then using that range I will start population on the next cell.

    I have the most of everything working, I can open the correct excel document and go the correct sheet, but when I try to execute the find method it always fails.

    VB Code:
    1. public void ExportMeasurements()
    2.         {
    3.             // A new application needs to be created.
    4.             Excel.Application excelApp = new Excel.ApplicationClass();
    5.              
    6.             // See if the file exists and has correct extension
    7.             if (!File.Exists(this.cExcelFile))
    8.             {
    9.                 excelApp.Quit();
    10.                 excelApp = null;
    11.                 throw new FileNotFoundException("{0} does not exist.", this.cExcelFile);
    12.             }
    13.  
    14.             if (excelApp == null)
    15.             {
    16.                 throw new Exception("Error! Excel Failed to Initailize");
    17.             }
    18.  
    19.             // Open an existing document into a Workbook object:
    20.  
    21.                 Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(this.cExcelFile,
    22.                     Excel.XlUpdateLinks.xlUpdateLinksUserSetting, false, 5, "", "", false, Excel.XlPlatform.xlWindows,
    23.                     "", true, false, 0, true, false, false);
    24.      
    25.  
    26.             // Count the sheets in collection.
    27.             if (cSheets.Count == 0 || cSheets == null)
    28.             {
    29.                 throw new NotSupportedException("No Sheet Specified.");
    30.             }
    31.  
    32.             // Get all of the sheets in the workbook opened.
    33.             Excel.Sheets excelSheets = excelWorkbook.Worksheets;
    34.  
    35.             // Now that you have the collection of Worksheets, you must get an individual sheet edit data within.
    36.             Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(cSheets[0].ToString());
    37.  
    38.             ICollection IMasterKeys = cMasterDrill.GetKeys();
    39.             ICollection ISlaveKeys = cSlaveDrill.GetKeys();
    40.            
    41.             // Make sure that the key counts match for both drils.
    42.             if (ISlaveKeys.Count != IMasterKeys.Count)
    43.             {
    44.                 excelApp.Quit();
    45.                 excelApp = null;
    46.                 throw new ArgumentOutOfRangeException("IMasterKeys (" + IMasterKeys.Count + ") != ("
    47.                     + ISlaveKeys.Count + ") IBaseKeys.");
    48.             }
    49.  
    50.             foreach (object itMaster in IMasterKeys)
    51.             {  
    52.                 // Load well stats into arraylist.
    53.                 ArrayList masterList = GetMeasurements(itMaster.ToString());
    54.                
    55.                 foreach (object itSlave in ISlaveKeys)
    56.                 {
    57.                     // Load well stats into arraylist.
    58.                     ArrayList slaveList = GetMeasurements(itSlave.ToString());
    59.                     [b]//ERRORS HERE
    60.                     //Finding the cell containing the specified string
    61.                     try
    62.                     {
    63.                         Excel.Range rng = excelWorksheet.Cells.Find("LA 10004", null, null,
    64.                             Excel.XlLookAt.xlWhole, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext,
    65.                             null, null, null);
    66.                     }
    67.                     catch (Exception e)
    68.                     {
    69.                        
    70.                     }[/b]
    71.                    
    72.                      
    73.                 }
    74.             }
    75.  
    76.             // Close out of excel.
    77.             excelWorkbook = null;
    78.             excelWorksheet = null;
    79.             excelSheets = null;
    80.  
    81.             excelApp.Quit();
    82.             excelApp = null;
    83.         }
    84.     }

    I always throw this exception "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"

    I know the string is in the file. Any ideas?

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  2. #2
    Hyperactive Member vbcode1980's Avatar
    Join Date
    Nov 2005
    Location
    Anywhere the wind blows
    Posts
    365

    Re: Searching Excel Document For String

    Hmm, I did some searching on the function but can't find anything wrong.
    Are you sure the arguments you made Null are optional?
    I code C#....

  3. #3

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Searching Excel Document For String

    I got it working I had to yse Type.Missing instead of null.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width