Locked Word Document Control Automation in WPF Application

  • Tuesday, February 28, 2012 4:46 AM
     
     

    Hi All,

    A word document control is embedded in WPF application and its content is visible for editing and reading. I am trying to automate this control but facing issue as elucidated below

    I am making use of UI Automation API to automate WPF control in Coded UI Test and code to automate above scenario as follows

    AutomationElement applicationElement=<application Element assingned here>

    AutomationElement wordElement=applicationElement.FindFirst(TreeScope.Descendants, new System.Windows.Automation.PropertyCondition(AutomationElement.NameProperty,"Microsoft Word Document"));

    Object objWordCtrl=wordElement;

    Microsoft.office.Interop.Word.Document worddoc=(Microsoft.office.interop.Word.Document) wordElement;

    But I am getting expection error saying unable to cast object of type 'System.Windows.Automation.AutomationElement' to type 'Microsoft.office.Interop.word.Document'

    Can you please help me on above error so that i can make use of word API to act on word document.

All Replies

  • Tuesday, February 28, 2012 6:44 AM
     
     

    Any help would be really appreciated.

  • Thursday, March 01, 2012 3:58 AM
    Moderator
     
     Proposed

    Hi,

    In Coded UI test, Excel is not supported officially. However, fortunately product engineering team wrote an extension for this scenario. You may get some idea from this: http://blogs.msdn.com/b/gautamg/archive/2010/01/05/3-introducing-sample-excel-extension.aspx

    On the other hand, embedded Excel in WPF could be difference case. Do you use a third party Excel WPF control? You can also consulted control provider in this case.

    Regards.


    Forrest Guo | MSDN Community Support | Feedback to us


  • Thursday, March 01, 2012 8:01 AM
     
     

    Thanks Forrest.

    Here a word document is embedded in WPF app. User can view content and edit to document. I want to know how can object casting work as shown in above code.

  • Friday, March 02, 2012 8:32 AM
    Moderator
     
     

    Vijay,

    I don't think we can make such type conversion. Not only because wordElement of type AutomationElement is different from Word object model Document interface, AutomationElement doesn't implement the interface.

    AutomationElement class represents a UI Automation element in the UI Automation tree, and contains values used as identifiers by UI Automation client applications http://msdn.microsoft.com/en-us/library/system.windows.automation.automationelement.aspx

    Word Document interface: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.document(v=office.14).aspx

    And, which kind of control do you use to host the word document?

    Word UI doesn't support UIA automation framework, you may turn to MSAA for help. A MSAA sample can be found at: http://code.msdn.microsoft.com/CSOfficeRibbonAccessibility-cd7fe67f

    Regards,


    Forrest Guo | MSDN Community Support | Feedback to us

  • Monday, March 05, 2012 1:22 AM
    Moderator
     
     

    Vijay,

    Have you got the answer of this question?

    Regards,


    Forrest Guo | MSDN Community Support | Feedback to us

  • Monday, March 05, 2012 8:51 AM
     
     

    Thanks Forrest.

    I will check what type of control used to host word document and get back to you.