如何在C#/ WPF中以可编辑模式打开word文档文件(.doc)

时间:2014-07-08 18:55:03

标签: c# wpf office-interop

当我运行此代码时,它以Readonly模式打开文件,但我想在可编辑模式下打开我的文档文件。我已将readonly模式设置为false。这是我使用的代码: -

Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document document = new Microsoft.Office.Interop.Word.Document();

try
{
    //Set status for word application is to be visible or not.
    //Create a missing variable for missing value
    object readOnly = false;
    object missing = System.Reflection.Missing.Value;
    object isVisible = true;

    document = winword.Documents.Open(pathToFile, ReadOnly: false, Visible: true);
    document.Activate();

    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(winword);

    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(document);
}
catch (Exception ex)
{
    // WB.Close(false, Type.Missing, Type.Missing);
    throw;
}

1 个答案:

答案 0 :(得分:0)

我的猜测是你无法做到。 Readonly模式部分地作为一种安全措施来阻止某些宏在Word中执行。因此,提供一个覆盖它的SDK将对最终用户构成风险。您可以通过使用文件名参数直接运行exe来绕过它,从那时它就像一个人双击图标,但我不熟悉哪些库是或不可用在WPF中。在好的控制台或Windows窗体中,你一直在寻找

System.Diagnostics.Process.Start([the path of Word.exe], pathToFile);

但是您必须小心找到Word.exe的路径,因为这可能因字的版本和给出的安装说明而有所不同。可以想象,那些COM对象会以某种方式暴露路径,因为他们很有可能需要知道它,但我又不熟悉它们。