Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C取消选择COMobject WordDocument_C#_Visual Studio 2015_Vsto_Selection_Office Interop - Fatal编程技术网

C# C取消选择COMobject WordDocument

C# C取消选择COMobject WordDocument,c#,visual-studio-2015,vsto,selection,office-interop,C#,Visual Studio 2015,Vsto,Selection,Office Interop,在Visual Studio中,我创建了一个Word 2016文档项目。在此文档中,我添加了一个自定义ActionsPane控件。此控件所做的唯一事情是向活动文档添加一个PlainTextContentControl if (Globals.ThisDocument.Content.Application.ActiveDocument == null) return; var tagControl = Globals.ThisDocument.Controls.AddPlainT

在Visual Studio中,我创建了一个Word 2016文档项目。在此文档中,我添加了一个自定义ActionsPane控件。此控件所做的唯一事情是向活动文档添加一个PlainTextContentControl

if (Globals.ThisDocument.Content.Application.ActiveDocument == null) return;
        var tagControl = Globals.ThisDocument.Controls.AddPlainTextContentControl(Guid.NewGuid().ToString());
tagControl.PlaceholderText = @"PLACEHOLDER";
tagControl.LockContents = true;
这一切都很好,在Word文档中添加并选择了plaintextcontrol。但是我想要的是添加控件,并且光标将跳到控件的末尾,这样用户就可以直接开始键入。将自动选择新添加的控件。我怎么才能把这个转过来

我已经试过:

var range = Globals.ThisDocument.Content;
range.Application.Selection.Collapse();
有人能帮我吗?谢谢

编辑: Als尝试了这个解决方案

private static IntPtr documentHandle;
        public delegate bool EnumChildProc(IntPtr hwnd, int lParam);

        [DllImport("user32.dll")]
        public static extern System.IntPtr SetFocus(System.IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(IntPtr hWndParent, EnumChildProc callback, int lParam);
        [DllImport("user32.dll")]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int count);

        private static bool EnumChildWindow_Handle(IntPtr handle, int lparam)
        {
            StringBuilder s = new StringBuilder(50);
            GetWindowText(handle, s, 50);
            Debug.WriteLine(s.ToString());
            if (s.ToString() == "Microsoft Word-document")
            {
                documentHandle = handle;
            }
            return true;
        }

 private void MoveCursorToEndOfLastAddedTag(PlainTextContentControl ctrl)
        {
            EnumChildProc EnumChildWindow = new EnumChildProc(EnumChildWindow_Handle);
            EnumChildWindows(Process.GetCurrentProcess().MainWindowHandle, EnumChildWindow, 0);
            SetFocus(documentHandle);
}

这也不行。

我终于成功了。这不是最干净的方式,但它像一个魅力。我唯一的代码是:

private void MoveCursorToEndOfLastAddedTag(PlainTextContentControl ctrl)
{
    System.Windows.Forms.SendKeys.Send("{F10}");
    System.Windows.Forms.SendKeys.Send("{F10}");
    System.Windows.Forms.SendKeys.Send("{RIGHT}");
}
它发送两次F10键。第一次将焦点设置到功能区时,第二次将焦点返回到文档。使用right键,我从PlainTextContentControl中删除所选内容


谢谢大家的帮助。

您的上一个代码片段没有多大意义,特别是因为折叠的默认行为是wdCollapseStart。内容控制是文档中的最后一项吗?如果在控件中插入控件选择后更改代码,使其完成,然后在Word UI中按向右箭头,会发生什么情况?这会给你带来你想要的结果吗?FWIW通常情况下,我会期望类似以下的东西工作:Word.Range rng?tagControl.InnerObject.Range;对象oCollapse=Word.Wd.collapseDirection.wdCollapseEnd;rng.崩塌或崩塌;rng.选择;您好Cindy,事实上,当控件添加到Word文档中时,我按下向右箭头键,它会为我提供所需的外观。光标现在位于添加的控件之后。。我将尝试您的代码部分并让您知道。我已将代码更改为:我已将您作为答案发布的内容移动到您的问题中,因为答案中只应包含回答您问题的内容。这是你应该自己做的事情,下次再做;只需单击问题下方的“编辑”链接即可对其进行编辑。不是期望的结果:您需要具体说明问题的工作方式。而且,是的,当用户在操作窗格中工作时,焦点将出现在操作窗格中,从我的记忆中,无法以编程方式将焦点移回文档。当然不能使用Word/VSTO对象模型,尽管Windows API可能会管理它。用户必须单击文档上的“上一步”或按Ctrl+F6,直到焦点在运行完所有功能区后返回到文档,等等。