[英]When I try to use UI Automation for PowerPoint 2013, I can only get the first character/word when I use RangeFromPoint
該代碼適用於Word和Outlook但PowerPoint失敗,因為只有文本框的第一個字符或第一個字被選中。 這是一個錯誤嗎? 有沒有解決方法? 在PowerPoint 2013中的簡單PowerPoint幻燈片上試試這個。
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 2016中修復的?
當我查看范圍的邊界框時,它始終是整個元素,而不是選定的單詞。 這可能是RangeToPoint無法正常工作的問題的一部分。
原帖在MSDN上發布但沒有回復......
更新。 如果我使用
text = printRange(range, text);
while (range.Move(TextUnit.Word, 1) > 0)
{
text += Environment.NewLine;
text = printRange(range, text);
}
我明白了
此行為可能是由於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的Text Explorer工具時,我得到了相同的結果工具。 下圖顯示了文本資源管理器報告,當鼠標懸停在其中一個單詞上時,從PowerPoint 2013返回的文本是文本框中的整個文本。
(我應該補充說下面的測試代碼完全可以工作,我認為當前的顯示縮放設置需要達到100%。我沒有添加代碼來解釋其他一些活動的擴展。)
我不知道這是否已在PowerPoint 2016中修復,我將嘗試調查並告知您。
謝謝,
家伙
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) + "\"";
}
}
}
我只能建議Python代碼獲取幻燈片的標題文本(例如)。 對不起,我沒時間在C#上重寫它。 您可以使用PowerPoint.Application
COM對象和PowerPoint.Application
自動化的MSDN示例 。
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.