Convert Word of interop object to byte [] without saving physically





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















I have an object created in memory using Microsoft.Office.Interop and Microsoft.Office.Word and with all created, paragraphs, tables and the like. I need this object to generate a content byte to feed one field of the same type in a table.
The problem that I can not save it in any way physically with a oDoc.Save ("path") in order to use a FileStream and solve my problem.



Have tried several solutions and how to use the clipboard, and did not work. Any solution?










share|improve this question




















  • 1





    What error are you getting? Can you show some code?

    – keyboardP
    May 16 '11 at 20:53











  • I wonder if saving to a named pipe might work... But - writing to the TEMP are is a lot easier...

    – Marc Gravell
    May 16 '11 at 21:32











  • Marc, i dont writing in TEMP because dont have any permission to write in FS.

    – Iceknight
    May 17 '11 at 17:59











  • keyboardP, the code : object oMissing = System.Reflection.Missing.Value; Word.Application oWord; Word.Document oDoc; oWord = new Word.Application(); oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Range.Text = "The title; oPara1.Range.InsertParagraphAfter(); byte content = new byte[oDoc.Content.Text.Length]; System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); content = encoding.GetBytes(oDoc.Content.Text); This return the text w/o formt

    – Iceknight
    May 17 '11 at 18:02


















3















I have an object created in memory using Microsoft.Office.Interop and Microsoft.Office.Word and with all created, paragraphs, tables and the like. I need this object to generate a content byte to feed one field of the same type in a table.
The problem that I can not save it in any way physically with a oDoc.Save ("path") in order to use a FileStream and solve my problem.



Have tried several solutions and how to use the clipboard, and did not work. Any solution?










share|improve this question




















  • 1





    What error are you getting? Can you show some code?

    – keyboardP
    May 16 '11 at 20:53











  • I wonder if saving to a named pipe might work... But - writing to the TEMP are is a lot easier...

    – Marc Gravell
    May 16 '11 at 21:32











  • Marc, i dont writing in TEMP because dont have any permission to write in FS.

    – Iceknight
    May 17 '11 at 17:59











  • keyboardP, the code : object oMissing = System.Reflection.Missing.Value; Word.Application oWord; Word.Document oDoc; oWord = new Word.Application(); oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Range.Text = "The title; oPara1.Range.InsertParagraphAfter(); byte content = new byte[oDoc.Content.Text.Length]; System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); content = encoding.GetBytes(oDoc.Content.Text); This return the text w/o formt

    – Iceknight
    May 17 '11 at 18:02














3












3








3


1






I have an object created in memory using Microsoft.Office.Interop and Microsoft.Office.Word and with all created, paragraphs, tables and the like. I need this object to generate a content byte to feed one field of the same type in a table.
The problem that I can not save it in any way physically with a oDoc.Save ("path") in order to use a FileStream and solve my problem.



Have tried several solutions and how to use the clipboard, and did not work. Any solution?










share|improve this question
















I have an object created in memory using Microsoft.Office.Interop and Microsoft.Office.Word and with all created, paragraphs, tables and the like. I need this object to generate a content byte to feed one field of the same type in a table.
The problem that I can not save it in any way physically with a oDoc.Save ("path") in order to use a FileStream and solve my problem.



Have tried several solutions and how to use the clipboard, and did not work. Any solution?







c# interop ms-word byte






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 7 '12 at 7:51









Berkay Turancı

2,69432238




2,69432238










asked May 16 '11 at 20:51









IceknightIceknight

6826




6826








  • 1





    What error are you getting? Can you show some code?

    – keyboardP
    May 16 '11 at 20:53











  • I wonder if saving to a named pipe might work... But - writing to the TEMP are is a lot easier...

    – Marc Gravell
    May 16 '11 at 21:32











  • Marc, i dont writing in TEMP because dont have any permission to write in FS.

    – Iceknight
    May 17 '11 at 17:59











  • keyboardP, the code : object oMissing = System.Reflection.Missing.Value; Word.Application oWord; Word.Document oDoc; oWord = new Word.Application(); oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Range.Text = "The title; oPara1.Range.InsertParagraphAfter(); byte content = new byte[oDoc.Content.Text.Length]; System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); content = encoding.GetBytes(oDoc.Content.Text); This return the text w/o formt

    – Iceknight
    May 17 '11 at 18:02














  • 1





    What error are you getting? Can you show some code?

    – keyboardP
    May 16 '11 at 20:53











  • I wonder if saving to a named pipe might work... But - writing to the TEMP are is a lot easier...

    – Marc Gravell
    May 16 '11 at 21:32











  • Marc, i dont writing in TEMP because dont have any permission to write in FS.

    – Iceknight
    May 17 '11 at 17:59











  • keyboardP, the code : object oMissing = System.Reflection.Missing.Value; Word.Application oWord; Word.Document oDoc; oWord = new Word.Application(); oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Range.Text = "The title; oPara1.Range.InsertParagraphAfter(); byte content = new byte[oDoc.Content.Text.Length]; System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); content = encoding.GetBytes(oDoc.Content.Text); This return the text w/o formt

    – Iceknight
    May 17 '11 at 18:02








