Solved

MFC-OLE (Excel) Example

Posted on 1999-01-21
2
433 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 50 total points
Comment Utility
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
Comment Utility
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now