Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MFC-OLE (Excel) Example

Posted on 1999-01-21
2
Medium Priority
?
461 Views
Last Modified: 2013-11-25
Can any one give me example of excel integration ? ( Automation)
I want to move data from list control to specified cells in excel, save it and view it

Thanks
0
Comment
Question by:kolpurath
2 Comments
 

Accepted Solution

by:
sankar012898 earned 150 total points
ID: 1328112
hi kolpurath ,
     
            There is a OLB file which comes  from excel itself .
 It is  excel8.olb in the directory c:\program files\microsoft office\office . Just go to  classwizard  and add a new class
and select from a Type Library option and select this OLB file .
It will add  Excel8.cpp &&  excel8.h to your project .Most of
the classes  will be derived from COleDispatchDriver .

The code is below .

// Excel objects defined in excel8.h
_Application  m_ExcelApp;
Workbooks     m_ExcelWorkBooks;
_Workbook     m_ExcelWorkBook;
Worksheets    m_ExcelSheets;
_Worksheet    m_ExcelSheet;
Range            m_range;
CString m_value;

// Start Excel and get Application object...
if(!m_ExcelApp.CreateDispatch("Excel.Application"))
{
  AfxMessageBox("Couldn't start Excel.");  
  return -1;
}

//Make Excel Visible and display a message
m_ExcelApp.SetVisible(TRUE);        

VARIANT vOptional;
::VariantInit(&vOptional);
vOptional.vt = VT_ERROR;
V_ERROR(&vOptional) = DISP_E_PARAMNOTFOUND;
      
LPDISPATCH lpDispatch = m_ExcelApp.GetWorkbooks();

m_ExcelWorkBooks.AttachDispatch(lpDispatch);

m_ExcelWorkBook = m_ExcelWorkBooks.Open(pThis->m_fileName,
                   vOptional, vOptional, vOptional , vOptional,
                   vOptional, vOptional, vOptional, vOptional,
                   vOptional, vOptional, vOptional, vOptional );

      
lpDispatch = m_ExcelWorkBook.GetSheets();
m_ExcelSheets.AttachDispatch(lpDispatch);

lpDispatch = m_ExcelSheets.GetItem( COleVariant((short)(1)) );
m_ExcelSheet.AttachDispatch(lpDispatch);
      
lpDispatch = m_ExcelSheet.GetRange(COleVariant(pThis->m_row),
                                           COleVariant(pThis->m_column));
ASSERT(lpDispatch);

m_range.AttachDispatch(lpDispatch);
m_range.SetValue(COleVariant(m_value));


// after using
// Now release all the dispatch pointers
if(m_range.m_lpDispatch)
{
   m_range.ReleaseDispatch();
}

if(m_ExcelSheet.m_lpDispatch)
{
  m_ExcelSheet.ReleaseDispatch();
}      

if(m_ExcelSheets.m_lpDispatch)
{
   m_ExcelSheets.ReleaseDispatch();
}

if(m_ExcelWorkBook.m_lpDispatch)
   m_ExcelWorkBook.ReleaseDispatch();
      
if(m_ExcelWorkBooks.m_lpDispatch)
{
   m_ExcelWorkBooks.Close();
   m_ExcelWorkBooks.ReleaseDispatch();
}

if(m_ExcelApp.m_lpDispatch)
{
   m_ExcelApp.Quit();
   m_ExcelApp.ReleaseDispatch();
}
This code will open a  given file , set values in some cells
and also  save it , close it  .



sankar







0
 
LVL 4

Expert Comment

by:nil_dib
ID: 1328113
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question