Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...