0

I have this portal where I need to click on 1 to 8 ( as shown in image ) and put "Ok" on position 9. I managed to do mouse click with the code shown below. However, could not manage to enter text "Ok" on 9th position. Please help. Thanks in advance.

Image

The requirement is to add the text Ok on the specific position everytime

        Public Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
        Public Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
        Public Const MOUSEEVENTF_LEFTDOWN = &H2
        Public Const MOUSEEVENTF_LEFTUP = &H4
        Public Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
        Public Const MOUSEEVENTF_RIGHTUP As Long = &H10
        'Declare sleep
        Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

        Sub EnterValue()
          SetCursorPos 250, 250
          mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
          mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
          Sleep 50

          '1
          SetCursorPos 475, 350
          mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
          mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
          Sleep 50
        '
          '2
          SetCursorPos 550, 350
          mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
          mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
          Sleep 50

          '3
          SetCursorPos 625, 350
          mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
          mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
          Sleep 50

          '4
          SetCursorPos 700, 350
          mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
          mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
          Sleep 50

          '5
          SetCursorPos 775, 350
          mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
          mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
          Sleep 50

          '6
          SetCursorPos 850, 350
          mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
          mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
          Sleep 50

          '7
          SetCursorPos 925, 350
          mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
          mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
          Sleep 50

          '8
          SetCursorPos 1000, 350
          mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
          mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
          Sleep 50


        End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
  • The method you use is very unreliable and can break easily (a simple change of screen resolution would probably break it already). Check out [Browser automation in Excel VBA using Selenium](https://codingislove.com/browser-automation-in-excel-selenium/) for a Chrome solution or [Automate Internet Explorer (IE) Using VBA](https://www.automateexcel.com/vba/automate-internet-explorer-ie-using/) for a IE solution to do it properly. Simulating mouse clicks is probalby (sorry to say that) the worst solution to do that. – Pᴇʜ Jun 09 '20 at 10:17
  • Additional to @Pᴇʜ I'd recommend not using web browsers at all if your situation allows, use background http objects for data sending, scraping etc. https://stackoverflow.com/questions/158633/how-can-i-send-an-http-post-request-to-a-server-from-excel-using-vba – jamheadart Jun 09 '20 at 10:28
  • @PEH .....thanks for your suggestion. Let me explore this. – Dipal Shah Jun 10 '20 at 14:47

1 Answers1

0

Can you try something like:

Application.SendKeys("Ok")

Tomasz Paluch
  • 322
  • 1
  • 5
  • 10
  • Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted. – Tomasz Paluch Jun 10 '20 at 14:47