Consulting

Results 1 to 14 of 14

Thread: Accessing a control in another (non-Office) application

  1. #1

    Accessing a control in another (non-Office) application

    Hi

    I have got an application written in Delphi, that has a Form1, with some textboxes and buttons. I need to fill the textboxes with values from an Excel worksheet, and then click the OK button on Form1 to initiate a certain calculation, and some other things the application does.

    I would like to automate this process, that is, to fill the textboxes on Form1 via macro, and even have the macro simulate a clicking on the OK button, if possible. Then wait until the application is ready, then start again with the next set of values.

    I don't know how to do it. Can anyone give me a start? Spiced with some explanation, perhaps?

    Thank you for your time, in advance.
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  2. #2
    Nobody?
    I don't ask for a full code, I only ask for some directions.

    Please help.
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Why do you need to do this from Excel?

    Surely if the application has been created you should be doing it from there.

    I've only briefly used Delphi, free copy from a magazine a few years back, but I'm pretty sure it should have the capability to pull data from Excel.

  4. #4
    Quote Originally Posted by Norie
    Why do you need to do this from Excel?

    Surely if the application has been created you should be doing it from there.
    Because I only use the application, but I didn't create it. I guess it could've been equipped with an interface towards Excel, but it wasn't so I have to use it as it is.

    I've only briefly used Delphi, free copy from a magazine a few years back, but I'm pretty sure it should have the capability to pull data from Excel.
    Yes, as far as I know, Delphi has such capability, but the source code of the application is unavailable, too. Not to mention that, even if I had it, I'm not sure I could implement this "Pull Data From Excel" part
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  5. #5
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    What does this application actually do?

    Must you use it?

  6. #6
    Yes, I must use it. It is part of my job. It calculates Motoric Octane Number, Density and a few other physical properties of gasoline, based on chromatographic data, using an unknown algorithm.
    The application pulls the data from the chromatograph, online. It also has a primitive GUI for modelling gasolines. That's what I called Form1. When I've got a real gasoline analysis ready, I can type in imaginary modifications, to see what would happen if o-xylene content were twice as large, normal octane was 3.05% etc. That is when I should be able pull data from an Excel worksheet.
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  7. #7
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    if you knew the shortcut keys, you could probably use the SendKeys method to do what you wanted
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  8. #8
    I'm counting on Sendkeys to send the data over, but first I have to set the focus to a particular TextBox. I can not simply send over some TAB's to move the focus to the desired TextBox because, to do this, I need to obtain the initial position of the focus so that I can know how many TAB's I need.

    The need to learn the initial position of the focus puts me back to the start: how to identify a textbox in another application?
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  9. #9
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,288
    Location
    If your Delphi application is open and visible on the screen (no other window is active) you can let excel get a handle to the name of your delphi window. I'll see what I have in my collection (I use it to interact with our bookkeeping program).

  10. #10
    Quote Originally Posted by Charlize
    If your Delphi application is open and visible on the screen (no other window is active) you can let excel get a handle to the name of your delphi window. I'll see what I have in my collection (I use it to interact with our bookkeeping program).

    Great, thanks
    I'm awaiting this precious part of your collection
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  11. #11
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,288
    Location
    Found it. I've fetched it from somewhere at the web. I hope it gives you some idea's.[VBA]Option Explicit
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" _
    (ByVal cChar As Byte) As Integer
    Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" _
    (ByVal wCode As Long, ByVal wMapType As Long) As Long
    Private Const KEYEVENTF_KEYUP = &H2
    Private Const vbKeyShift = &H10
    Private Const vbKeyCtrl = &H11
    Private Const vbKeyAlt = &H12
    Const VK_RETURN As Integer = &HD
    Const SW_HIDE = 0
    Private Sub PrintNotepad()
    Dim hWnd As Long, Forewin As Long
    Forewin = GetForegroundWindow()
    ' Change this to match your caption
    hWnd = FindHwnd("Untitled - Notepad")
    If hWnd = 0 Then Exit Sub
    SetForegroundWindow hWnd
    Call Send_Keys("this is some text", hWnd)
    keybd_event vbKeyCtrl, 0, 0, 0
    Call Send_Keys("p", hWnd)
    keybd_event vbKeyCtrl, 0, KEYEVENTF_KEYUP, 0
    ' Send Return
    keybd_event VK_RETURN, 0, 0, 0
    keybd_event VK_RETURN, 0, KEYEVENTF_KEYUP, 0
    SetForegroundWindow Forewin
    End Sub
    Public Sub Send_Keys(KeysToSend As String, WindowToSendTo As Long)
    Dim ShiftDown As Boolean, AltDown As Boolean, ControlDown As Boolean
    Dim intCount As Integer
    Dim mScan As Long
    Dim a As Integer
    Dim mVK As Long

    If KeysToSend = "" Then Exit Sub
    If WindowToSendTo = 0 Then Exit Sub

    For a = 1 To Len(KeysToSend)

    mVK = VkKeyScan(Asc(Mid(KeysToSend, a, 1)))
    mScan = MapVirtualKey(mVK, 0)

    ShiftDown = (mVK And &H100)
    ControlDown = (mVK And &H200)
    AltDown = (mVK And &H400)

    mVK = mVK And &HFF


    ' Do While GetForegroundWindow() <> WindowToSendTo And intCount < 20
    ' DoEvents
    ' intCount = intCount + 1
    ' SetForegroundWindow WindowToSendTo
    ' Loop
    '
    ' If GetForegroundWindow() <> WindowToSendTo Then Exit Sub

    If ShiftDown Then keybd_event &H10, 0, 0, 0
    If ControlDown And &H200 Then keybd_event &H11, 0, 0, 0
    If AltDown And &H400 Then keybd_event &H12, 0, 0, 0

    keybd_event mVK, mScan, 0, 0

    If ShiftDown Then keybd_event vbKeyShift, 0, KEYEVENTF_KEYUP, 0
    If ControlDown Then keybd_event vbKeyCtrl, 0, KEYEVENTF_KEYUP, 0
    If AltDown Then keybd_event vbKeyAlt, 0, KEYEVENTF_KEYUP, 0

    Next a

    ' SetForegroundWindow Application.hWnd

    End Sub
    Private Function FindHwnd(strCaption As String) As Long
    'Returns the handle of the specified window
    FindHwnd = FindWindow(vbNullString, strCaption)
    End Function[/VBA]

  12. #12
    Thanks, Charlize.

    Yes it gives me some ideas. Your code is a great, and living, example of calling external functions, which is a quite new area for me.

    However, your code adresses only an application, and not one of the application's controls, and the latter is what I need right now.

    Following the path you showed me, I started my research for the proper API function, but have found nothing relevant yet. I'll leave this thread on for a while, maybe someone, who can point out the right method, drops by.

    Thanks again,

    Jimmy
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If the control has its own handle, you may be able to access that in a similar manner. Get hold of a window message spy tool, such as Winspector or Spy++ and see if you can get the window class.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,288
    Location
    This is one of the resources I use if I want to see how long I can try things out before my (test)computer crashes : http://allapi.mentalis.org/tips/tips1.shtml

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •