locked
Declaring IUIAutomationelement in VB.net RRS feed

  • Question

  • I want to use IUIAutomation in VB.net , I already added the reference dll file "UIAutomationClient.dll" but I still cant define any IUIAutomation elements 

    I am already importing the below

    Imports Microsoft.Office
    Imports System.Runtime.InteropServices
    Imports Scripting
    Imports SHDocVw
    Imports mshtml
    Please help what else I should declare so I can get the below objects defined :
     Dim UIAutomation As IUIAutomation
            Dim eBrowser As IUIAutomationElement, eFNB As IUIAutomationElement, e As IUIAutomationElement
            Dim InvokePattern As IUIAutomationInvokePattern


    Friday, June 2, 2017 8:39 PM

Answers

  • I am trying to automate IE webpage and handle the SaveAs notification bar to download an export by invoking the notification bar , I tried it in VBA and it works perfectly , but I am stuck with VB.net
    I added references as you showed me , still nothing the same problem

     No,  it will not fix the problem with declaring a IUIAutomationElement interface,  you will have to implement the code a little differently to use what i showed you but,  it accesses the same interface behind the scenes.  You can do the same things as with the native interface but,  just in a little different way.  You can read the msdn documents for it which i gave a link to in my post with the example.

    If you say it can`t be done then i`ll try it

    • Marked as answer by Mofadl Saturday, June 3, 2017 3:24 AM
    Saturday, June 3, 2017 2:11 AM

