默认情况下在编辑模式下使用 VSTO 打开下载的 Word 文档

Opening a downloaded Word document with VSTO in edit mode by default

我有一个从 Internet 下载的 word 文档,我想在应用程序级加载项中对其执行一些操作。这些操作(文档搜索、取消保护文档等)要求文档在打开时处于编辑模式。这里有一些示例代码可以说明我的需求:

private void ThisAddIn_Startup(object sender, EventArgs e)
{
    Application.DocumentOpen += application_DocumentOpen;
}

private void application_DocumentOpen(Document doc)
{
    if (doc.ProtectionType != WdProtectionType.wdNoProtection)
    {
        // this throws a COMException if the document is opened in read-only mode
        doc.Unprotect("password");
    }

    // ...
}

由于此加载项将分发给多个用户,我不能假设用户会设置任何应用程序属性,例如默认以编辑模式打开下载的文档,因此最好在代码中进行.考虑到我的限制,VSTO 或互操作库是否有一些方法可以实现这一点?感谢您的帮助。

编辑: 我的应用程序级加载项已安装在 Word 2013 上进行测试,并使用 VS 2013、VSTO 4.0 创建。

显示必要特征的示例文档can be found here。文档受 WdProtectionType.wdAllowOnlyFormFields 保护,密码为 "password".

您使用的 Word/VSTO 是什么版本。

我使用 Word 2013(64 位)和 VSTO 4.0/Visual Studio 2013 进行了尝试,并且在偶数处理函数 "application_DocumentOpen" 中没有出现任何异常。对于只读文档和受保护文档。

编辑: 在取消保护文档之前尝试将视图更改为打印视图。

if (doc.ProtectionType != Word.WdProtectionType.wdNoProtection)
{
doc.ActiveWindow.View.Type = Word.WdViewType.wdPrintView; 
doc.Unprotect("password"); 
} 

参考:http://answers.microsoft.com/en-us/office/forum/office_2013_release-word/not-available-for-reading-error-on-unprotecting-a/a888701b-d70a-4dbc-a1ec-68b8bad80848