MFC - CopyFromRecordset - Copy data to Excel

J

Jen Leonard

Hi,
I am trying to copy a set of data from my Access database to Excel. I am
using an MFC program where I have embedded an excel spreadsheet into my MFC
Dialog. I reviewed the knowledgebase articles and found:
http://support.microsoft.com/kb/243394/en-us
This article showed me how to copy a set of data, and I tried it and it
works. The problem is that it starts a new instance of excel and my data is
there and not in my MFC Dialog.

I revised the code using the existing Excel workbook/worksheet and I get an
exception on the call to CopyFromRecordset. Here is my code:

//Then get the first worksheet in the workbook
wsSet = wb.GetWorksheets();
ws = wsSet.GetItem(COleVariant((short)(1)));
charts = wb.GetCharts();
chart = charts.GetItem(COleVariant((short)(1)));

CDaoDatabase db;
CDaoRecordset rs;
CString sConn;
long lNumCols;

//Get a recordset that represents all the records in the Products
//table of the sample Northwind database
sConn.Format("%s\\Test.mdb", m_DWorkingDir);
db.Open(sConn, FALSE, FALSE);
rs.m_pDatabase = &db;
CDaoQueryDef query(&db);
query.Open("Main Screen");

rs.Open(&query);

lNumCols = rs.GetFieldCount();

Range oRange;
COleDispatchDriver rs2;
rs2.AttachDispatch((LPDISPATCH) rs.m_pDAORecordset);
oRange = ws.GetRange(COleVariant("A2"), vOpt);
oRange.CopyFromRecordset((LPUNKNOWN) rs.m_pDAORecordset,vOpt, vOpt);

Any ideas!?!? I really need some help to figure this out!
Jennifer
 

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