Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. _WorkSheet::Paste usage

_WorkSheet::Paste usage

Scheduled Pinned Locked Moved COM
c++
4 Posts 2 Posters 14 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    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));

    J 1 Reply Last reply
    0
    • F ForNow

      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));

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      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 to TRUE 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 pass FALSE as second parameter (or COleVariant((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 of GetRange() and pass it to Paste():

      Range objRange = objSheet.GetRange(COleVariant(TEXT("A1")),COleVariant(TEXT("M34")));
      objSheet.Paste(objRange, COleVariant((short)FALSE));

      F 1 Reply Last reply
      0
      • J Jochen Arndt

        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 to TRUE 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 pass FALSE as second parameter (or COleVariant((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 of GetRange() and pass it to Paste():

        Range objRange = objSheet.GetRange(COleVariant(TEXT("A1")),COleVariant(TEXT("M34")));
        objSheet.Paste(objRange, COleVariant((short)FALSE));

        F Offline
        F Offline
        ForNow
        wrote on last edited by
        #3

        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,

        J 1 Reply Last reply
        0
        • F ForNow

          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,

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          Sorry, I overlooked the parameter type. WorkSheet.GetRange() returns a LPDISPATCH which can be assigned to a Range object. You can use the LPDISPATCH returned by GetRange() or access the underlying IDispatch pointer of an existing Range object using the LPDISPATCH() 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));

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups