mikipero
mikipero

Reputation: 440

c# Microsoft.Office.Interop.Word.Document to Stream

I have a active document :

Microsoft.Office.Interop.Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;

How I can convert this to stream?

Or even better, how to send active document to rest web service?

Thanks

Upvotes: 4

Views: 5989

Answers (2)

user3285954
user3285954

Reputation: 4749

Save to a temp file then read file content as required by the rest service, e.g. byte array. Or use Open XML SDK if older Word formats are not required.

Upvotes: 0

Fixation
Fixation

Reputation: 989

Which rest-service? If it's SharePoint or the like i'm used to just calling the SaveAs function as follows:

Microsoft.Office.Interop.Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;
document.SaveAs("https://www.contoso.sharepoint.com/Documents/Document1.docx");

Edit: P.S. You can serialize anything to Stream. found an answer here

public static MemoryStream SerializeToStream(object o)
{
    MemoryStream stream = new MemoryStream();
    IFormatter formatter = new BinaryFormatter();
    formatter.Serialize(stream, o);
    return stream;
}

public static object DeserializeFromStream(MemoryStream stream)
{
    IFormatter formatter = new BinaryFormatter();
    stream.Seek(0, SeekOrigin.Begin);
    object o = formatter.Deserialize(stream);
    return o;
}

Upvotes: 1

Related Questions