I ended up using SendKeys as the Document object in notepad does not support the "SetValue" ValuePattern.
private void NotepadInsertButton_Click(object sender, EventArgs e)
{
string windowTitle = "Untitled - Notepad";
AutomationElement result = getRootElement(windowTitle);
if (result == null)
{
throw new ArgumentException();
}
Condition conditions = new AndCondition(
new PropertyCondition(AutomationElement.IsEnabledProperty, true),
new PropertyCondition(AutomationElement.ControlTypeProperty,
ControlType.Document)
);
AutomationElementCollection elementCollection =
result.FindAll(TreeScope.Children, conditions);
foreach (AutomationElement x in elementCollection)
{
InsertTextUsingUIAutomation(x, "Hello World!");
}
}
private AutomationElement getRootElement(string windowTitle)
{
AutomationElement root = AutomationElement.RootElement;
AutomationElement result = null;
foreach (AutomationElement window in root.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window)))
{
try
{
if (window.Current.Name.Contains(windowTitle) && window.Current.IsKeyboardFocusable)
{
result = window;
break;
}
}
catch (Exception e)
{
}
}
return result;
}
Condition conditions = new AndCondition(
new PropertyCondition(AutomationElement.IsEnabledProperty, true),
new PropertyCondition(AutomationElement.ControlTypeProperty,
ControlType.Document)
);
AutomationElementCollection elementCollection =
result.FindAll(TreeScope.Children, conditions);
foreach (AutomationElement x in elementCollection)
{
InsertTextUsingUIAutomation(x, "Hello World!");
}
private void InsertTextUsingUIAutomation(AutomationElement element, string value)
{
try
{
if (value == null)
throw new ArgumentNullException(
"String parameter must not be null.");
if (element == null)
throw new ArgumentNullException(
"AutomationElement parameter must not be null");
if (!element.Current.IsEnabled)
{
throw new InvalidOperationException(
"The control with an AutomationID of "
+ element.Current.AutomationId.ToString()
+ " is not enabled.\n\n");
}
if (!element.Current.IsKeyboardFocusable)
{
throw new InvalidOperationException(
"The control with an AutomationID of "
+ element.Current.AutomationId.ToString()
+ "is read-only.\n\n");
}
object valuePattern = null;
if (!element.TryGetCurrentPattern(
ValuePattern.Pattern, out valuePattern))
{
richTextBox1.AppendText("The control with an AutomationID of ");
richTextBox1.AppendText(element.Current.AutomationId.ToString());
richTextBox1.AppendText(" does not support ValuePattern.");
richTextBox1.AppendText(" Using keyboard input.\n");
element.SetFocus();
Thread.Sleep(100);
SendKeys.SendWait("^{HOME}");
SendKeys.SendWait("^+{END}");
SendKeys.SendWait("{DEL}");
SendKeys.SendWait(value);
}
else
{
richTextBox1.AppendText("The control with an AutomationID of ");
richTextBox1.AppendText(element.Current.AutomationId.ToString());
richTextBox1.AppendText((" supports ValuePattern."));
richTextBox1.AppendText(" Using ValuePattern.SetValue().\n");
element.SetFocus();
((ValuePattern)valuePattern).SetValue(value);
}
}
catch (ArgumentNullException exc)
{
richTextBox1.AppendText(exc.Message);
}
catch (InvalidOperationException exc)
{
richTextBox1.AppendText(exc.Message);
}
finally
{
richTextBox1.AppendText("");
}
}