Solved

MFC-OLE (Excel) Example

Posted on 1999-01-21
2
445 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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…

685 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