我正在尝试访问现有 .docx 文档(Office Open XML)的所有Rich-Text-Content-Controls。
我找到了一种通过循环指定范围来获取文档的所有内容控件的方法:
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);
}
但我找不到将这些 ContentControl (程序集:Microsoft.Office.Interop.Word
)转换为 RichTextContentControl 的方法(程序集:Microsoft.Office.Tools.Word
)。
RichTextContentControl richTextContentControl = contentControl as RichTextContentControl;
throws Exception
我想进行此投射,因为我需要订阅RichTextContentControl的输入和退出事件。
richTextContentControl.Entering += (sender, args) => {/*..*/ };
richTextContentControl.Exiting += (sender, args) => {/*..*/ };
答案 0 :(得分:1)
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));
};
}