Solved

MFC-OLE (Excel) Example

Posted on 1999-01-21
2
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Accepted Solution

by:
sankar012898 earned 50 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

MongoDB Through a MySQL Lens

This article looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.

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…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…

631 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