1

使用应用程序级加载项,我在打开文档时执行一些操作,这些操作需要将修订(跟踪的更改)呈现为内联而不是隐藏,以便它们包含在Range文档中。查阅文档后,我认为我所要做的就是更改活动窗口的视图属性MarkupMode:声称做我想做的事。

但是这个属性似乎与文档中的修订显示方式完全脱节!为了测试这一点,我尝试在文档中手动切换模式,然后在事件处理程序MarkupMode中查看并立即检查它。onSelectionChange我继续跟踪了 的一堆属性ActiveWindow.View,这是很好的衡量标准。令我惊讶和懊恼的是,当我查看具有内联更改的本地人时:

前

在值之前

...并将值与隐藏更改的值进行比较:

后

后值

没有改变!是什么赋予了?我是否没有查看正确的属性以确保内联呈现更改?Microsoft 是否完全无法编写有意义的文档?我会指出,我也尝试在代码中更改属性,以查看修订的呈现是否会更改,但没有成功。我将不胜感激任何反馈。

编辑:复制问题的简单代码:

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

private void application_WindowSelectionChange(Selection sel)
{
    var testDoc = sel.Document;

    var test = new
    {
        testDoc.ActiveWindow.View,
        testDoc.ActiveWindow.View.ShowRevisionsAndComments,
        testDoc.ActiveWindow.View.ShowInsertionsAndDeletions,
        testDoc.ActiveWindow.View.MarkupMode,
        testDoc.ActiveWindow.View.RevisionsMode
    };
}

编辑 2:除了这个人为的示例之外,我需要控制修订的标记样式,因为我正在搜索可能包含作为对象DocumentOpen存在的文本的文本。Revision更具体地说,我正在尝试使用上述文本执行以下操作(在修订中删除了文本“帮助您证明的强大方法”):

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

private void application_DocumentOpen(Document doc)
{
    // expected text, as taken from screengrab example above. Includes
    //  text removed in a revision
    string expectedText = "Video provides a powerful way to help you prove your point.";

    // make sure that we're in print view
    if (doc.ActiveWindow.View.Type != WdViewType.wdPrintView)
    {
        doc.ActiveWindow.View.Type = WdViewType.wdPrintView;
    }

    // attempt to ensure that document revisions are marked up inline. Does not accomplish anything
    doc.ActiveWindow.View.MarkupMode = WdRevisionsMode.wdInLineRevisions;        
    // attempt to locate text. Will fail if revisions are not marked up inline (deletion is not part of document content range otherwise)
    var locatedRange = doc.Content.OccurrenceOfText(expectedText);
}

// extension method to locate text inside a range. Searching entire Content in this example
private static Range OccurrenceOfText(this Range rng, string text)
{
    rng.Find.Forward = true;
    rng.Find.Format = false;

    rng.Find.Execute(text);

    if (!rng.Find.Found)
    {
        throw new Exception("Unable to locate text! Are Revisions marked up inline?");
    }

    // return brand new range containing located content
    return rng.Document.Range(rng.Start, rng.End);
}

编辑 3:正如 Cindy 所说,我的问题是我使用了错误的属性:我需要使用该View.RevisionsFilter.Markup属性进行更改。此外,我没有诊断出的一个问题是,根据 View 属性,完全有可能Range像我一样执行的搜索返回的Text属性与搜索的文本不同。如果Revision对象存在于Range.

4

1 回答 1

2

这个对我有用。我做了什么:

  1. 创建了一个测试文档。使用 =rand() -> 按 Enter 将一些文本放入文档中。复制到短语中,将“强大”改为“有用”。
  2. 打开跟踪更改并进行了一些更改,包括选择“有用”并输入“强大”。
  3. 确保更改显示在气球中,并显示标记的“原始”视图。
  4. 保存并关闭文档。
  5. 在调用 OccurrenceOfText 的行上放置一个断点,然后按 F5 以在调试模式下启动加载项。
  6. 打开保存的文档。Open 事件已触发。我检查了 View.Markup 和 View.RevisionsView 的值——它们已更改为 inline 和“Final”。单步执行其余代码并正确执行(发现发生)。

我需要稍微修改您的代码,因为我没有您的“包装器”。我还更改了 OccurrenceOfText 返回 Range 的方式:无需以您使用的复杂方式进行操作...

我注意到您似乎没有设置 View.RevisionsView - 也许这就是您没有看到预期结果的原因?

 private void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document doc)
  {
            //Test revisions
    // expected text, as taken from screengrab example above. Includes
    //  text removed in a revision
    string expectedText = "Video provides a powerful way to help you prove your point.";

    // make sure that we're in print view
    if (doc.ActiveWindow.View.Type != Word.WdViewType.wdPrintView)
    {
        doc.ActiveWindow.View.Type = Word.WdViewType.wdPrintView;
    }

    // attempt to ensure that document revisions are marked up inline. Does not accomplish anything
    Word.View vw = doc.ActiveWindow.View;
    vw.MarkupMode = Word.WdRevisionsMode.wdInLineRevisions;
    vw.RevisionsView = Word.WdRevisionsView.wdRevisionsViewFinal;      
    // attempt to locate text. Will fail if revisions are not marked up inline (deletion is not part of document content range otherwise)
    var locatedRange = OccurrenceOfText(doc.Content, expectedText);
}

// extension method to locate text inside a range. Searching entire Content in this example
private static Word.Range OccurrenceOfText(Word.Range rng, string text)
{
    rng.Find.Forward = true;
    rng.Find.Format = false;

    rng.Find.Execute(text);

    if (!rng.Find.Found)
    {
        throw new Exception("Unable to locate text! Are Revisions marked up inline?");
    }

    // return brand new range containing located content
    return rng;  //.Document.Range(rng.Start, rng.End);
 }
于 2015-11-30T20:54:12.097 回答