Read data from form

Posted by Superhuman on Stack Overflow See other posts from Stack Overflow or by Superhuman
Published on 2011-01-12T15:49:14Z Indexed on 2011/01/12 15:54 UTC
Read the original article Hit count: 152

Filed under:

This is a strange question, I've never tried to do this before.

I have a repetitive process requiring that I copy and paste data from text boxes in one program into another program for further processing. I'd like to automate this process using VB .NET. The application from which the data is gathered isn't mine, so I don't have ActiveX-like access to its controls.

How would you write an application to gain access to a form from another application, to be able to find the controls on the form, and gather the values from them?

Just experimenting, I've used the following code. This resulted in only the name of the form to which this code belongs. It didn't find the names of any other forms I have open, and I have a lot open to choose from. This is frustrating because it's only step one of what I'll need to do to make my life easier...

Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As CallBack, ByVal lParam As Integer) As Integer
Public Delegate Function CallBack(ByVal hwnd As IntPtr, ByVal lParam As IntPtr) As Boolean


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim cb As New CallBack(AddressOf MyCallBack)
    EnumWindows(cb, 8)
End Sub

Public Function MyCallBack(ByVal hwnd As Long, ByVal lparam As Long) As Boolean
    Dim frm As System.Windows.Forms.Control
    frm = System.Windows.Forms.Form.FromHandle(hwnd)
    If frm Is Nothing Then Return True

    If frm.Text <> "" Then
        TextBox1.Text += frm.Text & ", "
    End If

    Return True
End Function

Does anyone have a recommendation?

Thanks, SH

© Stack Overflow or respective owner

Related posts about winapi