Excel Automation c++ make visible
Posted on 2003-03-28
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
// Translate server ProgID into a CLSID. ClsidFromProgID
// gets this information from the registry.
// Get an interface to the running instance, if any..
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...
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.
sheet = sheets.GetItem(COleVariant((short)1));
//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?