C# 当我尝试为PowerPoint 2013使用UI自动化时,使用RangeFromPoint时,我只能获取第一个字符/单词
该代码适用于Word和Outlook,但在PowerPoint中失败,因为只有文本框的第一个字符或第一个单词被选中。这是虫子吗?有什么解决办法吗?请在PowerPoint 2013中的简单PowerPoint幻灯片上尝试此操作C# 当我尝试为PowerPoint 2013使用UI自动化时,使用RangeFromPoint时,我只能获取第一个字符/单词,c#,automation,powerpoint,ui-automation,C#,Automation,Powerpoint,Ui Automation,该代码适用于Word和Outlook,但在PowerPoint中失败,因为只有文本框的第一个字符或第一个单词被选中。这是虫子吗?有什么解决办法吗?请在PowerPoint 2013中的简单PowerPoint幻灯片上尝试此操作 private static async Task<string> getText(double x, double y) { string result = null; try { var location = ne
private static async Task<string> getText(double x, double y)
{
string result = null;
try
{
var location = new System.Windows.Point(x, y);
AutomationElement element = AutomationElement.FromPoint(location);
object patternObj;
if (element.TryGetCurrentPattern(TextPattern.Pattern, out patternObj))
{
var textPattern = (TextPattern)patternObj;
var range = textPattern.RangeFromPoint(location);
range.ExpandToEnclosingUnit(TextUnit.Word);
range.Select();
var text = range.GetText(-1).TrimEnd('\r');
return text.Trim();
}
else
{
return "no text found";
}
}
catch (Exception ex)
{
return ex.Message;
}
}
我明白了
此行为可能是由于PowerPoint 2013中的限制,我希望您无法使用UIA解决此问题。调用RangeFromPoint()时,鼠标下方的UIA提供程序(即实现IUIAutomationTextPattern::RangeFromPoint()的提供程序)将返回鼠标光标所在的退化(即空)范围。然后UIA客户端可以扩展返回的范围,以获取周围的字符、单词、行或段落 然而,正如您所指出的,PowerPoint 2013并没有做到这一点。我刚刚编写了下面的测试代码(使用tlbimp.exe生成的本机Windows UIA API的托管包装),发现PowerPoint显然为光标下的整个文本框返回了一个TextRange。当我运行代码时,我发现我确实在写字板、word 2013和PowerPoint OnLine中得到了光标下的预期单词,但不是PowerPoint 2013。当我运行作为Inspect SDK工具一部分的文本资源管理器工具时,得到了相同的结果。下图显示了文本浏览器,当鼠标悬停在其中一个单词上时,该浏览器报告PowerPoint 2013返回的文本是a文本框中的全部文本 (我应该补充一点,为了让下面的测试代码能够正常工作,我认为当前的显示缩放设置需要为100%。我没有添加代码来说明其他一些正在活动的缩放。) 我不知道这是否在2016年的PowerPoint中得到了解决,我会尝试调查并让您知道 谢谢 家伙
我建议只使用Python代码获取幻灯片的标题文本(例如)。对不起,我没有时间在C上重新写了。您可以使用
PowerPoint.Application
COM对象和
谢谢你调查这家伙。我们有没有可能在微软找到一个可以修复或提供解决方案的人?我会看看是否能找到更多关于这方面的信息,并让你知道。今天早上我安装了Office 2016预览版,并将Inspect的文本浏览器指向PowerPoint 2016,它的行为方式似乎与PowerPoint 2013相同。也就是说,当我尝试查找鼠标光标下的文本范围时,PowerPoint会返回光标下文本框的整个文本,而不是光标所在位置的退化范围。感谢您为我测试。任何帮助都将不胜感激。有什么新消息或线索吗?伙计,我从一个PowerPoint开发人员那里得到消息,这已经被复制并作为bug归档。希望他们能很快找到解决方案。为什么不使用Power Point的COM接口呢?您可以通过
PowerPoint.Application
对象访问它。每个MS Office产品都有很好的对象模型作为COM接口。我没有时间编写C代码,但在Python中我可以得到文本:pp.Presentations[0]。幻灯片[8]。形状[0]。TextFrame.TextRange.text
(第9张幻灯片的标题)。其中pp
是PowerPoint.Application
的一个实例。
text = printRange(range, text);
while (range.Move(TextUnit.Word, 1) > 0)
{
text += Environment.NewLine;
text = printRange(range, text);
}
private void buttonGetTheText_Click(object sender, EventArgs e)
{
labelText.Text = "No text found.";
IUIAutomation uiAutomation = new CUIAutomation8();
Point ptCursor = Cursor.Position;
tagPOINT pt;
pt.x = ptCursor.X;
pt.y = ptCursor.Y;
// Cache the Text pattern that's available through the element beneath
// the mouse cursor, (if the Text pattern's supported by the element,) in
// order to avoid another cross-process call to get the pattern later.
int patternIdText = 10014; // UIA_TextPatternId
IUIAutomationCacheRequest cacheRequestTextPattern =
uiAutomation.CreateCacheRequest();
cacheRequestTextPattern.AddPattern(patternIdText);
// Now get the element beneath the mouse.
IUIAutomationElement element =
uiAutomation.ElementFromPointBuildCache(pt, cacheRequestTextPattern);
// Does the element support the Text pattern?
IUIAutomationTextPattern textPattern =
element.GetCachedPattern(patternIdText);
if (textPattern != null)
{
// Now get the degenerative TextRange where the mouse is.
IUIAutomationTextRange range = textPattern.RangeFromPoint(pt);
if (range != null)
{
// Expand the range to include the word surrounding
// the point where the mouse is.
range.ExpandToEnclosingUnit(TextUnit.TextUnit_Word);
// Show the word in the test app.
labelText.Text = "Text is: \"" + range.GetText(256) + "\"";
}
}
}
from __future__ import print_function
import win32com.client as com
pp = com.Dispatch('PowerPoint.Application')
print(pp.Presentations[0].Slides[8].Shapes[0].TextFrame.TextRange.Text)