Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Creating an instance of an open Excel application

Posted on 1998-09-13
1
Medium Priority
?
393 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
[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
1 Comment
 

Accepted Solution

by:
bobbym earned 100 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

636 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