ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   exception in excel while writting to it (https://www.excelbanter.com/excel-programming/403808-exception-excel-while-writting.html)

Gaurav[_7_]

exception in excel while writting to it
 
Hi,

i am getting an exception when trying to open an existing file and
write something into it.
if i create a new worksheet, the code works fine.
I am not able to findout the problem. Please look at the code and let
me know what is wrong with it.


ERROR_PROTECT_BEGIN_PROXY()


Range rp = m_Excel.GetRange(COleVariant("C1"),
COleVariant("C1"));
rp.SetValue2(COleVariant("Hi")); // <------throwing
Exception


ERROR_PROTECT_END_PROXY("Error Copying")


void __stdcall CExcelProxy::OnWorkBookOpen(IDispatch *Wb)
{
DEBUG_MESSAGE("Opening Workbook");


// Load Macro into VBE for callbacks
LoadMacro();


m_pWorkBook = Wb;
m_pWorkSheet = m_pWorkBook-GetActiveSheet();


m_pWorkSheet-Protect(false);


// Set class-wide indicator


IDispatchPtr pDsp;
// Get reference to comand bars
pDsp = (Office::CommandBarsPtr)m_pApp-GetCommandBars();
m_pCmdBars = pDsp.Detach();


// Release pointers
ReleaseWorkBookPtrs();
ReleaseCommandBarPtrs();



}


void __stdcall CExcelProxy::OnNewWorkBook(IDispatch *Wb)
{
IDispatchPtr pDsp;
// Get reference to comand bars
pDsp = (Office::CommandBarsPtr)m_pApp-GetCommandBars();
m_pCmdBars = pDsp.Detach();

// Show "Save" menus
UpdateSaveMenus(true);


// Release all pointers
ReleaseCommandBarPtrs();


// Load macro for callbacks
LoadMacro();

joel

exception in excel while writting to it
 
See if this works

with m_Excel
Range rp = .GetRange(.COleVariant("C1"), .COleVariant("C1"));
rp.SetValue2(.COleVariant("Hi"));
end with


"Gaurav" wrote:

Hi,

i am getting an exception when trying to open an existing file and
write something into it.
if i create a new worksheet, the code works fine.
I am not able to findout the problem. Please look at the code and let
me know what is wrong with it.


ERROR_PROTECT_BEGIN_PROXY()


Range rp = m_Excel.GetRange(COleVariant("C1"),
COleVariant("C1"));
rp.SetValue2(COleVariant("Hi")); // <------throwing
Exception


ERROR_PROTECT_END_PROXY("Error Copying")


void __stdcall CExcelProxy::OnWorkBookOpen(IDispatch *Wb)
{
DEBUG_MESSAGE("Opening Workbook");


// Load Macro into VBE for callbacks
LoadMacro();


m_pWorkBook = Wb;
m_pWorkSheet = m_pWorkBook-GetActiveSheet();


m_pWorkSheet-Protect(false);


// Set class-wide indicator


IDispatchPtr pDsp;
// Get reference to comand bars
pDsp = (Office::CommandBarsPtr)m_pApp-GetCommandBars();
m_pCmdBars = pDsp.Detach();


// Release pointers
ReleaseWorkBookPtrs();
ReleaseCommandBarPtrs();



}


void __stdcall CExcelProxy::OnNewWorkBook(IDispatch *Wb)
{
IDispatchPtr pDsp;
// Get reference to comand bars
pDsp = (Office::CommandBarsPtr)m_pApp-GetCommandBars();
m_pCmdBars = pDsp.Detach();

// Show "Save" menus
UpdateSaveMenus(true);


// Release all pointers
ReleaseCommandBarPtrs();


// Load macro for callbacks
LoadMacro();


Gaurav[_7_]

exception in excel while writting to it
 
On Jan 8, 6:09*am, Joel wrote:
See if this works

with m_Excel
* * * *Range rp = .GetRange(.COleVariant("C1"), .COleVariant("C1"));
* * * * rp.SetValue2(.COleVariant("Hi"));
end with



"Gaurav" wrote:
Hi,


i am getting an exception when trying to open an existing file and
write something into it.
if i create a new worksheet, the code works fine.
I am not able to findout the problem. Please look at the code and let
me know what is wrong with it.


* * * * ERROR_PROTECT_BEGIN_PROXY()


* * * * Range rp = m_Excel.GetRange(COleVariant("C1"),
COleVariant("C1"));
* * * * rp.SetValue2(COleVariant("Hi")); * // *<------throwing
Exception


* * * * ERROR_PROTECT_END_PROXY("Error Copying")


void __stdcall CExcelProxy::OnWorkBookOpen(IDispatch *Wb)
{
* * * * DEBUG_MESSAGE("Opening Workbook");


* * * * // Load Macro into VBE for callbacks
* * * * LoadMacro();


* * * * m_pWorkBook = Wb;
* * * * m_pWorkSheet = m_pWorkBook-GetActiveSheet();


* * * * m_pWorkSheet-Protect(false);


* * * * // Set class-wide indicator


* * * * IDispatchPtr pDsp;
* * * * // Get reference to comand bars
* * * * pDsp = (Office::CommandBarsPtr)m_pApp-GetCommandBars();
* * * * m_pCmdBars = pDsp.Detach();


* * * * // Release pointers
* * * * ReleaseWorkBookPtrs();
* * * * ReleaseCommandBarPtrs();


}


void __stdcall CExcelProxy::OnNewWorkBook(IDispatch *Wb)
{
* * * * IDispatchPtr pDsp;
* * * * // Get reference to comand bars
* * * * pDsp = (Office::CommandBarsPtr)m_pApp-GetCommandBars();
* * * * m_pCmdBars = pDsp.Detach();


* * * * // Show "Save" menus
* * * * UpdateSaveMenus(true);


* * * * // Release all pointers
* * * * ReleaseCommandBarPtrs();


* * * * // Load macro for callbacks
* * * * LoadMacro();- Hide quoted text -


- Show quoted text -


Used the same code and it is not working

joel

exception in excel while writting to it
 
I'm not an expert at using Excel with C++ and MFC. try your original code
and change from: Value2 to: Value (eliminate the 2).

"Gaurav" wrote:

On Jan 8, 6:09 am, Joel wrote:
See if this works

with m_Excel
Range rp = .GetRange(.COleVariant("C1"), .COleVariant("C1"));
rp.SetValue2(.COleVariant("Hi"));
end with



"Gaurav" wrote:
Hi,


i am getting an exception when trying to open an existing file and
write something into it.
if i create a new worksheet, the code works fine.
I am not able to findout the problem. Please look at the code and let
me know what is wrong with it.


ERROR_PROTECT_BEGIN_PROXY()


Range rp = m_Excel.GetRange(COleVariant("C1"),
COleVariant("C1"));
rp.SetValue2(COleVariant("Hi")); // <------throwing
Exception


ERROR_PROTECT_END_PROXY("Error Copying")


void __stdcall CExcelProxy::OnWorkBookOpen(IDispatch *Wb)
{
DEBUG_MESSAGE("Opening Workbook");


// Load Macro into VBE for callbacks
LoadMacro();


m_pWorkBook = Wb;
m_pWorkSheet = m_pWorkBook-GetActiveSheet();


m_pWorkSheet-Protect(false);


// Set class-wide indicator


IDispatchPtr pDsp;
// Get reference to comand bars
pDsp = (Office::CommandBarsPtr)m_pApp-GetCommandBars();
m_pCmdBars = pDsp.Detach();


// Release pointers
ReleaseWorkBookPtrs();
ReleaseCommandBarPtrs();


}


void __stdcall CExcelProxy::OnNewWorkBook(IDispatch *Wb)
{
IDispatchPtr pDsp;
// Get reference to comand bars
pDsp = (Office::CommandBarsPtr)m_pApp-GetCommandBars();
m_pCmdBars = pDsp.Detach();


// Show "Save" menus
UpdateSaveMenus(true);


// Release all pointers
ReleaseCommandBarPtrs();


// Load macro for callbacks
LoadMacro();- Hide quoted text -


- Show quoted text -


Used the same code and it is not working



All times are GMT +1. The time now is 08:38 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com