Results 1 to 12 of 12

Thread: How to Send Keys from my VB5 App to another App's open Web Page

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2010
    Location
    Lake Gaston, NC
    Posts
    453

    How to Send Keys from my VB5 App to another App's open Web Page

    My user will open the desired Web Page(www.TD-Digital.com) for testing purposes
    My user will then "Restore" my App which is running minimized in the task bar, then Click a button in my app that will send keys to the open Web page.

    I have tried two different methods to accomplish this and neither works.
    The first Scripting method, crashes with an error 424 on the "Set MyShell = WScript.CreateObject("WScript.Shell")
    " line.

    The second method runs but the keys do not appear in the Web page.
    What am I doing wrong on both cases?

    Code:
    Const KEYEVENTF_KEYDOWN As Long = 0
    Const KEYEVENTF_KEYUP As Long = 2
    Const VK_SHIFT As Long = 16
    Const VK_TAB = &H9
    
    Private Response, WinTitle As String, WinClass As String, WinHwnd As Long
    Private ReturnValue, nVK As Long
    '****
    
    Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal _
       cChar As Byte) As Integer
    
    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 FindWindow Lib "user32" Alias "FindWindowA" _
                (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    Private Declare Function SetActiveWindow Lib "user32" (ByVal hwnd As Long) As Long
    '***************************************
    
    
    Private Sub Form_Load()
    Left = (Screen.Width - Width) / 2
    Top = ((Screen.Height - Height) / 2)
    End Sub
    '***********************************************
    
    
    Private Sub cmdTransfer_Click()
    Dim MyShell
    Me.Hide
    
    Set MyShell = WScript.CreateObject("WScript.Shell")
    MyShell.AppActivate "Richmond Times Dispatch - Windows Internet Explorer provided by CenturyLink"
    MyShell.SendKeys "Tom", wait
    MyShell.SendKeys "{Tab}", wait
    MyShell.SendKeys "Smith", wait
    GoTo ExitProc
    
    
    WinHwnd = FindWindow(vbNullString, "Richmond Times Dispatch - Windows internet Explorer provided by CenturyLink")
    
    If WinHwnd > 0 Then
        'AppActivate WinTitle
        ReturnValue = SetActiveWindow(WinHwnd)
    Else
        MsgBox "Window not found - Reenter"
        GoTo ExitProc
    End If
    
    sName = "Smith"
    Call SendAKey(sName)
    
    keybd_event VK_TAB, 0, KEYEVENTF_KEYDOWN, 0
    keybd_event VK_TAB, 0, KEYEVENTF_KEYUP, 0
    
    sName = "Man"
    Call SendAKey(sName)
    
    
    ExitProc:
    ReturnValue = Shell("C:\Program Files\DeiTransferData\DeiTransferData.exe", 0)
    End
    End Sub
    '*********************************************
    
    Private Sub SendAKey(name)
    Dim Start As Integer, NameLen As Integer, sKey As String
    Start = 1
    NameLen = Len(name)
    
    If NameLen > 0 Then
        Do While Start <= NameLen
            sKey = Mid(name, Start, 1)
            nVK = VkKeyScan(Asc(sKey)) And &HFF
            ' Capitalize all letters
            If sKey Like "[a-z]" Or sKey Like "[A-Z]" Then
                keybd_event VK_SHIFT, 0, KEYEVENTF_KEYDOWN, 0
                keybd_event nVK, 0, KEYEVENTF_KEYDOWN, 0
                keybd_event VK_SHIFT, 0, KEYEVENTF_KEYUP, 0
            ' DON'T Capitalize these
            ElseIf sKey = "," Or sKey = "." Or sKey = "'" Or sKey = "-" _
                Or sKey Like "[1-9]" Then
                keybd_event nVK, 0, KEYEVENTF_KEYDOWN, 0
            ' Capitalize Shift Number Keys
            ElseIf UCase(sKey) = sKey Then
                keybd_event VK_SHIFT, 0, KEYEVENTF_KEYDOWN, 0
                keybd_event nVK, 0, KEYEVENTF_KEYDOWN, 0
                keybd_event VK_SHIFT, 0, KEYEVENTF_KEYUP, 0
            Else
                keybd_event nVK, 0, KEYEVENTF_KEYDOWN, 0
            End If
            
            keybd_event nVK, 0, KEYEVENTF_KEYUP, 0
        
            Start = Start + 1
        Loop
    Else
        MsgBox "Enter some Valid input", vbExclamation
    End If
    End Sub

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: How to Send Keys from my VB5 App to another App's open Web Page

    Have you tried the simple built in SendKeys ...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2010
    Location
    Lake Gaston, NC
    Posts
    453

    Re: How to Send Keys from my VB5 App to another App's open Web Page

    I believe you have to first make the web page active and then send the keys. The reason I can't actually use SendKeys for real is that they don't work for Vista.

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to Send Keys from my VB5 App to another App's open Web Page

    What do you mean by another app's open web page? Is the other app using WebBrowser to show a Web Page.
    That should have nothing to do with sending keys to the other app.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2010
    Location
    Lake Gaston, NC
    Posts
    453

    Re: How to Send Keys from my VB5 App to another App's open Web Page

    The other app is a 3rd party web site my Customer uses in addition to my pgm.

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to Send Keys from my VB5 App to another App's open Web Page

    Maybe you should not use the term "other app's" open web page. It's a web page from a web site or a personal computer that is running an HTTP server.

    So what I fail to understand is why do you want to send keys (that only works for apps on the same machine and I think although not positive that the apps have to be Windows applications). To my knowledge you cannot send keys to an app on another computer (your customer in this case). If your customer is running a web site then are you trying to send data to the app itself or the web page it is running? I'm not clear on this.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2010
    Location
    Lake Gaston, NC
    Posts
    453

    Re: How to Send Keys from my VB5 App to another App's open Web Page

    My Customer is using one computer where my app resides.

    He displays the 3rd party web page on this computer and then he restores my app from the task bar, clicks the button on my app which is supposed to populate fields with data from my pgm into fields on the 3rd partyweb page.

  8. #8
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: How to Send Keys from my VB5 App to another App's open Web Page

    so you need to write to a webpage? If so, SendKeys is not your option. I would use the Webpage's Document to write to it's fields.

  9. #9
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: How to Send Keys from my VB5 App to another App's open Web Page

    I haven't studied your code YET, however sometimes a 'focus' problem can be solved by just hiding your app.
    EG Your program is running (minimized into Taskbar)
    User has 'focus' (it is active) on a web page.
    User maximizes (or whatever) your app from the Taskbar, and clicks your button.
    What your code should do, is immediately hide itself (not sure if minimizing is enough to lose 'focus').
    With a bit of luck the 'focus will be back on the web page, when your code sends the keys.
    (Some DoEvents may be required in your code)
    That should work if user has XP. As I recall Win7 does not return 'focus'
    There are also API ways to force 'focus' back to where we want it.

    Rob
    PS Don't tell me that Sendkeys does not work in Vista, or "I will get involved" (as I joke to my friends)

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2010
    Location
    Lake Gaston, NC
    Posts
    453

    Re: How to Send Keys from my VB5 App to another App's open Web Page

    Solved my own problem, but I had to do it with VB2012 code shown below.

    Code:
    Imports System
    Imports System.Windows.Forms
    Imports System.Security.Permissions
    
    <PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
    Public Class FormXfer
        Public Sub New()
            InitializeComponent()
        End Sub
        '*************************************************************************
    
        Private Sub FormXfer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToParent()
        End Sub
        '*****************************************************************************
    
        Private Sub ButtonXfer_Click(sender As Object, e As EventArgs) Handles ButtonXfer.Click
            Dim Response
            Try
                Beep()
                Response = MsgBox("Have you Clicked the first Input Field?", vbYesNo + vbExclamation)
                If Response = vbYes Then
                    Me.WindowState = FormWindowState.Minimized
                    My.Computer.Keyboard.SendKeys("John", True)
                    My.Computer.Keyboard.SendKeys("{TAB}", True)
                    My.Computer.Keyboard.SendKeys("Paul", True)
                    My.Computer.Keyboard.SendKeys("{TAB}", True)
                    My.Computer.Keyboard.SendKeys("Jones", True)
                    My.Computer.Keyboard.SendKeys("{TAB}", True)
                    My.Computer.Keyboard.SendKeys("April", True)
                    My.Computer.Keyboard.SendKeys("{TAB}", True)
                    My.Computer.Keyboard.SendKeys("15", True)
                    My.Computer.Keyboard.SendKeys("{TAB}", True)
                    My.Computer.Keyboard.SendKeys("1980", True)
                Else
                    Beep()
                    MsgBox("Click the first Input field now")
                End If
            Catch ex As Exception
                MsgBox("Error in ""TransferData Form"" : " + ex.Message)
            End Try
        End Sub
    End Class
    ''**********************************************************************************

  11. #11
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: How to Send Keys from my VB5 App to another App's open Web Page

    I was going to give you a hard time for not upgrading from VB5 to VB6 (as VB6 has better ADO interaction, and a couple of other small but great improvements).
    I am glad I didn't, as I would have felt guilty for the rest of my life, if I thought I was the one that drove you to VB.NOT

  12. #12
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: How to Send Keys from my VB5 App to another App's open Web Page

    I would stay away from the Sendkeys, is a user clicks anywhere else it wont work.
    Since i cannot login, here is an example of how to login. (not tested, since i cannot log in)

    Code:
    Private Sub Command1_Click()
    Dim IE_Window As Object
      
      Set IE_Window = CreateObject("InternetExplorer.Application")
        
        With IE_Window
          .Silent = True
          .Navigate "http://richmondtimesdispatch.va.ee.newsmemory.com/eebrowser/frame/check.7370/init/default.php?pSetup=richmondtimesdispatch&amp;notEmpty=/eeLayout/richmondtimesdispatch/images/zzz_howto.htm&amp;type="
          .Visible = True
        End With
        
        Do While IE_Window.Busy Or IE_Window.readystate <> 4
          DoEvents
        Loop
    
        With IE_Window.document.getElementById("pageId").contentWindow.document.Login
          .Username.Value = "John" 'Username Here"
          .Password.Value = "Doe" 'Password Here"
          .childNodes.Item(3).childNodes.Item(1).childNodes.Item(10).childNodes.Item(3).childNodes.Item(0).Click "Click Login"
        End With
        
    End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width