none
Copy & paste embeded object to file system RRS feed

  • Question

  • Cross-post at: http://stackoverflow.com/questions/27685791/copy-paste-embeded-object-to-file-system

    Background:

    I wrote a large piece of VBA code in an Excel workbook to help handle a lot of repeating jobs. I found it is painful to maintain and modify the code by using the "pool" VBA Editor. Then, I decided to move those VBA code to C# solutions, which I suppose I can benefit from the modern editor - Visual Studio.

    Problem:

    Although I don't know too much about VBA, I know even less about C#. Hence I got some problem when I try to "translate" my VBA code to C#, and here is one of them:

    I create a document-level solution for Excel. I need to embed some files (.cab, .exe) into the solution, so when I run a method (e.g. by clicking a button), those files will be copied to the file system, and do some job.

    In VBA, I embedded those files in the Excel workbook, then I can copy the embedded objects to the clipboard by using the following VBA code:

    Sheet1.OLEObjects("obj_cab").COpy

    And then I can paste the object to the file system by using the following VBA code:

    CreateObject("Shell.Application").Namespace("C:\Sample\").Self.InvokeVerb "Paste"

    In the C# solution, I can use the following equivalent method to copy the embedded object to the clipboard:

    Microsoft.Office.Interop.Excel.OLEObject OLEobj = (Microsoft.Office.Interop.Excel.OLEObject)sheet1.OLEObjects("obj_cab");
    OLEobj.Copy();

    Then I stuck here, I don't know how to get this object from clipboard and put it to the file system. The 'CreateObject' method does not exist in C#.

    Anyone can help? Either a better way to embed those files or a working way to paste those objects (files) from clipboard to the file system?

    Thanks a lot!

    Tuesday, December 30, 2014 1:38 AM

Answers

  • Hi FantaC,

    It's possible to save the oleobject to the disk. But it's not very simple. Because when you call the Copy method of OLEObject to clipboard, it's actually not the exact the oleobject file itself, it's actually a wrapper of the file, it means that it has different file headers and some other information from the original oleobject file.

    To save oleobject on the disk, you need to analyze the byte array by yourself, this would be a little complicated. You could check the answer in this thread for the code sample:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/1ab7a178-8021-4c67-a14b-5031a254fd9c/saving-oleobject-content-to-a-file

    The original poster of this thread also write a blogpost about this, you can check it here:

    Saving OLEObject Content To File

    Detailed information and code samples are provided in this blogpost.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 31, 2014 9:16 AM
    Moderator