Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MFC-OLE (Excel) Example

Posted on 1999-01-21
2
Medium Priority
?
449 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 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
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…
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…

722 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