Excel Automation c++ make visible

Posted on 2003-03-28
Medium Priority
Last Modified: 2010-08-05
The application I am developing needs to open excel and paste the contents of the clipboard to a new worksheet. I have managed to get the opening excel and pasting the clipboard to work, but I cannot get excel to display itself for the user. Excel will display itself if it was not previously running and had to start, but if it was running already control remains with my program, excel just adds a worksheet to the taskbar (not much of an indication to the user something has happened)

The code I'm using is
     covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

     _Application app;

     Workbooks books;
     _Workbook book;

      Worksheets sheets;
      _Worksheet sheet;
      // Translate server ProgID into a CLSID. ClsidFromProgID
      // gets this information from the registry.
      CLSID clsid;
      CLSIDFromProgID(L"Excel.Application", &clsid);  

      // Get an interface to the running instance, if any..
      IUnknown *pUnk;
      HRESULT hr = GetActiveObject(clsid, NULL, (IUnknown**)&pUnk);

       //excel not running so have to create a new instance

               AfxMessageBox("Couldn't start Excel and get Application object.");
 //else get its dispatch handle
          // Get IDispatch interface for Automation...
          IDispatch *pDisp=NULL;
          hr = pUnk->QueryInterface(IID_IDispatch, (void **)&pDisp);
          // Release the no-longer-needed IUnknown...
               AfxMessageBox("Couldn't get Excel IDispatch Interface.");
     //Get a new workbook.
     books = app.GetWorkbooks();
     book = books.Add (covOptional);

     //Get the first sheet.
     sheets =book.GetSheets();
     sheet = sheets.GetItem(COleVariant((short)1));

     //show excel
     //note only seems to make it the visible to the user the first time it is opened
     //give user control

My question is, how do I get excel to display itself to the user if it was already running. SetVisible and setUserControl don't seem to do the job?
Question by:fuzzbury
  • 2

Expert Comment

ID: 8942655
 Instead of :

  Try this:
    app.SetWindowState(1);  //I don't know 1 is different from your value but it works for me

Good luck


Accepted Solution

hxn127 earned 150 total points
ID: 8942952
Don't forget to activate your workbook after add.

good luck

Author Comment

ID: 8943084
Thats for that.

It turns out the problem was that I was calling the create workbook from a full screen modal dialog and Excel doesn't become the top window over a modal dialog if the work book was created programitcally.

Your comments are approciated though.

The TRUE , 1 thing will be the same in CV++ because TRUE is define via a macro to be 1

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

615 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