locked
Embedded file on clipboard RRS feed

  • Question

  • Hi

    I would like to place some RichText on the clipboard which among other things contains an embedded file. That is when the content is pasted in e.g. Word, the file is embedded and the user can open it by double-clicking. How can I do this?

    Thanks in advance
    /Andreas
    Thursday, October 4, 2007 11:10 AM

Answers

  • Embedding a simple text file generates this RTF on the Clipboard:

          object data = Clipboard.GetData("Rich Text Format");
          Console.WriteLine(data.ToString());

    output:

    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
    \uc1\pard\f0\fs20{\object\objemb{\*\objclass Package}\objw750\objh765{\*\objdata
    01050000
    02000000
    08000000
    5061636b61676500
    00000000
    00000000
    190c0000
    0200546573742e74787400633a5c74656d705c746573742e747874000000030011000000633a5c
    74656d705c746573742e74787400de0b00002d090970312e4261736554797065097b4e616d6520
    3d20224172726179222046756c6c4e616d65203d202253797374656d2e4172726179227d095379
    7374656d2e54797065207b53797374656d2e52756e74696d65547970657d0d0a2b09095b537973
    74656d2e52756e74696d65547970655d097b4e616d65203d20224172726179222046756c6c4e61
    6d65203d202253797374656d2e4172726179227d0953797374656d2e52756e74696d6554797065
    0d0a2b090962617365097b4e616d65203d20224172726179222046756c6c4e616d65203d202253
    797374656d2e4172726179227d0953797374656d2e5265666c656374696f6e2e4d656d62657249
    6e666f207b53797374656d2e52756e74696d65547970657d0d0a2b0909417373656d626c79097b
    6d73636f726c69622c2056657273696f6e3d322e302e302e302c2043756c747572653d6e657574
    72616c2c205075626c69634b6579546f6b656e3d623737613563353631393334653038397d0953
    797374656d2e5265666c656374696f6e2e417373656d626c790d0a0909417373656d626c795175
    616c69666965644e616d65092253797374656d2e41727261792c206d73636f726c69622c205665
    7273696f6e3d322e302e302e302c2043756c747572653d6e65757472616c2c205075626c69634b
    6579546f6b656e3d623737613563353631393334653038392209737472696e670d0a0909417474
    72696275746573095075626c6963207c204162737472616374207c2053657269616c697a61626c
    65207c204265666f72654669656c64496e69740953797374656d2e5265666c656374696f6e2e54
    797065417474726962757465730d0a2b09094261736554797065097b4e616d65203d20224f626a
    656374222046756c6c4e616d65203d202253797374656d2e4f626a656374227d0953797374656d
    2e54797065207b53797374656d2e52756e74696d65547970657d0d0a0909436f6e7461696e7347
    656e65726963506172616d65746572730966616c736509626f6f6c0d0a2b09094465636c617269
    6e674d6574686f64092770312e42617365547970652e4465636c6172696e674d6574686f642720
    746872657720616e20657863657074696f6e206f662074797065202753797374656d2e496e7661
    6c69644f7065726174696f6e457863657074696f6e270953797374656d2e5265666c656374696f
    6e2e4d6574686f6442617365207b53797374656d2e496e76616c69644f7065726174696f6e4578
    63657074696f6e7d0d0a2b09094465636c6172696e6754797065096e756c6c0953797374656d2e
    547970650d0a090946756c6c4e616d65092253797374656d2e41727261792209737472696e670d
    0a2b090947656e65726963506172616d6574657241747472696275746573092770312e42617365
    547970652e47656e65726963506172616d65746572417474726962757465732720746872657720
    616e20657863657074696f6e206f662074797065202753797374656d2e496e76616c69644f7065
    726174696f6e457863657074696f6e270953797374656d2e5265666c656374696f6e2e47656e65
    726963506172616d6574657241747472696275746573207b53797374656d2e496e76616c69644f
    7065726174696f6e457863657074696f6e7d0d0a2b090947656e65726963506172616d65746572
    506f736974696f6e092770312e42617365547970652e47656e65726963506172616d6574657250
    6f736974696f6e2720746872657720616e20657863657074696f6e206f66207479706520275379
    7374656d2e496e76616c69644f7065726174696f6e457863657074696f6e2709696e74207b5379
    7374656d2e496e76616c69644f7065726174696f6e457863657074696f6e7d0d0a2b0909475549
    44097b32303066623931632d383135642d333965302d396530372d306531626462326564343762
    7d0953797374656d2e477569640d0a0909486173456c656d656e74547970650966616c73650962
    6f6f6c0d0a090948617350726f78794174747269627574650966616c736509626f6f6c0d0a0909
    49734162737472616374097472756509626f6f6c0d0a09094973416e7369436c61737309747275
    6509626f6f6c0d0a0909497341727261790966616c736509626f6f6c0d0a090949734175746f43
    6c6173730966616c736509626f6f6c0d0a090949734175746f4c61796f7574097472756509626f
    6f6c0d0a0909497342795265660966616c736509626f6f6c0d0a09094973436c61737309747275
    6509626f6f6c0d0a09094973434f4d4f626a6563740966616c736509626f6f6c0d0a0909497343
    6f6e7465787466756c0966616c736509626f6f6c0d0a09094973456e756d0966616c736509626f
    6f6c0d0a090949734578706c696369744c61796f75740966616c736509626f6f6c0d0a09094973
    47656e65726963506172616d657465720966616c736509626f6f6c0d0a0909497347656e657269
    63547970650966616c736509626f6f6c0d0a0909497347656e6572696354797065446566696e69
    74696f6e0966616c736509626f6f6c0d0a09094973496d706f72740966616c736509626f6f6c0d
    0a09094973496e746572666163650966616c736509626f6f6c0d0a090949734c61796f75745365
    7175656e7469616c0966616c736509626f6f6c0d0a090949734d61727368616c42795265660966
    616c736509626f6f6c0d0a090949734e65737465640966616c736509626f6f6c0d0a090949734e
    6573746564417373656d626c790966616c736509626f6f6c0d0a090949734e657374656446616d
    414e44417373656d0966616c736509626f6f6c0d0a090949734e657374656446616d696c790966
    616c736509626f6f6c0d0a090949734e657374656446616d4f52417373656d0966616c73650962
    6f6f6c0d0a090949734e6573746564507269766174650966616c736509626f6f6c0d0a09094973
    4e65737465645075626c69630966616c736509626f6f6c0d0a090949734e6f745075626c696309
    66616c736509626f6f6c0d0a09094973506f696e7465720966616c736509626f6f6c0d0a090949
    735072696d69746976650966616c736509626f6f6c0d0a090949735075626c6963097472756509
    626f6f6c0d0a090949735365616c65640966616c736509626f6f6c0d0a0909497353657269616c
    697a61626c65097472756509626f6f6c0d0a090949735370656369616c4e616d650966616c7365
    09626f6f6c0d0a09094973537a41727261790966616c736509626f6f6c0d0a09094973556e6963
    6f6465436c6173730966616c736509626f6f6c0d0a0909497356616c7565547970650966616c73
    6509626f6f6c0d0a0909497356697369626c65097472756509626f6f6c0d0a09094d656d626572
    547970650954797065496e666f0953797374656d2e5265666c656374696f6e2e4d656d62657254
    797065730d0a2b09094d6f64756c65097b436f6d6d6f6e4c616e677561676552756e74696d654c
    6962726172797d0953797374656d2e5265666c656374696f6e2e4d6f64756c650d0a09094e616d
    657370616365092253797374656d2209737472696e670d0a2b09095265666c6563746564547970
    65096e756c6c0953797374656d2e547970650d0a2b09095374727563744c61796f757441747472
    6962757465097b53797374656d2e52756e74696d652e496e7465726f7053657276696365732e53
    74727563744c61796f75744174747269627574657d0953797374656d2e52756e74696d652e496e
    7465726f7053657276696365732e5374727563744c61796f75744174747269627574650d0a2b09
    095479706548616e646c65097b53797374656d2e52756e74696d655479706548616e646c657d09
    53797374656d2e52756e74696d655479706548616e646c650d0a2b090954797065496e69746961
    6c697a6572096e756c6c0953797374656d2e5265666c656374696f6e2e436f6e7374727563746f
    72496e666f0d0a2b0909556e6465726c79696e6753797374656d54797065097b4e616d65203d20
    224172726179222046756c6c4e616d65203d202253797374656d2e4172726179227d0953797374
    656d2e54797065207b53797374656d2e52756e74696d65547970657d0d0a2b0909537461746963
    206d656d6265727309090d0a0d0a0000
    01050000
    05000000
    0d000000
    4d45544146494c455049435400
    2b050000
    bbfaffff
    f0000000
    08002b0545050000
    0100090000037400000002001c0000000000050000000b0200000000050000000c02320031001c
    000000fb02f5ff000000000000900100000001000000005461686f6d610000ce0e0a7400fc0700
    b8a4f377c1a4f3772030f577390f665e040000002d010000050000000902000000000500000001
    02ffffff0005000000020101000000050000002e01060000000a00000021050800546573742e74
    7874210019001c000000fb021000070000000000bc02000000000102022253797374656d000039
    0f665e00000a0026008a0100000000ffffffff8cfc0700040000002d010100030000000000
    }{\result{\pict\wmetafile8\picw750\pich765\picwgoal750\pichgoal765
    0100090000037400000002001c0000000000050000000b0200000000050000000c02320031001c
    000000fb02f5ff000000000000900100000001000000005461686f6d610000ce0e0a7400fc0700
    b8a4f377c1a4f3772030f577390f665e040000002d010000050000000902000000000500000001
    02ffffff0005000000020101000000050000002e01060000000a00000021050800546573742e74
    7874210019001c000000fb021000070000000000bc02000000000102022253797374656d000039
    0f665e00000a0026008a0100000000ffffffff8cfc0700040000002d010100030000000000
    }}}}

    That doesn't look encouraging.  The .NET framework has no support whatsoever for OLE embedding, it looks like a deprecated technology to me.  Earlier versions of Windows had the OLE packager applet but that's gonzo.  My best guess is that you'll have to P/Invoke some Ole* method(s).
    Thursday, October 4, 2007 1:22 PM

