【问题标题】:How to get the previous list number of the removed paragraph如何获取已删除段落的上一个列表编号
【发布时间】:2020-07-07 10:31:31
【问题描述】:

我有一个启用跟踪更改的文档。例如:

我可以通过访问属性Paragraph.Range.ListFormat.ListString 找到列表项的实际列表编号。对于第 1 段和第 2 段,此属性具有适当的值“1”和“2”。但是当涉及到第 3 段和第 4 段时,此属性适当地包含值“1”和“3”,尽管之前的值是“3”和“4”。我可以使用 VSTO 中的任何其他属性或方法来获取 Paragraph.Range.ListFormat.ListString 属性的先前值吗?

更新:

这是我用来提取列表编号以阐明我想要实现的目标的代码。

var listParagraphs = Document.ListParagraphs.Cast<Paragraph>().ToList();
var actualListNumbers = new List<string>();
var previousListNumbers = new List<string>();

foreach(var paragraph in listParagraphs)
{
    actualListNumbers.Add(paragraph.Range.ListFormat.ListString);
    previousListNumbers.Add(?);
}

// actualListNumbers will contains the following values: 1, 2, 1, 3
// previousListNumbers should contain the following values: 1, 2, 3, 4

【问题讨论】:

  • 你可以分享你的代码,明确地在你使用Paragraph.Range.ListFormat.ListString的地方吗?
  • @UfguFugullu 用代码示例更新了我的问题

标签: ms-word vsto office-interop word-addins


【解决方案1】:

我做了很多研究,发现了Revisions。在这里可以读出旧值。但是,Microsoft Office 中似乎出现了错误,因为ListString 始终显示“1”。对于已删除的列表条目。

我尝试编写一个小类ParagraphRevisionHolder 来读取之前的值,但错误仍然存​​在。如果您确切地知道您的列表是什么样的,您可以编写一个解决方法,例如通过读取项目的索引。

在下面的示例中,我还读出了type of change,因此您可以查看列表条目是否被删除或编辑。 我希望你能找到解决问题的方法。如果你能在这里展示一下就好了。

/// <summary>
/// This function will be called on the start of this add in
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ThisAddIn_Startup(object sender, EventArgs e) {
    // Event will be fired when a document was opened
    this.Application.DocumentOpen += ThisApplication_Open;
}

/// <summary>
/// Get all ListParagraphs of the opened document
/// </summary>
/// <param name="document">the opened document</param>
void ThisApplication_Open(Document document) {
    ListParagraphs listParagraphs = document.ListParagraphs;
    List<ParagraphRevisionHolder> list = new List<ParagraphRevisionHolder>();

    foreach(Paragraph paragraph in listParagraphs) {
        list.Add(new ParagraphRevisionHolder(paragraph.Range));
    }
}

class ParagraphRevisionHolder {

    /// <summary>
    /// Returns if revisions of the range does exist
    /// </summary>
    public bool RevisionsExist = false;

    /// <summary>
    /// Returns the current ListString
    /// </summary>
    public string CurrentListString;

    /// <summary>
    /// Returns the current text
    /// </summary>
    public string CurrentText;

    /// <summary>
    /// Returns the ListString of the previous revision
    /// </summary>
    public string PreviousListString;

    /// <summary>
    /// Returns the text of the previous revision
    /// </summary>
    public string PreviousText;

    /// <summary>
    /// Returns the type of the revision
    /// </summary>
    public WdRevisionType PreviousRevisionType;

    /// <summary>
    /// Returns a collection of all revisions
    /// </summary>
    private Revisions Revisions;

    /// <summary>
    /// Constructor of this class
    /// </summary>
    /// <param name="paragraphRange">the range of a paragraph</param>
    public ParagraphRevisionHolder(Range paragraphRange) {
        this.CurrentListString = paragraphRange.ListFormat.ListString;
        this.CurrentText = paragraphRange.Text;
        this.Revisions = paragraphRange.Revisions;

        this.GetPreviousText();
    }

    /// <summary>
    /// Sets the data of the first revision
    /// </summary>
    private void GetPreviousText() {
        if (Revisions.Count > 0) {
            RevisionsExist = true;

            foreach(Revision revision in Revisions) {
                this.PreviousRevisionType = revision.Type;
                this.PreviousText = revision.Range.Text;

                // with the example document of your screenshot, revisions ListString always incorrectly returns "1."
                this.PreviousListString = revision.Range.ListFormat.ListString;

                break;
            }
        }
    }

}

【讨论】:

    【解决方案2】:

    我也遇到了这个问题。更有趣的是,当您删除列表中的最后一个(在您的情况下)项目时,删除的项目将保留其原始列表编号 4。

    在这种情况下我的解决方法是:

    1. 获取已删除的段落标记并拒绝删除(您必须以某种方式执行 RejectAll,因为 Revision(1) 将拒绝整个已删除的列表项)
    2. RejectAll 之后,listString 将从 1. 更改为 3.
    3. 获取列表字符串
    4. 再次删除段落标记

    【讨论】:

    • 我应该提到我不想编辑原始文档。拒绝修订,然后再次删除段落标记会更改修订的作者。
    • 您可以通过 ActiveDocument.Undo 恢复原状
    猜你喜欢
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    相关资源
    最近更新 更多