【问题标题】:C# Office getting the object where I-Cursor isC# Office 获取 I-Cursor 所在的对象
【发布时间】:2017-02-09 08:44:52
【问题描述】:

我正在寻找一种解决方案,在该解决方案中,我可以在触发方法时获取 I 光标所在的对象(该方法使用 Word 2010 中功能区中的按钮运行)。

案例: 有一个 RichTextContentControl 元素,当我在其中按下鼠标时,I-Cursor 停留在文本中。然后,在按下功能区中的方法按钮后,它应该获取 I 光标所在的某个 RichTextContentControl 元素并执行一些操作:

internal void addLock(Object sender)    //object as RichTextContentControl where I-cursor was!
    {
        sender.LockContents = true;
        sender.LockContentControl = true;
    }

到目前为止,我尝试使用上述方法并进行了一些研究,但这只给了我用鼠标按下的功能区中的按钮类型,不是 I-Cursor 所在的位置(键入光标).

我希望有人有类似的问题和一些提示。

编辑:

我正在使用以下代码生成 RichTextContenControlElement:

public partial class ThisAddIn
{
    private RichTextContentControl richTextControl = null;
    private int index = 0;


    internal void SetRichTextControlOnDocument()
    {

        Document vstoDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
        index++;

        string name = "MyRichTextBoxControl_"+System.Convert.ToString(index);
        Word.Selection wordSelection = this.Application.Selection;

        if (!string.IsNullOrEmpty(wordSelection.Text.ToString()))
        {
            Word.Selection selection = this.Application.Selection;
            if (selection != null && selection.Range != null)
            {

                this.richTextControl = vstoDocument.Controls.AddRichTextContentControl(selection.Range, name);
                this.richTextControl.LockContentControl = true;
                this.richTextControl.LockContents = true;
            }
        }
        else
        {
            MessageBox.Show("No text was selceted to lock!", "Error");
        }
    }

如何使用“this”来处理我分别单击的 I 光标所在的 RichText 元素。 ?

【问题讨论】:

  • 这是 VSTO 解决方案吗?你试过this.Application.Selection吗? (this.Application 应该返回运行 VSTO 解决方案的 Word 应用程序)
  • 是的,我已经尝试过了,我回来了:system.__comobject 而不是某个richtextcontrolelement。
  • 在运行时,您正在与之交互的所有 Word 对象都将报告为system.__comobject。如果您将鼠标悬停在 this.Application.Selection 上,您应该会看到 Dynamic view 选项,这可能会有所帮助。
  • 谢谢!这确实有助于深入了解该对象。我更进一步,现在我可以使用selection.ParentContentControl.Tag.ToString() 获得某个 RichTextControl 元素的标签。但我找不到将它重新分配给我的.this 对象的方法。 .SelectContentControlsByTag(String tag) 不适用于 RichTextContentControls...

标签: c# visual-studio visual-studio-2015 ms-word


【解决方案1】:

获得了一个可行的解决方案,以使用以下代码获取 I-Cursor 所在的对象(例如 RichTextContentControl 元素)。基本上是从 ContentControl 中获取 .Tag 并遍历文档中的所有 ContentControls 并挑选出具有匹配标签的那个:

        internal void remLock()
    {
        vstoDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);

        Word.Selection selection = this.Application.Selection;

        String s; = selection.ParentContentControl.Tag.ToString();

        remByTag(s);

    }

    private void remByTag(String ccTag)
    {

        if (vstoDocument.ContentControls.Count != 0)
        {
            foreach (Word.ContentControl cc in vstoDocument.ContentControls)
            {
                if (cc.Type == Word.WdContentControlType.wdContentControlRichText)
                {
                    if (cc.Tag.ToString() == ccTag)
                    {
                        cc.LockContentControl = false;
                        cc.LockContents = false;

                        MessageBox.Show("Lock has been removed");
                        return;
                    }
                }
            }
        }
    }

编辑

以下操作也有效。如果单击 RichTextContentControl 元素,事件会将其分配给本地 RichTextContentControl:

        private Microsoft.Office.Interop.Word.ContentControl rt = null;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {

        this.Application.ActiveDocument.ContentControlOnEnter += ActiveDocument_ContentControlOnEnter;

    }



    private void ActiveDocument_ContentControlOnEnter(Word.ContentControl cc)
    {
        cc.Type = Word.WdContentControlType.wdContentControlRichText;

        this.rt = cc;


    }

【讨论】:

    猜你喜欢
    • 2014-09-03
    • 1970-01-01
    • 2013-07-04
    • 2022-06-29
    • 2012-03-04
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多