Aha, I see the problem now.
This seems to be a Excel COM more rather than a CVI issue. I replicated the same thing in VB 6.0. Apparently if its a Chart, it gets added right before the last sheet. Here is the VB code i tried with Excel 2003. I don't whether Microsoft changed this behavior recently
excelApp.Visible = True
Set excelBook = excelApp.Workbooks.Add
Set excelSheet(0) = excelBook.Worksheets.Add
With excelBook
.Sheets.Add After:=.Worksheets(Worksheets.Count), Type:=xlChart
End With
The only way around this would be to move the newly created chart to the end. I added some code to the ExcelRpt_ChartNew function in reportexcel.c like so. I tried this with CVI 7.1 and it did the trick.
==========================================
HRESULT CVIFUNC ExcelRpt_ChartNew(CAObjHandle workbookHandle, int insertBeforeIndex, CAObjHandle *chartHandle)
{
HRESULT __result = S_OK;
CAObjHandle sheetsHandle = 0;
CAObjHandle insWorksheetHandle = 0;
VARIANT SheetV;
LPDISPATCH dispatchPtr = NULL;
int sheetnum;
CA_VariantSetEmpty (&SheetV);
if(!chartHandle)
__caErrChk (E_INVALIDARG);
__caErrChk (Excel_GetProperty (workbookHandle, NULL, Excel_WorkbookSheets, CAVT_OBJHANDLE, &sheetsHandle));
.
.
.
.
__caErrChk (Excel_SheetsAdd (sheetsHandle, NULL, (insertBeforeIndex != -1) ? SheetV : CA_DEFAULT_VAL,
(insertBeforeIndex == -1) ? SheetV : CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_VariantDouble(ExcelConst_xlChart), chartHandle));
if(insertBeforeIndex== -1)
Excel_ChartMove (*chartHandle, NULL, CA_DEFAULT_VAL, SheetV);
Error:
CA_DiscardObjHandle(sheetsHandle);
CA_DiscardObjHandle(insWorksheetHandle);
CA_VariantClear(&SheetV);
if (dispatchPtr)
dispatchPtr->lpVtbl->Release(dispatchPtr);
return __result;
}
=========================================
I'll report this issue to the developers so we can address this in a better way for future releases.
Hope this helps
Bilal Durrani
NI