Click to See Complete Forum and Search --> : How to copy an Excel worksheet using automation


May 18th, 1999, 11:20 AM
I have to write an export function to Excel.
Using automation I am able to read or write cells.
But canīt copy a worksheet using the folowing code

#import <mso97.dll> no_namespace rename("DocumentProperties", "DocumentPropertiesXL")
#import <vbeext1.olb> no_namespace
#import <excel8.olb> rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces

...

_ApplicationPtr pXL;
pXL.CreateInstance(L"Excel.Application.8");

WorkbooksPtr pBooks = pXL->Workbooks;
_WorkbookPtr pBook = pBooks->Open("d:\\simXLS\\myKlip.xls");

WorksheetsPtr pSheets = pBook->Worksheets;
_WorksheetPtr pSheet = pSheets->GetItem("V1");

pSheet->Range["D16"]->Value = 75.0; // Ok
pSheet->Copy("V1"); // Error



This results in an error-message like this
"IDispach error #1004"

Can anyone help?

May 18th, 1999, 12:05 PM
It expects a sheet pointer there, not a string name.
So, the following is an error.
pSheet->Copy("V1"); // Error

1. First get the sheet pointer before which you want to copy to.
2. Then give that pointer as input to 'Copy' function.
The following code works fine (I tried it.), it puts a copy of the sheet before itself.
.....
Excel::_WorksheetPtr ws1 = wb1->Worksheets->Item[IntV(1)];
ws1->Copy( ws1.GetInterfacePtr() );
......
When in doubt, try Macros Recording in Excel itself. It might help. I got the idea after recording only.