0

I am trying to create VBA tool, that copy-paste some data from excel workbook to some internal company application, which we are using via browser. The program should "click" several buttons and checkboxes, and paste cells values into textboxes like this presented on following screens:

SS1

SS2

SC3

Unfortunately, there are several restrictions:

  • This site is not working on MS Internet Explorer. I can use it only by FireFox.
  • Thanks to security rules in may company, I am not able to install any software. So, I can't use Selenium (requires installing add-on), nor do this task with R or Python.
  • Clicking Tab key is not working, so I can't just SendKeys "{TAB}" multiple times.

Have someone got any idea how I can resolve this?

5
  • For clearyfication: You want to copy some data from Excel into windows clipboard and after that you want to paste it into a website? Commented Dec 23, 2016 at 10:19
  • Yes, but before, I need to click some buttons and mark a checkbox. I have to loop whole this process x times. Commented Dec 23, 2016 at 10:28
  • I'm not sure that you can control a mouse in general with vba. Even when you able doing that,, when you leave the focus from Excel, there is not code the computer can execute. VBA works only in a sandbox of Excel and not ouside. Commented Dec 23, 2016 at 10:37
  • @reporter As far as I know, sending click events works, even outside Excel, as long as the Excel file stays open in the background. But sending click events is risky to say the least. What if the website changes, or your screen resolution changes, or the browser opens in non-fullscreen mode once... Limak, they want you to dig for oil but only allow you to use a spoon. Try reasoning with your boss/IT, and ask for Selenium or some proper tool. Commented Dec 23, 2016 at 12:15
  • @reporter as OT, yes, you can control the mouse with excel vba Commented Dec 23, 2016 at 14:56

1 Answer 1

1

Workaround:
As you state the only solution (to my knowledge since Firefox does not have an api) would be clicking and waiting a few -is there any window that may pop up to verify that you are done?-.



Steps:
1.Set your firefox window in a constant position and then get the coordinates
You would need to declare each coordinate, you may use the following code to help you to calculate the coordinate like so:

Dim MyPointAPI As POINTAPI
Private Type POINTAPI
  X As Long
  Y As Long
End Type
Private Declare Function GETCURSORPOS Lib "user32" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Public Sub MouseCursorGetOurXY()
'this was taken online quite a while ago, left everything as copied from the original source
    Dim L As Long
Application.Wait (Now() + TimeValue("00:00:02"))
    L = GETCURSORPOS(MyPointAPI) 'get our cursor position first
    MsgBox CStr(MyPointAPI.X) & ", " & CStr(MyPointAPI.Y) 'displays cursor X Y coordinates
End Sub

enter image description here


2.Set your code with these coordinates:

 Public Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Public Declare 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
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Sub Click_Windows()
    Const CheckBoxCoordX As Long = 455
    Const CheckBoxCoordY As Long = 300
    Const InputBoxCoordX As Long = 155
    Const InputBoxCoordY As Long = 255
    Const FireFoxWindowTitle As String = "WebPage Title"
    Dim do_Mydata As DataObject
    Dim lHnd As Long
    Dim i As Long
    lHnd = FindWindow(vbNullString, FireFoxWindowTitle)
    If lHnd > 0 Then 'This means that your program is in memory ' 1. If lHnd > 0
    'Now that You've already checked your app is running then ..
    AppActivate FireFoxWindowTitle
    Else ' 1. If lHnd > 0
    i = 0
    Do Until lHnd > 0
    lHnd = FindWindow(vbNullString, FireFoxWindowTitle)
    If i < 5 Then ' 2. If i < 5
    Application.Wait (Now() + TimeValue("00:00:01"))
    i = i + 1
    Else ' 2. If i < 5
    MsgBox ("It seems that the webpage is not opened!")
    End
    End If ' 2. If i < 5
    Loop
    End If ' 1. If lHnd > 0
        'code to click only
       SetCursorPos CheckBoxCoordX, CheckBoxCoordY
       mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
       mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
       Application.Wait (Now() + TimeValue("00:00:01"))
       'move to the input box and paste data from excel
       SetCursorPos InputBoxCoordX, InputBoxCoordY
       mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
       mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
        Set do_Mydata = New DataObject
        With do_Mydata
            .SetText Range("A1").Value
            .PutInClipboard
        End With
        SendKeys "^v", True
        Set do_Mydata = Nothing
        Application.CutCopyMode = False

    End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I have tried it when @reporter mentioned mouse control. It looks like the only solution for now, but I will keep looking for the more appropriate one.
Saddly, there's no other way to do it without an api for firefox/chrome

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.