Serialize current ActiveDocument from office 2007

2019-01-20 01:22发布

问题:

Im creating a VSTO Office 2007 add-in.

I need to be enable or find a solution where I can save to a webservice with a byte[] instead of hard drive.

So, I open the document by going to a website and clicking on a url, that click send me a Word Document, and I select Open using MS Word 2007.

The document open, and if I check the data I have:

ActiveDocument.Fullname = http://[servername or ip]/[some iis folder]/file.asp?id=353&type=doc`

so I think this is all in memory since I dont have the original file or temp file if exists.

I have no problem from a disk, even if the document is open. How can I do a byte[] from an current ActiveDocument?

I have found a lot of answers, that state that it cannot be done. But I also have a customer that have an old add-in that does the byte[] from a current ActiveDocument.

Can anyone help me.

回答1:

You can convert ActiveDocument to the COM interop IPersistFile to save a copy of the open documents' bytes to a local temp path and then read them back in for sending to your webservice. In Office, you cannot read the bytes from the active document without first saving to disk. See this MSDN blog for reference.

COM.IPersistFile compoundDocument = Application.ActiveDocument as COM.IPersistFile; 
compoundDocument.Save(filePath, false);
byte[] content = File.ReadAllBytes(filePath);