Solved

Creating an instance of an open Excel application

Posted on 1998-09-13
1
379 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

862 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

25 Experts available now in Live!

Get 1:1 Help Now