All replies

  • I don't know what an "embedded file" is.  Do it the other way around, create whatever you need in Word first and copy it onto the Clipboard.  Then in .NET, inspect the Clipboard to see how you should format the clipboard data so Word is happy.
    Thursday, October 4, 2007 12:01 PM
  • If for instance in WordPad I select "Insert->Object->Create From File" and select maybe a .txt file then that file is shown in the .rtf document as an icon I can click to open the embedded text file. The contents of the .rtf file can be inserted directly on the clipboard with:

     

    DataObject mData = new DataObject();

    mData.SetData(DataFormats.Rtf, *insert rtf data here*);

    Clipboard.SetDataObject(mData, true);

     

    and when Ctrl+V is selected in Word it works. However, I want to do this for various text files so I need to generate the Rtf data programmatically, but embedding a file generates binary data which I can't decipher and I haven't been able to find a description of how to build the Rtf data. Also I haven't been able to find any .NET methods for this. The RichTextBox GUI component can be used to generate RichText but it only seems to work for text and not embedded images or objects.

     

    /Andreas

    Thursday, October 4, 2007 12:13 PM
  • Embedding a simple text file generates this RTF on the Clipboard:

          object data = Clipboard.GetData("Rich Text Format");
          Console.WriteLine(data.ToString());

    output:

    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
    \uc1\pard\f0\fs20{\object\objemb{\*\objclass Package}\objw750\objh765{\*\objdata
    01050000
    02000000
    08000000
    5061636b61676500
    00000000
    00000000
    190c0000
    0200546573742e74787400633a5c74656d705c746573742e747874000000030011000000633a5c
    74656d705c746573742e74787400de0b00002d090970312e4261736554797065097b4e616d6520
    3d20224172726179222046756c6c4e616d65203d202253797374656d2e4172726179227d095379
    7374656d2e54797065207b53797374656d2e52756e74696d65547970657d0d0a2b09095b537973
    74656d2e52756e74696d65547970655d097b4e616d65203d20224172726179222046756c6c4e61
    6d65203d202253797374656d2e4172726179227d0953797374656d2e52756e74696d6554797065
    0d0a2b090962617365097b4e616d65203d20224172726179222046756c6c4e616d65203d202253
    797374656d2e4172726179227d0953797374656d2e5265666c656374696f6e2e4d656d62657249
    6e666f207b53797374656d2e52756e74696d65547970657d0d0a2b0909417373656d626c79097b
    6d73636f726c69622c2056657273696f6e3d322e302e302e302c2043756c747572653d6e657574
    72616c2c205075626c69634b6579546f6b656e3d623737613563353631393334653038397d0953
    797374656d2e5265666c656374696f6e2e417373656d626c790d0a0909417373656d626c795175
    616c69666965644e616d65092253797374656d2e41727261792c206d73636f726c69622c205665
    7273696f6e3d322e302e302e302c2043756c747572653d6e65757472616c2c205075626c69634b
    6579546f6b656e3d623737613563353631393334653038392209737472696e670d0a0909417474
    72696275746573095075626c6963207c204162737472616374207c2053657269616c697a61626c
    65207c204265666f72654669656c64496e69740953797374656d2e5265666c656374696f6e2e54
    797065417474726962757465730d0a2b09094261736554797065097b4e616d65203d20224f626a
    656374222046756c6c4e616d65203d202253797374656d2e4f626a656374227d0953797374656d
    2e54797065207b53797374656d2e52756e74696d65547970657d0d0a0909436f6e7461696e7347
    656e65726963506172616d65746572730966616c736509626f6f6c0d0a2b09094465636c617269
    6e674d6574686f64092770312e42617365547970652e4465636c6172696e674d6574686f642720
    746872657720616e20657863657074696f6e206f662074797065202753797374656d2e496e7661
    6c69644f7065726174696f6e457863657074696f6e270953797374656d2e5265666c656374696f
    6e2e4d6574686f6442617365207b53797374656d2e496e76616c69644f7065726174696f6e4578
    63657074696f6e7d0d0a2b09094465636c6172696e6754797065096e756c6c0953797374656d2e
    547970650d0a090946756c6c4e616d65092253797374656d2e41727261792209737472696e670d
    0a2b090947656e65726963506172616d6574657241747472696275746573092770312e42617365
    547970652e47656e65726963506172616d65746572417474726962757465732720746872657720
    616e20657863657074696f6e206f662074797065202753797374656d2e496e76616c69644f7065
    726174696f6e457863657074696f6e270953797374656d2e5265666c656374696f6e2e47656e65
    726963506172616d6574657241747472696275746573207b53797374656d2e496e76616c69644f
    7065726174696f6e457863657074696f6e7d0d0a2b090947656e65726963506172616d65746572
    506f736974696f6e092770312e42617365547970652e47656e65726963506172616d6574657250
    6f736974696f6e2720746872657720616e20657863657074696f6e206f66207479706520275379
    7374656d2e496e76616c69644f7065726174696f6e457863657074696f6e2709696e74207b5379
    7374656d2e496e76616c69644f7065726174696f6e457863657074696f6e7d0d0a2b0909475549
    44097b32303066623931632d383135642d333965302d396530372d306531626462326564343762
    7d0953797374656d2e477569640d0a0909486173456c656d656e74547970650966616c73650962
    6f6f6c0d0a090948617350726f78794174747269627574650966616c736509626f6f6c0d0a0909
    49734162737472616374097472756509626f6f6c0d0a09094973416e7369436c61737309747275
    6509626f6f6c0d0a0909497341727261790966616c736509626f6f6c0d0a090949734175746f43
    6c6173730966616c736509626f6f6c0d0a090949734175746f4c61796f7574097472756509626f
    6f6c0d0a0909497342795265660966616c736509626f6f6c0d0a09094973436c61737309747275
    6509626f6f6c0d0a09094973434f4d4f626a6563740966616c736509626f6f6c0d0a0909497343
    6f6e7465787466756c0966616c736509626f6f6c0d0a09094973456e756d0966616c736509626f
    6f6c0d0a090949734578706c696369744c61796f75740966616c736509626f6f6c0d0a09094973
    47656e65726963506172616d657465720966616c736509626f6f6c0d0a0909497347656e657269
    63547970650966616c736509626f6f6c0d0a0909497347656e6572696354797065446566696e69
    74696f6e0966616c736509626f6f6c0d0a09094973496d706f72740966616c736509626f6f6c0d
    0a09094973496e746572666163650966616c736509626f6f6c0d0a090949734c61796f75745365
    7175656e7469616c0966616c736509626f6f6c0d0a090949734d61727368616c42795265660966
    616c736509626f6f6c0d0a090949734e65737465640966616c736509626f6f6c0d0a090949734e
    6573746564417373656d626c790966616c736509626f6f6c0d0a090949734e657374656446616d
    414e44417373656d0966616c736509626f6f6c0d0a090949734e657374656446616d696c790966
    616c736509626f6f6c0d0a090949734e657374656446616d4f52417373656d0966616c73650962
    6f6f6c0d0a090949734e6573746564507269766174650966616c736509626f6f6c0d0a09094973
    4e65737465645075626c69630966616c736509626f6f6c0d0a090949734e6f745075626c696309
    66616c736509626f6f6c0d0a09094973506f696e7465720966616c736509626f6f6c0d0a090949
    735072696d69746976650966616c736509626f6f6c0d0a090949735075626c6963097472756509
    626f6f6c0d0a090949735365616c65640966616c736509626f6f6c0d0a0909497353657269616c
    697a61626c65097472756509626f6f6c0d0a090949735370656369616c4e616d650966616c7365
    09626f6f6c0d0a09094973537a41727261790966616c736509626f6f6c0d0a09094973556e6963
    6f6465436c6173730966616c736509626f6f6c0d0a0909497356616c7565547970650966616c73
    6509626f6f6c0d0a0909497356697369626c65097472756509626f6f6c0d0a09094d656d626572
    547970650954797065496e666f0953797374656d2e5265666c656374696f6e2e4d656d62657254
    797065730d0a2b09094d6f64756c65097b436f6d6d6f6e4c616e677561676552756e74696d654c
    6962726172797d0953797374656d2e5265666c656374696f6e2e4d6f64756c650d0a09094e616d
    657370616365092253797374656d2209737472696e670d0a2b09095265666c6563746564547970
    65096e756c6c0953797374656d2e547970650d0a2b09095374727563744c61796f757441747472
    6962757465097b53797374656d2e52756e74696d652e496e7465726f7053657276696365732e53
    74727563744c61796f75744174747269627574657d0953797374656d2e52756e74696d652e496e
    7465726f7053657276696365732e5374727563744c61796f75744174747269627574650d0a2b09
    095479706548616e646c65097b53797374656d2e52756e74696d655479706548616e646c657d09
    53797374656d2e52756e74696d655479706548616e646c650d0a2b090954797065496e69746961
    6c697a6572096e756c6c0953797374656d2e5265666c656374696f6e2e436f6e7374727563746f
    72496e666f0d0a2b0909556e6465726c79696e6753797374656d54797065097b4e616d65203d20
    224172726179222046756c6c4e616d65203d202253797374656d2e4172726179227d0953797374
    656d2e54797065207b53797374656d2e52756e74696d65547970657d0d0a2b0909537461746963
    206d656d6265727309090d0a0d0a0000
    01050000
    05000000
    0d000000
    4d45544146494c455049435400
    2b050000
    bbfaffff
    f0000000
    08002b0545050000
    0100090000037400000002001c0000000000050000000b0200000000050000000c02320031001c
    000000fb02f5ff000000000000900100000001000000005461686f6d610000ce0e0a7400fc0700
    b8a4f377c1a4f3772030f577390f665e040000002d010000050000000902000000000500000001
    02ffffff0005000000020101000000050000002e01060000000a00000021050800546573742e74
    7874210019001c000000fb021000070000000000bc02000000000102022253797374656d000039
    0f665e00000a0026008a0100000000ffffffff8cfc0700040000002d010100030000000000
    }{\result{\pict\wmetafile8\picw750\pich765\picwgoal750\pichgoal765
    0100090000037400000002001c0000000000050000000b0200000000050000000c02320031001c
    000000fb02f5ff000000000000900100000001000000005461686f6d610000ce0e0a7400fc0700
    b8a4f377c1a4f3772030f577390f665e040000002d010000050000000902000000000500000001
    02ffffff0005000000020101000000050000002e01060000000a00000021050800546573742e74
    7874210019001c000000fb021000070000000000bc02000000000102022253797374656d000039
    0f665e00000a0026008a0100000000ffffffff8cfc0700040000002d010100030000000000
    }}}}

    That doesn't look encouraging.  The .NET framework has no support whatsoever for OLE embedding, it looks like a deprecated technology to me.  Earlier versions of Windows had the OLE packager applet but that's gonzo.  My best guess is that you'll have to P/Invoke some Ole* method(s).
    Thursday, October 4, 2007 1:22 PM
  • Ok, I tried to do that but it turned out to be rather complicated. Instead I found this page where someone has already implemented it http://www.codeproject.com/cs/miscctrl/MyExtRichTextBox.asp. The author says it only works for images but on my system it works fine for any type of file. It's a lot of code for a simple task though.

     

    Thanks for your help.

    /Andreas

    Friday, October 5, 2007 6:31 AM
  • I wasted a lot of time looking at VB's 'setclipboard', OLE, and

    related APIs and finally came to a completely different solution.

     

    My actual angle, (perhaps I read your initial post incorrectly,)

    was to add an OLE embedded object into a richtextbox.

     

    I think my solution can be changed around to meet your needs.

    (This took a while for me to figure out!)

     

    Imports System.IO

    ' Project, add reference, browse to c:\windows\system32

    ' and add shell32.dll

    Imports Shell32

     

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles Button1.Click

    Dim ofd As New OpenFileDialog

    ofd.Multiselect = False

    ofd.InitialDirectory = "c:\"

    If ofd.ShowDialog <> Windows.Forms.DialogResult.OK Then

    Exit Sub

    End If

    Dim Sh As New Shell

    Dim SF As Folder = Sh.NameSpace(Path.GetDirectoryName(ofd.FileName))

    For Each F As FolderItem In SF.Items

    If F.Name = Path.GetFileName(ofd.FileName) Then

    F.InvokeVerb("Copy")

    End If

    Next

    RichTextBox1.Paste()

    End Sub

    End Class

     

    Sunday, October 7, 2007 6:35 AM