_WorkSheet::Paste usage
-
Hi, I am trying to paste a .txt file onto an Excel spreadsheet Off hand it seemed to me the logical method would be _WorkSheet::Paste I have done countless google searches and have yet to come up with C++ exampe of how this method is used This is the prototype of the method in Excel.h
void Paste(const VARIANT& Destination, const VARIANT& Link);
When I did research on the data type VARIANT the documentation said its a union for many data types One of them being a LDISPATCH type The code below is a snipet of the code I have been stuggling with to get the _WorkSheet::Paste method to work My logic was to get a LDISPATCH pointer to the range off cells I was trying to paste onto However I keep on running into exceptions If anyone could shead some light on this I would be very gratefull thanks in advance
CI46023App theApp; // Main CwinThread
_Application app; // excel application object
_Workbook objBook; // workbook object
Workbooks objBooks; // ditto
Worksheets objSheets; // worksheet object
_Worksheet objSheet; // worksheet
LPDISPATCH myrangeptr;app.CreateDispatch("Excel.Application");
objBooks = app.GetWorkbooks();
objBook = objBooks.Add(VOptional);
objSheets = objBook.GetWorksheets();
objSheet = objSheets.GetItem(COleVariant((short)1));
myrangeptr = objSheet.GetRange(COleVariant(TEXT("A1")),COleVariant(TEXT("M34")));
objSheet.Paste((VARIANT &)myrangeptr, COleVariant((short)TRUE)); -
Hi, I am trying to paste a .txt file onto an Excel spreadsheet Off hand it seemed to me the logical method would be _WorkSheet::Paste I have done countless google searches and have yet to come up with C++ exampe of how this method is used This is the prototype of the method in Excel.h
void Paste(const VARIANT& Destination, const VARIANT& Link);
When I did research on the data type VARIANT the documentation said its a union for many data types One of them being a LDISPATCH type The code below is a snipet of the code I have been stuggling with to get the _WorkSheet::Paste method to work My logic was to get a LDISPATCH pointer to the range off cells I was trying to paste onto However I keep on running into exceptions If anyone could shead some light on this I would be very gratefull thanks in advance
CI46023App theApp; // Main CwinThread
_Application app; // excel application object
_Workbook objBook; // workbook object
Workbooks objBooks; // ditto
Worksheets objSheets; // worksheet object
_Worksheet objSheet; // worksheet
LPDISPATCH myrangeptr;app.CreateDispatch("Excel.Application");
objBooks = app.GetWorkbooks();
objBook = objBooks.Add(VOptional);
objSheets = objBook.GetWorksheets();
objSheet = objSheets.GetItem(COleVariant((short)1));
myrangeptr = objSheet.GetRange(COleVariant(TEXT("A1")),COleVariant(TEXT("M34")));
objSheet.Paste((VARIANT &)myrangeptr, COleVariant((short)TRUE));I'm not quite sure if the
Worksheet.Paste
method does what you want. It will copy text from the clipboard to the specified (or current) selection (see Worksheet.Paste Method[^]). Setting the second parameter toTRUE
will not copy the text but place a link. So if you have copied the text from the file to the clipboard, you may use this function but should passFALSE
as second parameter (orCOleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR)
as optional value which uses the default). However, the first (also optional parameter) is a range object. Just declare the object, assign the return value ofGetRange()
and pass it toPaste()
:Range objRange = objSheet.GetRange(COleVariant(TEXT("A1")),COleVariant(TEXT("M34")));
objSheet.Paste(objRange, COleVariant((short)FALSE)); -
I'm not quite sure if the
Worksheet.Paste
method does what you want. It will copy text from the clipboard to the specified (or current) selection (see Worksheet.Paste Method[^]). Setting the second parameter toTRUE
will not copy the text but place a link. So if you have copied the text from the file to the clipboard, you may use this function but should passFALSE
as second parameter (orCOleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR)
as optional value which uses the default). However, the first (also optional parameter) is a range object. Just declare the object, assign the return value ofGetRange()
and pass it toPaste()
:Range objRange = objSheet.GetRange(COleVariant(TEXT("A1")),COleVariant(TEXT("M34")));
objSheet.Paste(objRange, COleVariant((short)FALSE));Hi, I am at home now but from what I remember when I tried to build the code with VISUAL C++ 6.0 I got a build error saying cannot convert paramter 1 from const struct &VARIANT to class Range The documentation says it accepts a range object as the first parm but the prototype for the code is Paste(const &VARIANT,
-
Hi, I am at home now but from what I remember when I tried to build the code with VISUAL C++ 6.0 I got a build error saying cannot convert paramter 1 from const struct &VARIANT to class Range The documentation says it accepts a range object as the first parm but the prototype for the code is Paste(const &VARIANT,
Sorry, I overlooked the parameter type.
WorkSheet.GetRange()
returns aLPDISPATCH
which can be assigned to a Range object. You can use theLPDISPATCH
returned byGetRange()
or access the underlyingIDispatch
pointer of an existingRange
object using theLPDISPATCH()
operator to create the required variant:VARIANT vtRng;
vtRng.vt = VT_DISPATCH;
// Assign LPDISPATCH returned by GetRange()
vtRng.pdispVal = objSheet.GetRange(COleVariant(TEXT("A1")),COleVariant(TEXT("M34")));
// Alternatively, access the IDispatch pointer of an existing Range object
// vtRng.pdispVal = objRange;
objSheet.Paste(vtRng, COleVariant((short)TRUE));