简体   繁体   English

在MS Office加载项中获取当前活动的文档文件

[英]Get currently active document file in MS Office Add-In

I creating VSTO Add-In that will send documents to server via REST API. 我创建了VSTO加载项,该加载项将通过REST API将文档发送到服务器。 I need send currently opened document (eg docx ) just as a file . 我需要发送当前打开的文档(例如docx作为文件

First problem was getting full name of the active document. 第一个问题是获取活动文档的全名。 If found the only way: 如果找到唯一方法:

Path.Combine(Globals.ThisAddIn.Application.ActiveDocument.Path,
             Globals.ThisAddIn.Application.ActiveDocument.Name)

This code can return good path on local drive: D:\\Docs\\Doc1.docx But also it can return HTTP path to document in a cloud (eg OneDrive ): https://d.docs.live.net/xxxxx/Docs\\Doc1.docx 此代码可以在本地驱动器上返回正确的路径: D:\\Docs\\Doc1.docx但它也可以返回到云中文档的HTTP路径(例如OneDrive ): https://d.docs.live.net/xxxxx/Docs\\Doc1.docx : D:\\Docs\\Doc1.docx https://d.docs.live.net/xxxxx/Docs\\Doc1.docx

Even if it would only local documents I can't get file of this document. 即使只有本地文档,我也无法获取此文档的文件。 I tryed this code: 我尝试了这段代码:

using (var stream = new StreamReader(docFullPath)) { }

And in case of locally stored document I got System.IO.IOException: The process cannot access the file because it is being used by another process . 在本地存储的文档中,我得到了System.IO.IOException: The process cannot access the file because it is being used by another process Not surprizing. 不令人惊讶。

And in case of cloud stored document I got System.NotSupportedException: The given path's format is not supported . 在云存储文档的情况下,我得到了System.NotSupportedException: The given path's format is not supported Of cource! 当然!

I believe I'm doing all wrong and my goal is reachable. 我相信我做错了一切,我的目标是可以实现的。 My question is: How read file of currently opened document on MS Office App from Add-In without closing App? 我的问题是:如何在不关闭App的情况下从外接程序读取MS Office App上当前打开的文档的文件?

Even if you could access the file ActiveDocument.FullName points to there is no guarantee that the user already has saved all changes to disk, or even worse, the document is still in the state of creation and has never been saved yet. 即使您可以访问文件ActiveDocument.FullName指向,也无法保证用户已经将所有更改保存到磁盘,甚至更糟的是,该文档仍处于创建状态并且从未保存过。

There is another little known/documented way to retrieve a file of an open document which is using the IPersistFile COM interface which is implemented by the Document object. 还有另一种鲜为人知/已记录的方法来检索打开的文档的文件,该方法使用IPersistFile COM接口,该接口由Document对象实现。 The following sample saves a document to the specified location. 以下示例将文档保存到指定位置。 This happens without modifying the save state of the document, ie you get the exact version of the document as it is open (and not as it has previously been saved to disk) and the user may later still decide to save possible modifications of the document or not. 在不修改文档的保存状态的情况下会发生这种情况,即,您在打开文档时会获得文档的确切版本(而不是以前保存到磁盘的版本),并且用户以后可能仍会决定保存文档的可能修改或不。

public static void SaveCopyAs(this Document doc, string path)
{
    var persistFile = (System.Runtime.InteropServices.ComTypes.IPersistFile)doc;
    persistFile.Save(path, false);
}

You can copy an open document on the filesystem using File.Copy(FullName, <some_temp_filename>) , and then send the copy to your REST service. 您可以使用File.Copy(FullName, <some_temp_filename>)在文件系统上复制打开的文档,然后将副本发送到REST服务。 This works even though it's still open for exclusive reading/writing in Word. 即使它仍然可以在Word中进行独家读取/写入,此方法仍然有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM