Solved

Creating an instance of an open Excel application

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

  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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 how to clear a vector as well as how to detect empty vectors in C++.

758 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

17 Experts available now in Live!

Get 1:1 Help Now