Click here to Skip to main content
15,891,136 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Read in .pdf files Pin
mesajflaviu3-Dec-10 20:10
mesajflaviu3-Dec-10 20:10 
QuestionGif in listctrl? Pin
Tcpip20052-Dec-10 16:45
Tcpip20052-Dec-10 16:45 
AnswerRe: Gif in listctrl? Pin
David Crow2-Dec-10 17:10
David Crow2-Dec-10 17:10 
AnswerRe: Gif in listctrl? Pin
Niklas L2-Dec-10 20:48
Niklas L2-Dec-10 20:48 
GeneralRe: Gif in listctrl? Pin
Tcpip20053-Dec-10 20:09
Tcpip20053-Dec-10 20:09 
QuestionVS 2010 - SubClass - FromHandlePermanent Assert Pin
Tony Teveris2-Dec-10 15:55
Tony Teveris2-Dec-10 15:55 
AnswerRe: VS 2010 - SubClass - FromHandlePermanent Assert Pin
Randor 2-Dec-10 19:20
professional Randor 2-Dec-10 19:20 
QuestionExcel automation: SetValue() function throws exception when user changes sheets Pin
garyflet2-Dec-10 9:14
garyflet2-Dec-10 9:14 
Using Visual C++ 6 on Windows XP and automation to send data to Excel 2007. With my software there are occasions in which I repeatedly open an Excel file, write to a specified sheet, then close without saving. However, if the user clicks on a different sheet, when my code gets to SetValue(), it throws a COleDispatchException. The description of the error is a blank string. Why doesn't Excel just keep writing to the original sheet? How can I make that happen? A less desirable workaround would be to somehow disable Excel while my code is writing to Excel. Making Excel invisible while writing is not acceptable. Incidentally, there was no problem with Excel 2000.

//worksheet wrapper class
  CXLWorksheet worksheet;
	//range wrapper class
  CXLRange range;
	CString strRange;

  VARIANT v, vRet, vNotPassed, vBOOL;
  VariantInit(&v);
  VariantInit(&vRet);
  VariantInit(&vNotPassed);
  VariantInit(&vBOOL);
  V_VT(&vNotPassed) = VT_ERROR;
  V_ERROR(&vNotPassed) = DISP_E_PARAMNOTFOUND;
	V_VT(&vBOOL) = VT_BOOL;

	LPDISPATCH pXLAppDispatch = NULL;
	LPUNKNOWN lpUnk; CLSID clsid; 
	if( S_OK == ::CLSIDFromProgID(L"Excel.Application", &clsid) )
	{
		if( S_OK == ::GetActiveObject(clsid, NULL, &lpUnk) )
		{
			VERIFY(lpUnk->QueryInterface(IID_IDispatch, (void**)&pXLAppDispatch) == S_OK);
			//m_XLApp is Excel application class
			m_XLApp.AttachDispatch( pXLAppDispatch );
			lpUnk->Release();
		}
	}


	V_BOOL(&vBOOL) = TRUE;
	m_XLApp.SetVisible(vBOOL);
	

	VARIANT vFilename; 
	V_VT(&vFilename) = VT_BSTR;

//strFileName is full path of Excel file name
	int Len = strFileName.GetLength();
	V_BSTR(&vFilename) = SysAllocString(
		T2COLE(strFileName.GetBuffer(Len+1)) );

//m_workbooks is workbooks wrapper class
	if( !m_workbooks.m_lpDispatch )
	{
		m_workbooks.AttachDispatch( m_XLApp.GetWorkbooks() );

		TRY
		{
		vRet = m_workbooks.Open(vFilename, 
			vNotPassed,vNotPassed,vNotPassed,vNotPassed,
			vNotPassed,vNotPassed,vNotPassed,vNotPassed,
			vNotPassed,vNotPassed,vNotPassed );
		}
		CATCH_ALL(e)
		{
			//* Create a new one instead
			vRet = m_workbooks.Add(vNotPassed);
		}
		END_CATCH_ALL

	//m_workbook is workbook wrapper class
		if( V_VT(&vRet) == VT_DISPATCH )
			m_workbook.AttachDispatch( V_DISPATCH(&vRet) );
	}

	LPDISPATCH lpDisp;
	//for this example, nXLWorksheetNo == 1
	lpDisp = m_workbook.GetWorksheets(nXLWorksheetNo);
	worksheet.AttachDispatch(lpDisp);

	//in this example, nCols == 10	
	for( int nCol=0; nCol<nCols; nCol++ )
	{
		//* Work on an entire column at a time
			//* Add Column (field) title

		//strRange == "A1"
		strRange = pTable->m_strXLTopLeftCell;
		XLAdjustCol(strRange, nCol);
		V_VT(&v) = VT_BSTR;
		V_BSTR(&v) = SysAllocString(
			T2COLE(strRange.GetBuffer(0)) );
		LPDISPATCH pDisp = worksheet.Range1(v);
		VariantClear(&v);
		range.AttachDispatch(pDisp);
		VariantInit(&v);
		V_VT(&v) = VT_BSTR;
		//in this example, strHeader is "nVar_VSL" (no significance to bug)
		V_BSTR(&v) = strHeader.AllocSysString();
		if( V_VT(&v) != VT_EMPTY )
		{
			try
			{
			//here is where the bug is
			//range throws exception if user changes to another sheet
			//why doesn't it just keep writing to current sheet?
			range.SetValue(v);
			}
			catch(COleDispatchException *pEx)
			{
				//most of the time crashes when nCol==0 or 8
				TRACE("nCol = %d\n",nCol);
				pEx->Delete();
			}
			VariantClear(&v);
		}
                   }