All replies

  • Hello,

    An interface is used in this fashion which means you implement the interface on your class 

    Public Class Class1 : Implements IKaren
    
        Public Property Succcess As Boolean Implements IKaren.Succcess
            Get
    
            End Get
            Set(value As Boolean)
    
            End Set
        End Property
    
        Public Sub RunJob() Implements IKaren.RunJob
    
        End Sub
    End Class

    So you would do

    Public Class Class1 : Implements IUIAutomation
    
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Friday, June 2, 2017 8:50 PM
  • Thanks Karen for your fast response ,but  still with this syntax IUIAutomation is underlined red as undefined, so I am asking if there is any other library I forgot to add or there is another import I should do  
    Friday, June 2, 2017 9:04 PM
  • You will need to add a reference to your project, one of the following

    Also, the following is from here.

    Developer audience

    The Windows Automation API is designed for experienced C/C++ developers. In general, developers need a moderate level of understanding about Component Object Model (COM) objects and interfaces, Unicode, and Windows API programming.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, June 2, 2017 9:16 PM
  • You must add a reference to uiautomationcore.dll

    then

    Imports UIAutomationClient

    and you can do

     Dim uiA As IUIAutomation = New CUIAutomation

    Friday, June 2, 2017 10:13 PM
  • You must add a reference to uiautomationcore.dll

    then

    Imports UIAutomationClient

    and you can do

     Dim uiA As IUIAutomation = New CUIAutomation

    You don't new up an Interface. Let's say I have an Interface IKaren in a class project, compile and give the DLL to you. I would indicate to implement the Interface e.g.

    Public Class Class1 : Implements IKaren

    Visual Studio will complain, hover over IKaren and Visual Studio will ask to implement it, we then get

    Public Class Class1 : Implements IKaren
    
        Public Sub SayGoodBye() Implements IKaren.SayGoodBye
            Throw New NotImplementedException()
        End Sub
    
        Public Sub SayHello() Implements IKaren.SayHello
            Throw New NotImplementedException()
        End Sub
    End Class

    In each case of  Throw New NotImplementedException() you would write your code to the subscribed events.

    You need to do the same for IUIAutomation.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, June 2, 2017 10:24 PM

  • You don't new up an Interface.

    you do it since VB 6

    See https://bugs.chromium.org/p/chromium/issues/detail?id=271227

    Friday, June 2, 2017 10:54 PM
  • You must add a reference to uiautomationcore.dll

    then

    Imports UIAutomationClient

    and you can do

     Dim uiA As IUIAutomation = New CUIAutomation

    You don't new up an Interface. Let's say I have an Interface IKaren in a class project, compile and give the DLL to you. I would indicate to implement the Interface e.g.

    Public Class Class1 : Implements IKaren

    It is like in C++ or C#

    Tested on Windows 10, VS 2015 with Notepad :

      Dim proc As Process = proc.Start(New ProcessStartInfo("notepad"))
            proc.WaitForInputIdle()
            Dim hWnd As IntPtr = proc.MainWindowHandle
    
            Dim uiA As IUIAutomation = New CUIAutomation
            Dim elementMain As IUIAutomationElement = uiA.ElementFromHandle(hWnd)
    
            Dim condition As IUIAutomationCondition
            condition = uiA.CreatePropertyCondition(30012, "Edit")
    
            Dim childElement As IUIAutomationElement = elementMain.FindFirst(4, condition)
            ' Test properties
            Dim sProviderDescription = childElement.CurrentProviderDescription
            Dim sClassName = childElement.CurrentClassName

    • Edited by Castorix31 Saturday, June 3, 2017 12:08 AM
    Friday, June 2, 2017 11:35 PM
  •  Perhaps if you explained what it is you are actually trying to do,  it could be done without even creating the Interface.  For example,  maybe you are trying to click a button or another control element on a 3rd party application.  Below is a very simplified example of invoking a click on a button that is on another application.

     I first created a simple application that had a Button on it named "Button1" which shows a MessageBox when it is clicked.  I then saved it as "My UIAutomation Test Form".

     Then i created another application with the code below to use UIAutomation to invoke a click on the button of the first application as shown in the animated gif below the code.

    Imports System.Windows.Automation ' add a reference to (UIAutomationClient) and (UIAutomationTypes)

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Dim p As Process = Process.GetProcessesByName("My UIAutomation Test Form").FirstOrDefault 'get the Process by its Process Name
            If p IsNot Nothing Then
                ' get the main window Element
                Dim parentElement As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)

                ' define a lookup condition for a Button with AutomationID = "Button1"
                Dim propCondition1 As New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button)
                Dim propCondition2 As New PropertyCondition(AutomationElement.AutomationIdProperty, "Button1")
                Dim conditionGetButton As Condition = New AndCondition(propCondition1, propCondition2)

                ' get the Button element
                Dim ButtonElement As AutomationElement = parentElement.FindFirst(TreeScope.Children, conditionGetButton)

                Dim objPattern As Object = Nothing
                'try getting the invoke pattern of the button element
                If ButtonElement.TryGetCurrentPattern(InvokePattern.Pattern, objPattern) Then
                    Dim invPattern As InvokePattern = DirectCast(objPattern, InvokePattern) 'cast the pattern object to an InvokePattern
                    invPattern.Invoke() 'invoke the pattern / click the button
                End If
            End If
        End Sub
    End Class
     

     

     

     You may also want to look through the links below....

    Invoke a Control Using UI Automation

    UI Automation Overview


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Saturday, June 3, 2017 12:59 AM
    Saturday, June 3, 2017 12:13 AM
  • I dont have uiautomationcore.dll in Visual Studio 2017, I also searched for it its not there, what should I do ?
    Saturday, June 3, 2017 12:45 AM
  • I dont have uiautomationcore.dll in Visual Studio 2017, I also searched for it its not there, what should I do ?

     Perhaps try the approach i showed in my last post.  All you need to do is add the references i commented about at the top of the code,  shown below in the Reference Manager dialog window.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Saturday, June 3, 2017 1:02 AM
    Saturday, June 3, 2017 1:01 AM
  • I am trying to automate IE webpage and handle the SaveAs notification bar to download an export by invoking the notification bar , I tried it in VBA and it works perfectly , but I am stuck with VB.net
    I added references as you showed me , still nothing the same problem
    Saturday, June 3, 2017 1:51 AM
  • I am trying to automate IE webpage and handle the SaveAs notification bar to download an export by invoking the notification bar , I tried it in VBA and it works perfectly , but I am stuck with VB.net
    I added references as you showed me , still nothing the same problem

     No,  it will not fix the problem with declaring a IUIAutomationElement interface,  you will have to implement the code a little differently to use what i showed you but,  it accesses the same interface behind the scenes.  You can do the same things as with the native interface but,  just in a little different way.  You can read the msdn documents for it which i gave a link to in my post with the example.

    If you say it can`t be done then i`ll try it

    • Marked as answer by Mofadl Saturday, June 3, 2017 3:24 AM
    Saturday, June 3, 2017 2:11 AM
  • Thanks  IronRazerz for your advice!  , I will definitely try with native interface 
    Saturday, June 3, 2017 3:26 AM