none
Open /Copy&Paste Word document content from bytes/file streams to Current Active document. RRS feed

  • Question

  • Hello,

    Currently i am saving word document content(including fields,text,pictures etc..) into  a flat file by using filestreams. like this

      var iPersistFile = (System.Runtime.InteropServices.ComTypes.IPersistFile) _WordDocument;
                        string wTempFile = Path.GetTempFileName();
                        iPersistFile.Save(wTempFile, false);

                        byte[] wordDocBytes = File.ReadAllBytes(wTempFile);
                        using (var fWriteStream = new FileStream(filepathName, FileMode.Create, FileAccess.ReadWrite))
                        {
                            fWriteStream.Write(wordDocBytes.ToArray(), 0, wordDocBytes.ToArray().Length);
                            fWriteStream.Close();
                        }

    So , now i am able to open that flat file and copy & paste the content to current word document, but here it's opening as like embedded object i.e its getting a box around the data of word content see below image for reference. how can i open that flat file like as normal word document. I used below code to open the document. Any help would be appreciate. i am using win7, with office 2007. 

            DataObject objData = new DataObject();
                     string[] filename = new string[1];
                     filename[0] = _FilePath;
                     objData.SetData(DataFormats.FileDrop, true, filename);
                     Clipboard.SetDataObject(objData, true);

                     _WordDocument.ActiveWindow.Selection.WholeStory();
                     _WordDocument.ActiveWindow.Selection.Paste();

    Wednesday, February 4, 2015 1:21 PM

Answers

  • Hi,

    As Eugene said, “The Word object model doesn't provide any method for creating files from a stream or bytes array”.

    >> now i am able to open that flat file and copy & paste the content to current word document, but here it's opening as like embedded object

    I think this behavior is expected, since you were drop a document to the active document (just like inserting an object to the document). As a work around, you could open the source document first and then paste it to the target document.

    Here is the code for your reference:

        Application.Documents.Open("_SRC_FILE_PATH")
        
        Application.ActiveDocument.Content.Copy()
        
        Application.ActiveDocument.Close()
        
        Application.ActiveWindow.Selection.Paste()
    
    

    Regards,

    Jeffrey


    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.

    Tuesday, February 10, 2015 11:32 AM
    Moderator

All replies

  • Hello Ravindra,

    The Word object model doesn't provide any method for creating files from a stream or bytes array. You need to create a file on the disk and then open it. Also you may consider using the Open XML sdk instead.

    Wednesday, February 4, 2015 1:33 PM
  • Thanks Eugene,

    Actually we are hosting a word document in AxWebbrowser control, so here we are playing with .doc file, not with .docx file. so here we are following for saving the word content -- file streams , and for opening streams -- word content. i hope Open XML sdk won't work for .doc files. if yes  then if you provide any reference link that would be highly appreciated.

    Thanks. 

    Wednesday, February 4, 2015 1:46 PM
  • You are on the right avenue. The Open XML sdk doesn't make any sense in case of binary doc files. I' suggest using any third party components then (for example, Aspose).
    Wednesday, February 4, 2015 1:49 PM
  • Hi,

    As Eugene said, “The Word object model doesn't provide any method for creating files from a stream or bytes array”.

    >> now i am able to open that flat file and copy & paste the content to current word document, but here it's opening as like embedded object

    I think this behavior is expected, since you were drop a document to the active document (just like inserting an object to the document). As a work around, you could open the source document first and then paste it to the target document.

    Here is the code for your reference:

        Application.Documents.Open("_SRC_FILE_PATH")
        
        Application.ActiveDocument.Content.Copy()
        
        Application.ActiveDocument.Close()
        
        Application.ActiveWindow.Selection.Paste()
    
    

    Regards,

    Jeffrey


    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.

    Tuesday, February 10, 2015 11:32 AM
    Moderator