在另一个 ContentControl 之后插入 ContentControl

发布于 2024-08-30 16:49:41 字数 1193 浏览 5 评论 0原文

在我们的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写给空气的情书 2024-09-06 16:49:41

回答有点晚了,但这就是我处理这种情况的方法:

我认为这基本上就是你所做的,但我插入了一个段落,然后移动到一个段落单元。似乎适用于我的情况。

Globals.ThisDocument.Paragraphs.First.Range.Select();

var cc = Globals.ThisDocument.Controls.AddRichTextContentControl(Guid.NewGuid().ToString());

var newRange = cc.Range;
object count = 1;
newRange.Move(Unit: Word.WdUnits.wdParagraph, Count: ref count);

Globals.ThisDocument.Paragraphs.Add();
newRange.Move(Unit: Word.WdUnits.wdParagraph, Count: ref count);

var cc2 = Globals.ThisDocument.Controls.AddRichTextContentControl(newRange, Guid.NewGuid().ToString());

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.

Globals.ThisDocument.Paragraphs.First.Range.Select();

var cc = Globals.ThisDocument.Controls.AddRichTextContentControl(Guid.NewGuid().ToString());

var newRange = cc.Range;
object count = 1;
newRange.Move(Unit: Word.WdUnits.wdParagraph, Count: ref count);

Globals.ThisDocument.Paragraphs.Add();
newRange.Move(Unit: Word.WdUnits.wdParagraph, Count: ref count);

var cc2 = Globals.ThisDocument.Controls.AddRichTextContentControl(newRange, Guid.NewGuid().ToString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文