简体   繁体   English

订阅现有RichTextContentControl的事件

[英]Subscribe to events of an existing RichTextContentControl

I'm trying to access all Rich-Text-Content-Controls of an existing .docx Document (Office Open XML). 我正在尝试访问现有.docx文档(Office Open XML)的所有Rich-Text-Content-Controls。

I've found a way to get all Content Controls of a Document by looping over a specified range: 我找到了一种通过遍历指定范围来获取文档的所有内容控件的方法:

var contentControls = new List<ContentControl>();
Range rangeStory;
foreach (Range range in wordDocument.StoryRanges)
{
    rangeStory = range;
    do
    {
        try
        {
            contentControls.AddRange(rangeStory.ContentControls.Cast<ContentControl>());
        }
        catch (COMException) { }
        rangeStory = rangeStory.NextStoryRange;
    }
    while (rangeStory != null);
}

But I can't find a way to cast these ContentControl s (assembly: Microsoft.Office.Interop.Word ) to RichTextContentControl s (assembly: Microsoft.Office.Tools.Word ). 但是我找不到将这些ContentControl (程序集: Microsoft.Office.Interop.Word )转换为RichTextContentControl (程序集: Microsoft.Office.Tools.Word )的方法。

RichTextContentControl richTextContentControl = contentControl as RichTextContentControl;
throws Exception

I want to do this casting, because I need to subscribe to RichTextContentControl's entering and exiting events. 我想进行此转换,因为我需要订阅RichTextContentControl的进入退出事件。

richTextContentControl.Entering += (sender, args) => {/*..*/ };
richTextContentControl.Exiting += (sender, args) => {/*..*/ }; 

Found it! 找到了! There is a very easy way to access any kind of Content Controls via Vsto: 有一种非常简单的方法可以通过Vsto访问任何内容控件:

foreach (var result in thisDocument.Controls.OfType<RichTextContentControl>())
{
    result.Entering += (sender, args) =>
    {
        MediatorContext.Current.Send(new CurrentKomponenteChangedRequest(result.ID, State.Entering));
    };

    result.Exiting += (sender, args) =>
    {
        MediatorContext.Current.Send(new CurrentKomponenteChangedRequest(result.ID, State.Exiting));
    };
}

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

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