?
Solved

Excel Automation c++ make visible

Posted on 2003-03-28
3
Medium Priority
?
1,117 Views
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
{
     COleVariant
     covTrue((short)TRUE),
     covFalse((short)FALSE),
     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

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

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

     //show excel
     //note only seems to make it the visible to the user the first time it is opened
     app.SetWindowState(MsExcel::xlMaximized);
     app.SetVisible(TRUE);
     //give user control
     app.SetUserControl(TRUE);
}


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?
0
Comment
Question by:fuzzbury
[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
  • 2
3 Comments
 

Expert Comment

by:hxn127
ID: 8942655
 Instead of :
    app.SetWindowState(...);
    app.SetVisible(TRUE);

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

Good luck


 
0
 

Accepted Solution

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

good luck
0
 

Author Comment

by:fuzzbury
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
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 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 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.
Suggested Courses

762 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