1




1





What error are you getting? Can you show some code?

– keyboardP
May 16 '11 at 20:53





What error are you getting? Can you show some code?

– keyboardP
May 16 '11 at 20:53













I wonder if saving to a named pipe might work... But - writing to the TEMP are is a lot easier...

– Marc Gravell
May 16 '11 at 21:32





I wonder if saving to a named pipe might work... But - writing to the TEMP are is a lot easier...

– Marc Gravell
May 16 '11 at 21:32













Marc, i dont writing in TEMP because dont have any permission to write in FS.

– Iceknight
May 17 '11 at 17:59





Marc, i dont writing in TEMP because dont have any permission to write in FS.

– Iceknight
May 17 '11 at 17:59













keyboardP, the code : object oMissing = System.Reflection.Missing.Value; Word.Application oWord; Word.Document oDoc; oWord = new Word.Application(); oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Range.Text = "The title; oPara1.Range.InsertParagraphAfter(); byte content = new byte[oDoc.Content.Text.Length]; System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); content = encoding.GetBytes(oDoc.Content.Text); This return the text w/o formt

– Iceknight
May 17 '11 at 18:02





keyboardP, the code : object oMissing = System.Reflection.Missing.Value; Word.Application oWord; Word.Document oDoc; oWord = new Word.Application(); oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Range.Text = "The title; oPara1.Range.InsertParagraphAfter(); byte content = new byte[oDoc.Content.Text.Length]; System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); content = encoding.GetBytes(oDoc.Content.Text); This return the text w/o formt

– Iceknight
May 17 '11 at 18:02












2 Answers
2






active

oldest

votes


















2














Do you really have to use the Microsoft.Office.Interop and Microsoft.Office.Word?



If it is not really necessary, you could use the OpenXML SDK libraries for manipulating the content of the WordDocument.



OpenXML SDK contains a class WordprocessingDocument that can manipulate a memory stream containing a WordDocument content. And MemoryStream can be converted using ToArray() to a byte.



As a code sample:



byte templateContent = File.ReadAllBytes(templateFile);

MemoryStream stream = new MemoryStream();
stream.Write(templateContent, 0, templateContent.Length);

WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true);

// When done
byte contentOfWordFile = stream.toArray();





