Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

ms word - Serialize current ActiveDocument from office 2007 add-in

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...