Solved

Creating an instance of an open Excel application

Posted on 1998-09-13
1
381 Views
Last Modified: 2008-02-07
I'm writing a small app that uses OLE to communicate with Excel.  I know how to automate opening a new Excel application, adding a new workbook, and attaching an IDispatch pointer to Sheet #1.  How do I initiate communication with Sheet1 of Book1 of an Excel application  that is already open?  Any sample code appreciated.  Thanks.

0
Comment
Question by:dave140
1 Comment
 

Accepted Solution

by:
bobbym earned 50 total points
ID: 1172621
Try using the compiler COM support of VC++ instead of the IDispatchDriver. The COM support uses smart pointers so that you do not have to worry about reference counting and such things. It also respresents better the Automation hierarachy.

In order to get the smart pointers for Excel include something like this in your .h file. This would generate the appropriate .tlh and .tli files for the smart pointers

#ifndef       _OFFICE_TYPE_LIBRARIES_INCLUDED
#define  _OFFICE_TYPE_LIBRARIES_INCLUDED
#undef RGB

#pragma warning( disable : 4146)  

#import  "c:\Msoffice\Office\MSO97.dll"
#import  "c:\Program Files\Common Files\Microsoft Shared\Vba\VBEEXT1.olb"
#import  "c:\Msoffice\Office\excel8.olb"
#endif


and somewhere in your .CPP file do something like this to run Excel.

Excel::_ApplicationPtr pExcelApp;
try
{
  Excel::_WorkbookPtr   pWorkbook;

  _variant_t vtIndex((long)1);
  _variant_t vtCell("A1");
  _variant_t vtValue("Some value");

  HRESULT hRes = pExcelApp.CreateInstance(_uuidof(Excel::Application));
  pExcelApp->Visible = Office::msoTrue;

  pWorkbook = pExcelApp->Workbooks->Open((LPCTSTR)"Some file name");

  Excel::SheetsPtr pWorksheets = m_pWorkbook->Worksheets;
  Excel::_WorksheetPtr pWorksheet  = pWorksheets->Item[vtIndex];
  pWorksheet->Range[vtCell]->Value = vtValue;
}
catch (_com_error err)
{
AfxMessageBox("COM Error Description:" + CString((LPCTSTR)err.Description()));
}

This code should write something in A1 in Excel (I am not sure I did not tested it but this is the idea :)). If you send me an e-mail (bobby_milanov@hotmail.com) I will send you a small C++ to make your life easier.

 Also note that you can also control when a smart pointer is linked to the object . If you call Release to a pointer this calls reelase to the underlying interface and then detaches the smart pointer.

Hope that helped :).

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best sources to up-to-date in C++? 8 82
How to convert MFC APP to Win32 APP. 19 80
What is sub-make ? 2 60
object oriented programming comparison 5 72
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 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