View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Jen Leonard Jen Leonard is offline
external usenet poster
 
Posts: 1
Default MFC - CopyFromRecordset - Copy data to Excel

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