RE: Help needed with opening an Excel file from an embedded excel appl

Tech-Archive recommends: Fix windows errors by optimizing your registry



One thing you might try doing is going through the wrapper class that was
created for you by MFC. Insert a breakpoint at the wbs.Open(...) Then when
you reach it, jump into the code (F11). You should jump into the
InvokeHelper(...) and then jump into the InvokeHelperV(...). In
InvokeHelperV(...) ensure the proper arguments are being assigned to the
DISPPARAMS. Most importantly is where the dispatch invokes the method [it is
where it sets the variable sc, which is of type SCODE]. Use the Error Lookup
tool to look up the SCODE; you may need to convert to hexidecimal. Also, the
varialbes pexcepinfo and puArgErr -- parameters in Invoke -- should also
assist in debugging the problem.

Again, this is to help DEBUG the problem; it is not an absolute solution.
If you still need help with, I could assist you with a solution. It involves
bypassing the InvokeHelper() and rewriting the Open(...) function wrapped by
MFC. Basically, you delete -- or comment out -- the two lines created by
MFC: InvokeHelper and static BYTE params. Instead, you setup a DISPPARAMS
structure, containing the number of arguments and named arguments, etc. Then
you call Invoke() from the member m_lpDispatch.

Again, if you need a walk through with this method, just tell me, and I'll
be glad to assist you in accomplishing your task, but I'd first try to figure
out why the Open() method is not working. Good luck.

Saul775

"ricky2m_1974@xxxxxxxxxxx" wrote:

Hello, I have a SDI MFC application that uses the following code to
create an empty embedded excel 2003 application:

(Extracted from http://support.microsoft.com/kb/311546)

void CEmbed_ExcelView::EmbedAutomateExcel()
{
//Change the cursor so that the user knows that something exciting is
going
//on.
BeginWaitCursor();

CEmbed_ExcelCntrItem* pItem = NULL;
TRY
{
//Get the document associated with this view, and be sure that it is
//valid.
CEmbed_ExcelDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

//Create a new item associated with this document, and be sure that
//it is valid.
pItem = new CEmbed_ExcelCntrItem(pDoc);
ASSERT_VALID(pItem);

// Get a Class ID for the Excel ***.
// This is used in creation.
CLSID clsid;
if(FAILED(::CLSIDFromProgID(L"Excel.***",&clsid)))
//Any exception will do. You just need to break out of the
//TRY statement.
AfxThrowMemoryException();

// Create the Excel embedded item.
if(!pItem->CreateNewItem(clsid))
//Any exception will do. You just need to break out of the
//TRY statement.
AfxThrowMemoryException();

//Make sure the new CContainerItem is valid.
ASSERT_VALID(pItem);

// Start the server to edit the item.
pItem->DoVerb(OLEIVERB_SHOW, this);

// As an arbitrary user interface design, this sets the
// selection to the last item inserted.
m_pSelection = pItem; // Set selection to last inserted. item
pDoc->UpdateAllViews(NULL);

//Query for the dispatch pointer for the embedded object. In
//this case, this is the Excel work***.
LPDISPATCH lpDisp;
lpDisp = pItem->GetIDispatch();

//Add text in cell A1 of the embedded Excel ***.
CWorkbook wb;
CWorksheets wsSet;
CWork*** ws;
CRange CRange;
CApplication app;

//Set CWorkbook wb to use lpDisp, the IDispatch* of the
//actual workbook.
wb.AttachDispatch(lpDisp);

//Get the application for the work***.
app = wb.get_Application();

//Get the first work*** in the workbook.
wsSet = wb.get_Worksheets();
ws = wsSet.get_Item(COleVariant((short)1));

//Get a CRange object that corresponds to cell A1.
CRange = ws.get_Range(COleVariant("A1"), COleVariant("A1"));

//Fill A1 with the string "Hello, World!"
CRange.put_Value2(COleVariant("Hello, World!"));

//NOTE: If you are automating Excel 2002, the CRange.SetValue method
has an
//additional optional parameter that specifies the data type.
Because the
//parameter is optional, existing code will still work correctly, but
new
//code should use the new convention. The call for Excel2002 should

//resemble the following:

//CRange.SetValue( ColeVariant( (long)DISP_E_PARAMNOTFOUND, VT_ERROR
),
// COleVariant("Hello, World!"));
}

//Clean up if something went wrong.
CATCH(CException, e)
{
if (pItem != NULL)
{
ASSERT_VALID(pItem);
pItem->Delete();

}
AfxMessageBox(IDP_FAILED_TO_CREATE);
}
END_CATCH

//Set the cursor back to normal so that the user knows that exciting
stuff
//is no longer happening.
EndWaitCursor();
}


The above code works fine but I need it to open an existing Excel file.


I have tried inserting the following code to the end of the function to
open the Excel file 'Test.xls', but nothing happens:

COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

wbs.Open("C:\\Test.xls", covOptional, covOptional, covOptional,
covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional );

Any help will be much appreciated.


.


Quantcast