share|improve this answer


























  • You actually don't even need the File.ReadAllBytes(), etc. part, because WordprocessingDocument will accept the file path, directly, instead of requiring the stream to be created outside, like that. You would do using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filePath, true)) { using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream())) { string docText = sr.ReadToEnd(); } XmlDocument xml = new XmlDocument(); xml.LoadXml(docText); // do stuff with XmlNodes… And then you would have docText = xml.OuterXml; that could then be converted to bytes.

    – vapcguy
    Nov 15 '18 at 3:03











  • The only problem with my method is you are then encoding a string of XML into bytes for the database: byte bytes = Encoding.ASCII.GetBytes(docText); , so you have to remember that if you want to reconstitute the file back to a docx you'd need to convert the bytes back to a string, then need using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))) { sw.Write(docText); }

    – vapcguy
    Nov 15 '18 at 3:13













  • Talks all about it: docs.microsoft.com/en-us/office/open-xml/…

    – vapcguy
    Nov 15 '18 at 3:15











  • Well, I also found that encoding the bytes is bad and doesn't work... it corrupts the file if you write it back out. I had to use File.ReadAllBytes() to read it, then File.WriteAllBytes() for it to be accurate. You can write the string of XML like I described, but converting bytes back to string doesn't work, because the encoding (whether you use UTF8, Unicode, Default, UTF7, ASCII, doesn't matter) will corrupt it.

    – vapcguy
    Nov 19 '18 at 19:16











  • Converting it to byte from a stream with .ToArray() also works, and using using (MemoryStream stream = new MemoryStream()) { stream.Write(byteArray, 0, (int)byteArray.Length); // Save the file with the new name File.WriteAllBytes(strDocumentPath, stream.ToArray()); } to write it back out also is a way to convert it back and forth successfully.

    – vapcguy
    Nov 26 '18 at 15:47





















0














Sounds like this is a dynamically-created Word document.



Since you have the document in the form of a Document object, you should be able to get its string of XML, then bytes, by doing this:



Microsoft.Office.Interop.Word.Document d = new Microsoft.Office.Interop.Word.Document();

// All of your building of the document was here
// The object must be updated with content

string docText = d.WordOpenXML; // this assumes content is here
byte bytes = Encoding.UTF8.GetBytes(docText);


I don't think that saving the object to the file system first is required, since you already have the object you have built all dynamically, in memory. It should just be a matter of accessing its WordOpenXML.



If you were grabbing the file from the file system, it would look pretty much the same, except for how the document is opened first:



string sourceFilePath = @"C:test.docx";
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
var document = wordApp.Documents.Open(sourceFilePath);
string docText = document.WordOpenXML;
byte bytes = Encoding.UTF8.GetBytes(docText);


If you ever want to download these bytes back into a document, you'd need to do this:



string documentPath = @"C:test.docx"; // can be modified with dynamic paths, file name from database, etc.
byte contentBytes = null;
// … Fill contentBytes from the database, then...

// Create the Word document using the path
using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(documentPath, true))
{
// This should get you the XML string...
string docText = System.Text.Encoding.UTF8.GetString(contentBytes);

// Then we write it out...
using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
}


See How can I form a Word document using stream of bytes for more information.






share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6023272%2fconvert-word-of-interop-object-to-byte-without-saving-physically%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Do you really have to use the Microsoft.Office.Interop and Microsoft.Office.Word?



    If it is not really necessary, you could use the OpenXML SDK libraries for manipulating the content of the WordDocument.



    OpenXML SDK contains a class WordprocessingDocument that can manipulate a memory stream containing a WordDocument content. And MemoryStream can be converted using ToArray() to a byte.



    As a code sample:



    byte templateContent = File.ReadAllBytes(templateFile);

    MemoryStream stream = new MemoryStream();
    stream.Write(templateContent, 0, templateContent.Length);

    WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true);

    // When done
    byte contentOfWordFile = stream.toArray();





    share|improve this answer


























    • You actually don't even need the File.ReadAllBytes(), etc. part, because WordprocessingDocument will accept the file path, directly, instead of requiring the stream to be created outside, like that. You would do using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filePath, true)) { using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream())) { string docText = sr.ReadToEnd(); } XmlDocument xml = new XmlDocument(); xml.LoadXml(docText); // do stuff with XmlNodes… And then you would have docText = xml.OuterXml; that could then be converted to bytes.

      – vapcguy
      Nov 15 '18 at 3:03











    • The only problem with my method is you are then encoding a string of XML into bytes for the database: byte bytes = Encoding.ASCII.GetBytes(docText); , so you have to remember that if you want to reconstitute the file back to a docx you'd need to convert the bytes back to a string, then need using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))) { sw.Write(docText); }

      – vapcguy
      Nov 15 '18 at 3:13













    • Talks all about it: docs.microsoft.com/en-us/office/open-xml/…

      – vapcguy
      Nov 15 '18 at 3:15











    • Well, I also found that encoding the bytes is bad and doesn't work... it corrupts the file if you write it back out. I had to use File.ReadAllBytes() to read it, then File.WriteAllBytes() for it to be accurate. You can write the string of XML like I described, but converting bytes back to string doesn't work, because the encoding (whether you use UTF8, Unicode, Default, UTF7, ASCII, doesn't matter) will corrupt it.

      – vapcguy
      Nov 19 '18 at 19:16











    • Converting it to byte from a stream with .ToArray() also works, and using using (MemoryStream stream = new MemoryStream()) { stream.Write(byteArray, 0, (int)byteArray.Length); // Save the file with the new name File.WriteAllBytes(strDocumentPath, stream.ToArray()); } to write it back out also is a way to convert it back and forth successfully.

      – vapcguy
      Nov 26 '18 at 15:47


















    2














    Do you really have to use the Microsoft.Office.Interop and Microsoft.Office.Word?



    If it is not really necessary, you could use the OpenXML SDK libraries for manipulating the content of the WordDocument.



    OpenXML SDK contains a class WordprocessingDocument that can manipulate a memory stream containing a WordDocument content. And MemoryStream can be converted using ToArray() to a byte.



    As a code sample:



    byte templateContent = File.ReadAllBytes(templateFile);

    MemoryStream stream = new MemoryStream();
    stream.Write(templateContent, 0, templateContent.Length);

    WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true);

    // When done
    byte contentOfWordFile = stream.toArray();





    share|improve this answer


























    • You actually don't even need the File.ReadAllBytes(), etc. part, because WordprocessingDocument will accept the file path, directly, instead of requiring the stream to be created outside, like that. You would do using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filePath, true)) { using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream())) { string docText = sr.ReadToEnd(); } XmlDocument xml = new XmlDocument(); xml.LoadXml(docText); // do stuff with XmlNodes… And then you would have docText = xml.OuterXml; that could then be converted to bytes.

      – vapcguy
      Nov 15 '18 at 3:03











    • The only problem with my method is you are then encoding a string of XML into bytes for the database: byte bytes = Encoding.ASCII.GetBytes(docText); , so you have to remember that if you want to reconstitute the file back to a docx you'd need to convert the bytes back to a string, then need using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))) { sw.Write(docText); }

      – vapcguy
      Nov 15 '18 at 3:13













    • Talks all about it: docs.microsoft.com/en-us/office/open-xml/…

      – vapcguy
      Nov 15 '18 at 3:15











    • Well, I also found that encoding the bytes is bad and doesn't work... it corrupts the file if you write it back out. I had to use File.ReadAllBytes() to read it, then File.WriteAllBytes() for it to be accurate. You can write the string of XML like I described, but converting bytes back to string doesn't work, because the encoding (whether you use UTF8, Unicode, Default, UTF7, ASCII, doesn't matter) will corrupt it.

      – vapcguy
      Nov 19 '18 at 19:16











    • Converting it to byte from a stream with .ToArray() also works, and using using (MemoryStream stream = new MemoryStream()) { stream.Write(byteArray, 0, (int)byteArray.Length); // Save the file with the new name File.WriteAllBytes(strDocumentPath, stream.ToArray()); } to write it back out also is a way to convert it back and forth successfully.

      – vapcguy
      Nov 26 '18 at 15:47
















    2












    2








    2







    Do you really have to use the Microsoft.Office.Interop and Microsoft.Office.Word?



    If it is not really necessary, you could use the OpenXML SDK libraries for manipulating the content of the WordDocument.



    OpenXML SDK contains a class WordprocessingDocument that can manipulate a memory stream containing a WordDocument content. And MemoryStream can be converted using ToArray() to a byte.



    As a code sample:



    byte templateContent = File.ReadAllBytes(templateFile);

    MemoryStream stream = new MemoryStream();
    stream.Write(templateContent, 0, templateContent.Length);

    WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true);

    // When done
    byte contentOfWordFile = stream.toArray();





    share|improve this answer















    Do you really have to use the Microsoft.Office.Interop and Microsoft.Office.Word?



    If it is not really necessary, you could use the OpenXML SDK libraries for manipulating the content of the WordDocument.



    OpenXML SDK contains a class WordprocessingDocument that can manipulate a memory stream containing a WordDocument content. And MemoryStream can be converted using ToArray() to a byte.



    As a code sample:



    byte templateContent = File.ReadAllBytes(templateFile);

    MemoryStream stream = new MemoryStream();
    stream.Write(templateContent, 0, templateContent.Length);

    WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true);

    // When done
    byte contentOfWordFile = stream.toArray();






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 11 '12 at 17:18

























    answered Jul 29 '11 at 9:03









    GeorgeGeorge

    307210




    307210













    • You actually don't even need the File.ReadAllBytes(), etc. part, because WordprocessingDocument will accept the file path, directly, instead of requiring the stream to be created outside, like that. You would do using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filePath, true)) { using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream())) { string docText = sr.ReadToEnd(); } XmlDocument xml = new XmlDocument(); xml.LoadXml(docText); // do stuff with XmlNodes… And then you would have docText = xml.OuterXml; that could then be converted to bytes.

      – vapcguy
      Nov 15 '18 at 3:03











    • The only problem with my method is you are then encoding a string of XML into bytes for the database: byte bytes = Encoding.ASCII.GetBytes(docText); , so you have to remember that if you want to reconstitute the file back to a docx you'd need to convert the bytes back to a string, then need using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))) { sw.Write(docText); }

      – vapcguy
      Nov 15 '18 at 3:13













    • Talks all about it: docs.microsoft.com/en-us/office/open-xml/…

      – vapcguy
      Nov 15 '18 at 3:15











    • Well, I also found that encoding the bytes is bad and doesn't work... it corrupts the file if you write it back out. I had to use File.ReadAllBytes() to read it, then File.WriteAllBytes() for it to be accurate. You can write the string of XML like I described, but converting bytes back to string doesn't work, because the encoding (whether you use UTF8, Unicode, Default, UTF7, ASCII, doesn't matter) will corrupt it.

      – vapcguy
      Nov 19 '18 at 19:16











    • Converting it to byte from a stream with .ToArray() also works, and using using (MemoryStream stream = new MemoryStream()) { stream.Write(byteArray, 0, (int)byteArray.Length); // Save the file with the new name File.WriteAllBytes(strDocumentPath, stream.ToArray()); } to write it back out also is a way to convert it back and forth successfully.

      – vapcguy
      Nov 26 '18 at 15:47





















    • You actually don't even need the File.ReadAllBytes(), etc. part, because WordprocessingDocument will accept the file path, directly, instead of requiring the stream to be created outside, like that. You would do using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filePath, true)) { using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream())) { string docText = sr.ReadToEnd(); } XmlDocument xml = new XmlDocument(); xml.LoadXml(docText); // do stuff with XmlNodes… And then you would have docText = xml.OuterXml; that could then be converted to bytes.

      – vapcguy
      Nov 15 '18 at 3:03











    • The only problem with my method is you are then encoding a string of XML into bytes for the database: byte bytes = Encoding.ASCII.GetBytes(docText); , so you have to remember that if you want to reconstitute the file back to a docx you'd need to convert the bytes back to a string, then need using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))) { sw.Write(docText); }

      – vapcguy
      Nov 15 '18 at 3:13













    • Talks all about it: docs.microsoft.com/en-us/office/open-xml/…

      – vapcguy
      Nov 15 '18 at 3:15











    • Well, I also found that encoding the bytes is bad and doesn't work... it corrupts the file if you write it back out. I had to use File.ReadAllBytes() to read it, then File.WriteAllBytes() for it to be accurate. You can write the string of XML like I described, but converting bytes back to string doesn't work, because the encoding (whether you use UTF8, Unicode, Default, UTF7, ASCII, doesn't matter) will corrupt it.

      – vapcguy
      Nov 19 '18 at 19:16











    • Converting it to byte from a stream with .ToArray() also works, and using using (MemoryStream stream = new MemoryStream()) { stream.Write(byteArray, 0, (int)byteArray.Length); // Save the file with the new name File.WriteAllBytes(strDocumentPath, stream.ToArray()); } to write it back out also is a way to convert it back and forth successfully.

      – vapcguy
      Nov 26 '18 at 15:47



















    You actually don't even need the File.ReadAllBytes(), etc. part, because WordprocessingDocument will accept the file path, directly, instead of requiring the stream to be created outside, like that. You would do using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filePath, true)) { using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream())) { string docText = sr.ReadToEnd(); } XmlDocument xml = new XmlDocument(); xml.LoadXml(docText); // do stuff with XmlNodes… And then you would have docText = xml.OuterXml; that could then be converted to bytes.

    – vapcguy
    Nov 15 '18 at 3:03





    You actually don't even need the File.ReadAllBytes(), etc. part, because WordprocessingDocument will accept the file path, directly, instead of requiring the stream to be created outside, like that. You would do using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filePath, true)) { using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream())) { string docText = sr.ReadToEnd(); } XmlDocument xml = new XmlDocument(); xml.LoadXml(docText); // do stuff with XmlNodes… And then you would have docText = xml.OuterXml; that could then be converted to bytes.

    – vapcguy
    Nov 15 '18 at 3:03













    The only problem with my method is you are then encoding a string of XML into bytes for the database: byte bytes = Encoding.ASCII.GetBytes(docText); , so you have to remember that if you want to reconstitute the file back to a docx you'd need to convert the bytes back to a string, then need using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))) { sw.Write(docText); }

    – vapcguy
    Nov 15 '18 at 3:13







    The only problem with my method is you are then encoding a string of XML into bytes for the database: byte bytes = Encoding.ASCII.GetBytes(docText); , so you have to remember that if you want to reconstitute the file back to a docx you'd need to convert the bytes back to a string, then need using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))) { sw.Write(docText); }

    – vapcguy
    Nov 15 '18 at 3:13















    Talks all about it: docs.microsoft.com/en-us/office/open-xml/…

    – vapcguy
    Nov 15 '18 at 3:15





    Talks all about it: docs.microsoft.com/en-us/office/open-xml/…

    – vapcguy
    Nov 15 '18 at 3:15













    Well, I also found that encoding the bytes is bad and doesn't work... it corrupts the file if you write it back out. I had to use File.ReadAllBytes() to read it, then File.WriteAllBytes() for it to be accurate. You can write the string of XML like I described, but converting bytes back to string doesn't work, because the encoding (whether you use UTF8, Unicode, Default, UTF7, ASCII, doesn't matter) will corrupt it.

    – vapcguy
    Nov 19 '18 at 19:16





    Well, I also found that encoding the bytes is bad and doesn't work... it corrupts the file if you write it back out. I had to use File.ReadAllBytes() to read it, then File.WriteAllBytes() for it to be accurate. You can write the string of XML like I described, but converting bytes back to string doesn't work, because the encoding (whether you use UTF8, Unicode, Default, UTF7, ASCII, doesn't matter) will corrupt it.

    – vapcguy
    Nov 19 '18 at 19:16













    Converting it to byte from a stream with .ToArray() also works, and using using (MemoryStream stream = new MemoryStream()) { stream.Write(byteArray, 0, (int)byteArray.Length); // Save the file with the new name File.WriteAllBytes(strDocumentPath, stream.ToArray()); } to write it back out also is a way to convert it back and forth successfully.

    – vapcguy
    Nov 26 '18 at 15:47







    Converting it to byte from a stream with .ToArray() also works, and using using (MemoryStream stream = new MemoryStream()) { stream.Write(byteArray, 0, (int)byteArray.Length); // Save the file with the new name File.WriteAllBytes(strDocumentPath, stream.ToArray()); } to write it back out also is a way to convert it back and forth successfully.

    – vapcguy
    Nov 26 '18 at 15:47















    0














    Sounds like this is a dynamically-created Word document.



    Since you have the document in the form of a Document object, you should be able to get its string of XML, then bytes, by doing this:



    Microsoft.Office.Interop.Word.Document d = new Microsoft.Office.Interop.Word.Document();

    // All of your building of the document was here
    // The object must be updated with content

    string docText = d.WordOpenXML; // this assumes content is here
    byte bytes = Encoding.UTF8.GetBytes(docText);


    I don't think that saving the object to the file system first is required, since you already have the object you have built all dynamically, in memory. It should just be a matter of accessing its WordOpenXML.



    If you were grabbing the file from the file system, it would look pretty much the same, except for how the document is opened first:



    string sourceFilePath = @"C:test.docx";
    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    var document = wordApp.Documents.Open(sourceFilePath);
    string docText = document.WordOpenXML;
    byte bytes = Encoding.UTF8.GetBytes(docText);


    If you ever want to download these bytes back into a document, you'd need to do this:



    string documentPath = @"C:test.docx"; // can be modified with dynamic paths, file name from database, etc.
    byte contentBytes = null;
    // … Fill contentBytes from the database, then...

    // Create the Word document using the path
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(documentPath, true))
    {
    // This should get you the XML string...
    string docText = System.Text.Encoding.UTF8.GetString(contentBytes);

    // Then we write it out...
    using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
    {
    sw.Write(docText);
    }
    }


    See How can I form a Word document using stream of bytes for more information.






    share|improve this answer






























      0














      Sounds like this is a dynamically-created Word document.



      Since you have the document in the form of a Document object, you should be able to get its string of XML, then bytes, by doing this:



      Microsoft.Office.Interop.Word.Document d = new Microsoft.Office.Interop.Word.Document();

      // All of your building of the document was here
      // The object must be updated with content

      string docText = d.WordOpenXML; // this assumes content is here
      byte bytes = Encoding.UTF8.GetBytes(docText);


      I don't think that saving the object to the file system first is required, since you already have the object you have built all dynamically, in memory. It should just be a matter of accessing its WordOpenXML.



      If you were grabbing the file from the file system, it would look pretty much the same, except for how the document is opened first:



      string sourceFilePath = @"C:test.docx";
      Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
      var document = wordApp.Documents.Open(sourceFilePath);
      string docText = document.WordOpenXML;
      byte bytes = Encoding.UTF8.GetBytes(docText);


      If you ever want to download these bytes back into a document, you'd need to do this:



      string documentPath = @"C:test.docx"; // can be modified with dynamic paths, file name from database, etc.
      byte contentBytes = null;
      // … Fill contentBytes from the database, then...

      // Create the Word document using the path
      using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(documentPath, true))
      {
      // This should get you the XML string...
      string docText = System.Text.Encoding.UTF8.GetString(contentBytes);

      // Then we write it out...
      using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
      {
      sw.Write(docText);
      }
      }


      See How can I form a Word document using stream of bytes for more information.






      share|improve this answer




























        0












        0








        0







        Sounds like this is a dynamically-created Word document.



        Since you have the document in the form of a Document object, you should be able to get its string of XML, then bytes, by doing this:



        Microsoft.Office.Interop.Word.Document d = new Microsoft.Office.Interop.Word.Document();

        // All of your building of the document was here
        // The object must be updated with content

        string docText = d.WordOpenXML; // this assumes content is here
        byte bytes = Encoding.UTF8.GetBytes(docText);


        I don't think that saving the object to the file system first is required, since you already have the object you have built all dynamically, in memory. It should just be a matter of accessing its WordOpenXML.



        If you were grabbing the file from the file system, it would look pretty much the same, except for how the document is opened first:



        string sourceFilePath = @"C:test.docx";
        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
        var document = wordApp.Documents.Open(sourceFilePath);
        string docText = document.WordOpenXML;
        byte bytes = Encoding.UTF8.GetBytes(docText);


        If you ever want to download these bytes back into a document, you'd need to do this:



        string documentPath = @"C:test.docx"; // can be modified with dynamic paths, file name from database, etc.
        byte contentBytes = null;
        // … Fill contentBytes from the database, then...

        // Create the Word document using the path
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(documentPath, true))
        {
        // This should get you the XML string...
        string docText = System.Text.Encoding.UTF8.GetString(contentBytes);

        // Then we write it out...
        using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
        {
        sw.Write(docText);
        }
        }


        See How can I form a Word document using stream of bytes for more information.






        share|improve this answer















        Sounds like this is a dynamically-created Word document.



        Since you have the document in the form of a Document object, you should be able to get its string of XML, then bytes, by doing this:



        Microsoft.Office.Interop.Word.Document d = new Microsoft.Office.Interop.Word.Document();

        // All of your building of the document was here
        // The object must be updated with content

        string docText = d.WordOpenXML; // this assumes content is here
        byte bytes = Encoding.UTF8.GetBytes(docText);


        I don't think that saving the object to the file system first is required, since you already have the object you have built all dynamically, in memory. It should just be a matter of accessing its WordOpenXML.



        If you were grabbing the file from the file system, it would look pretty much the same, except for how the document is opened first:



        string sourceFilePath = @"C:test.docx";
        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
        var document = wordApp.Documents.Open(sourceFilePath);
        string docText = document.WordOpenXML;
        byte bytes = Encoding.UTF8.GetBytes(docText);


        If you ever want to download these bytes back into a document, you'd need to do this:



        string documentPath = @"C:test.docx"; // can be modified with dynamic paths, file name from database, etc.
        byte contentBytes = null;
        // … Fill contentBytes from the database, then...

        // Create the Word document using the path
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(documentPath, true))
        {
        // This should get you the XML string...
        string docText = System.Text.Encoding.UTF8.GetString(contentBytes);

        // Then we write it out...
        using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
        {
        sw.Write(docText);
        }
        }


        See How can I form a Word document using stream of bytes for more information.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 '18 at 21:35

























        answered Nov 15 '18 at 4:01









        vapcguyvapcguy

        3,37612732




        3,37612732






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6023272%2fconvert-word-of-interop-object-to-byte-without-saving-physically%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen