在另一个 ContentControl 之后插入 ContentControl
在我们的 VSTO Word 2010 Addin 中,我们尝试在给定的其他 ContentControl 之后插入 RichTextControl。我们已经尝试过这一点:
public ContentControl AddContentControl(WdContentControlType type, int position)
{
Paragraph paragraphBefore = null;
if (position == 0)
{
if (WordDocument.Paragraphs.Count == 0)
{
WordDocument.Paragraphs.Add();
}
paragraphBefore = WordDocument.Paragraphs.First;
}
else
{
paragraphBefore = Controls.ElementAt(position - 1).Range.Paragraphs.Last;
}
object start = paragraphBefore.Range.End;
object end = paragraphBefore.Range.End + 1;
paragraphBefore.Range.InsertParagraphAfter();
Range rangeToUse = WordDocument.Range(ref start, ref end);
ContentControl newControl = _ContentControl = _WordDocument.ContentControls.Add(type, rangeToInsert);
Controls.Insert(position, newControl);
OnNewContentControl(newControl, position);
return newControl.ContentControl;
}
它工作得很好,除非我们要插入的控件之前的控件末尾有一个空段落。如果是这种情况,新的 ContentControl 将插入到最后一个控件中。
我们怎样才能避免这种情况呢?
In our VSTO Word 2010 Addin, we are trying to insert a RichTextControl after a given other ContentControl. We have tried this:
public ContentControl AddContentControl(WdContentControlType type, int position)
{
Paragraph paragraphBefore = null;
if (position == 0)
{
if (WordDocument.Paragraphs.Count == 0)
{
WordDocument.Paragraphs.Add();
}
paragraphBefore = WordDocument.Paragraphs.First;
}
else
{
paragraphBefore = Controls.ElementAt(position - 1).Range.Paragraphs.Last;
}
object start = paragraphBefore.Range.End;
object end = paragraphBefore.Range.End + 1;
paragraphBefore.Range.InsertParagraphAfter();
Range rangeToUse = WordDocument.Range(ref start, ref end);
ContentControl newControl = _ContentControl = _WordDocument.ContentControls.Add(type, rangeToInsert);
Controls.Insert(position, newControl);
OnNewContentControl(newControl, position);
return newControl.ContentControl;
}
which works fine, unless the control that is before the one we want to insert has an empty paragraph at the end. If that is the case, the new ContentControl is inserted within the last control.
How can we avoid this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答有点晚了,但这就是我处理这种情况的方法:
我认为这基本上就是你所做的,但我插入了一个段落,然后移动到一个段落单元。似乎适用于我的情况。
A little late for an answer, but this is what I did to handle this case:
I think it's basically what you did, but I insert a paragraph and then move to by a Paragraph unit. Seems to work in my case.