LPDISPATCH CXLWorksheet::Range1(const VARIANT& Cell1)
{
	LPDISPATCH result;
	static BYTE parms[] =
		VTS_VARIANT;
	InvokeHelper(0xc5, DISPATCH_PROPERTYGET, VT_DISPATCH, (void*)&result, parms,
		&Cell1);
	return result;
}
LPDISPATCH CXLWorkbook::GetWorksheets(short nXLWorksheetNo)
{
	VARIANT vWorkSheetNo;
	VariantInit(&vWorkSheetNo);
	V_VT(&vWorkSheetNo) = VT_I2;
	V_I2(&vWorkSheetNo) = nXLWorksheetNo;
	static BYTE parms[] =
		VTS_VARIANT;
	LPDISPATCH result;
	InvokeHelper(0x1ee, DISPATCH_PROPERTYGET, VT_DISPATCH, (void*)&result, parms, &vWorkSheetNo );
	return result;
}

QuestionMy own written DLL not accessible in dialog based app; [modified] Pin
AmbiguousName2-Dec-10 8:41
AmbiguousName2-Dec-10 8:41 
AnswerRe: My own written DLL not accessible in dialog based app; Pin
Aescleal2-Dec-10 9:39
Aescleal2-Dec-10 9:39 
AnswerRe: My own written DLL not accessible in dialog based app; Pin
Sauro Viti2-Dec-10 9:39
professionalSauro Viti2-Dec-10 9:39 
GeneralRe: My own written DLL not accessible in dialog based app; Pin
AmbiguousName2-Dec-10 22:31
AmbiguousName2-Dec-10 22:31 
GeneralRe: My own written DLL not accessible in dialog based app; Pin
Sauro Viti3-Dec-10 1:36
professionalSauro Viti3-Dec-10 1:36 
Question[C++] First Order Logic (FOL) Parsing and Compiling Pin
erotavlas_turbo2-Dec-10 3:23
erotavlas_turbo2-Dec-10 3:23 
AnswerRe: [C++] First Order Logic (FOL) Parsing and Compiling Pin
Alain Rist2-Dec-10 6:43
Alain Rist2-Dec-10 6:43 
Questionhow to define 'window.myapp' instead of 'window.external' in IE Control with js? Pin
Jun Jin Pyo2-Dec-10 0:19
Jun Jin Pyo2-Dec-10 0:19 
Questiondisabling alt+space Pin
learningvisualc1-Dec-10 23:32
learningvisualc1-Dec-10 23:32 
AnswerRe: disabling alt+space Pin
Rajesh R Subramanian2-Dec-10 0:08
professionalRajesh R Subramanian2-Dec-10 0:08 
GeneralRe: disabling alt+space Pin
pasztorpisti2-Dec-10 3:30
pasztorpisti2-Dec-10 3:30 
AnswerRe: disabling alt+space Pin
Emilio Garavaglia2-Dec-10 22:30
Emilio Garavaglia2-Dec-10 22:30 
GeneralRe: disabling alt+space Pin
pasztorpisti2-Dec-10 23:00
pasztorpisti2-Dec-10 23:00 
GeneralRe: disabling alt+space Pin
Rajesh R Subramanian2-Dec-10 23:38
professionalRajesh R Subramanian2-Dec-10 23:38 
GeneralRe: disabling alt+space Pin
pasztorpisti3-Dec-10 0:13
pasztorpisti3-Dec-10 0:13 
GeneralRe: disabling alt+space Pin
Emilio Garavaglia3-Dec-10 1:23
Emilio Garavaglia3-Dec-10 1:23 
QuestionHow to code starting program with windows Pin
Max++1-Dec-10 22:18
Max++1-Dec-10 22:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.