none
Excel Range.FindNext() : cannot convert parameter 1 from 'class Range' to 'const struct tagVARIANT &'

    Question

  • Hi All,

    I am trying to search a string in MSExcel sheet using following source code in VC++,MFC.

    First occurrence of string is found using Range.Find() but for next occurrence Range.FindNext() is giving error.

    error C2664: 'FindNext' : cannot convert parameter 1 from 'class MSExcel::Range' to 'const struct tagVARIANT &' 

    Rereferring this for 'FindNext'

    http://msdn.microsoft.com/en-us/library/office/ff196143.aspx

    //////Source code////////

    // Convenient values declared as ColeVariants.
    COleVariant vTrue((short)TRUE),vFalse((short)FALSE);
    COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

    MSExcel::_Application objApp;
    MSExcel::_Workbook objBook;
    MSExcel::Workbooks objBooks;
    MSExcel::Worksheets objSheets;
    MSExcel::_Worksheet objSheet;
    MSExcel::Range objRange = NULL;
    MSExcel::Range  objFoundRng = NULL;

    int nI = 0;
    INT nFound = 0;

    // Instantiate Excel and open an existing workbook.
    objApp.CreateDispatch("Excel.Application");
    objBooks = objApp.GetWorkbooks();


    objBook = objBooks.Open(m_straryFileList.GetAt(nI),
    VOptional, VOptional, VOptional, VOptional,
    VOptional, VOptional, VOptional, VOptional,
    VOptional, VOptional, VOptional, VOptional,
    VOptional, VOptional);

    objSheets = objBook.GetWorksheets();
    objSheet = objSheets.GetItem(COleVariant((short)1));

    //Get the range object for which you wish to retrieve the
    objRange = objSheet.GetUsedRange();

    //find for first match
    objFoundRng = objRange.Find(COleVariant(strSearchStr), VOptional,
    VOptional, xlWhole, VOptional,
    xlNext, xlIgnoreCase, VOptional, VOptional);

    if(objFoundRng != NULL){
    nFound++;
    //store address where first found
    CString strAddrs  = _T("");
    CString strFirstFound = objFoundRng.GetAddress(vTrue, vTrue,
    (long)1, VOptional, VOptional);

    //  FindNext until the first found cell is found again
    while (strFirstFound != strAddrs){            
    objFoundRng = objFoundRng.FindNext(objFoundRng);
    if(objFoundRng != NULL){
    nFound++;
    strAddrs = objFoundRng.GetAddress(vTrue, vTrue, (long)1, 
    VOptional, VOptional);
    }
    }   
    }



    //Close the workbook without saving changes
    //and quit Microsoft Excel.
    objBook.Close(COleVariant((short)FALSE), VOptional, VOptional);

    objApp.Quit();

    Am doing anything wrong?

    Any other way to do the same?

    Saturday, August 03, 2013 10:17 AM

Answers

  • removed FindNext by Find and now working. :)

    //find for word in a range
    	objFoundRng = objRange.Find(COleVariant(strSearchStr), VOptional,	
    		xlValues, xlPart, xlByRows,
    		xlNext, vFalse, VOptional, VOptional);
    	
    	//find for next occurence
    	if(objFoundRng != NULL){			
    		CString strFirstFound = objFoundRng.GetAddress(vTrue, vTrue,
    			(long)1, VOptional, VOptional);					
    		
    		CString strNextFound = _T("");				
    		while(strFirstFound != strNextFound){					
    			objFoundRng = objFoundRng.Find(COleVariant(strSearchStr), VOptional,	
    				xlValues, xlPart, xlByRows,
    				xlNext, vFalse, VOptional, VOptional);
    			
    			strNextFound = objFoundRng.GetAddress(vTrue, vTrue,
    				(long)1, VOptional, VOptional);		
    			
    		}
    	}

    Friday, August 09, 2013 3:10 AM

All replies

  • objFoundRng = objFoundRng.FindNext(objFoundRng);

    change it to

      objFoundRng = objRange.FindNext(objFoundRng);

    because you like to find the next cell in objRange after the cell objFoundRng.

    Andreas.
    Monday, August 05, 2013 10:24 AM
  • Thanks for Reply,

    I tried the same but no help.

    Actually FindNext() is not accepting objFoundRng  as input.

    error C2664: 'FindNext' : cannot convert parameter 1 from 'class MSExcel::Range' to 'const struct tagVARIANT &'

    Any Idea?

    many thanks...

    Tuesday, August 06, 2013 7:59 AM
  • removed FindNext by Find and now working. :)

    //find for word in a range
    	objFoundRng = objRange.Find(COleVariant(strSearchStr), VOptional,	
    		xlValues, xlPart, xlByRows,
    		xlNext, vFalse, VOptional, VOptional);
    	
    	//find for next occurence
    	if(objFoundRng != NULL){			
    		CString strFirstFound = objFoundRng.GetAddress(vTrue, vTrue,
    			(long)1, VOptional, VOptional);					
    		
    		CString strNextFound = _T("");				
    		while(strFirstFound != strNextFound){					
    			objFoundRng = objFoundRng.Find(COleVariant(strSearchStr), VOptional,	
    				xlValues, xlPart, xlByRows,
    				xlNext, vFalse, VOptional, VOptional);
    			
    			strNextFound = objFoundRng.GetAddress(vTrue, vTrue,
    				(long)1, VOptional, VOptional);		
    			
    		}
    	}

    Friday, August 09, 2013 3:10 AM