0

I have an issue with setting value in Excel with UI Automation.

I can get all the cells and their information howver I am unable to set the AutomationElement by using method ValuePattern.SetValue()

The code will be executed and no error will be thrown however the cell wil not take the new value into account.

Same behaviour with the tool Accessibility Insights for Microsoft, I can get all the information but editing does not work, furthermore the tool will display sucesss but the value will not be changed.

I have tried launching Excel as an administrator but this does not change anything.

Updating the value with Insight Accessibility

Here's the code :

AutomationElement root = AutomationElement.RootElement;

var excelRoot = FindInTree(
            TreeWalker.ControlViewWalker.GetFirstChild(root),
            "WorkInstructionExcel.xlsx"
        );
Condition conditions = new AndCondition(
            new PropertyCondition(AutomationElement.IsEnabledProperty, true),
            new PropertyCondition(AutomationElement.ControlTypeProperty,
                ControlType.DataItem)
        );

AutomationElementCollection gridCollection =
            excelRoot.FindAll(TreeScope.Descendants, conditions);

var enumerator = gridCollection.GetEnumerator();

AutomationElement elem;
        
while (enumerator.MoveNext())
{
     elem = (AutomationElement)enumerator.Current;
     if (elem.Current.Name == "C2")
     {
        if (elem.TryGetCurrentPattern(ValuePattern.Pattern, out object valuePattern))
           if (((ValuePattern)valuePattern).Current.IsReadOnly == false)
                    {
                        elem.SetFocus();
                       
                        ((ValuePattern)valuePattern).SetValue("test");
                    }
            }
        }
3

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.