vbs mouse event for IE window

SRG041808
SRG041808 used Ask the Experts™
on
I need to create a vbs script that can click a specific spot inside an Internet Explorer window...

navigating to the site works fine


I have attached my current code....


dim Window
set Window = CreateObject("InternetExplorer.Application")
PWORD="blahblahblah"
Window.RegisterAsBrowser = True
Window.Navigate("websitegoeshere"& PWORD)
Window.MenuBar = True
Window.ToolBar = False
Window.AddressBar = True
Window.StatusBar = True
Window.FullScreen = False
Window.Resizable = True
Window.Visible = True
Window.Width = 800
Window.Height = 600 
 
'below here is where it gets all messed up or appears to not click anything
 
Dim dest_x 
Dim dest_y 
 
dest_x = 18078
dest_y = 18078
 
' Move the mouse to its final destination and click it.
    mouse_event _
        MOUSEEVENTF_ABSOLUTE + _
        MOUSEEVENTF_MOVE + _
        MOUSEEVENTF_LEFTDOWN + _
        MOUSEEVENTF_LEFTUP, _
        dest_x, dest_y, 0, 0

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
mouse_event() is a Windows API and therefore cannot be used directly in a VBScript (.vbs)...sorry dude.
See: http://msdn.microsoft.com/en-us/library/ms646260(VS.85).aspx

As far as know there isn't any way for a VBScript to do this without help from a third party DLL.

Author

Commented:
if VBS can't do it what programming language could?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Tons of them...

VB6
VB.Net
C#
C++
Delphi
JAVA
AutoIT
etc...
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Author

Commented:
nice lol .... figures that i would pick the one that wouldn't work.....


so how would I write what i have so far in java? or tell java to run my vbs script?


 That language isn't my strong point but I will learn it after this......

If you can do that you get the points!

thanks
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
I haven't code in JAVA in literally years (maybe a decade?)...

I could do it in VB.Net.  You can get VB.Net Express for free from Microsoft:
http://www.microsoft.com/express/vb/Default.aspx

That would allow you to write the code and produce an EXE.  If you turn off all the type checking then you could use almost the code you have verbatim...

The coord for mouse_event() are in Pixels and you have:

    dest_x = 18078
    dest_y = 18078

You either have a massive screen or multiple monitors?

Author

Commented:
I have multiple monitors.....  

i was told on the web that a screen coordinates went from 0,0 to 65535,65535 corner to corner in pixels... i was trying to get any outcome i could and was aiming for center screen....


vb.net sounds good... if it is close to vbscript i can work with that

Author

Commented:
I installed microsoft visual basic 2008 express.....i have tried adding to my code or making new and i get a ton of errors..... guess ill have to work with it
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Untested...but doesn't produce any compiler errors:
Option Strict Off
Option Explicit Off
Module Module1
 
    Public Const MOUSEEVENTF_MOVE As Integer = &H1
    Public Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
    Public Const MOUSEEVENTF_LEFTUP As Integer = &H4
    Public Const MOUSEEVENTF_ABSOLUTE As Integer = &H8000
    Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
 
    Public Sub Main()
        Dim Window
        Window = CreateObject("InternetExplorer.Application")
        PWORD = "blahblahblah"
        Window.RegisterAsBrowser = True
        Window.Navigate("websitegoeshere" & PWORD)
        Window.MenuBar = True
        Window.ToolBar = False
        Window.AddressBar = True
        Window.StatusBar = True
        Window.FullScreen = False
        Window.Resizable = True
        Window.Visible = True
        Window.Width = 800
        Window.Height = 600
 
        Dim dest_x
        Dim dest_y
 
        dest_x = 18078
        dest_y = 18078
 
        ' Move the mouse to its final destination and click it.
        mouse_event( _
            MOUSEEVENTF_ABSOLUTE + _
            MOUSEEVENTF_MOVE + _
            MOUSEEVENTF_LEFTDOWN + _
            MOUSEEVENTF_LEFTUP, _
            dest_x, dest_y, 0, 0)
    End Sub
 
End Module

Open in new window

Author

Commented:
excellent!  thanks
Hi,

I got to the point of positioning - for which i used the following code:

'Code: VBScript
'Function: To position pointer just above the status line where it shows a button to
'                 click on show content
'date: 11/15/2012

Set Excel = Wscript.CreateObject("Excel.Application")

GetMessagePos = excel.ExecuteExcel4Macro("CALL(""user32"",""GetMessagePos"",""J"")")

x = CLng("&H" & Right(Hex(GetMessagePos), 4))
y = CLng("&H" & Left(Hex(GetMessagePos), (Len(Hex(GetMessagePos)) - 4)))

x = 1030            'Setting cursor to top left for X
y = 740              'setting cursor to top left for y

LeftClick = TRUE

Excel.ExecuteExcel4Macro("CALL(""user32"",""SetCursorPos"",""JJJ""," & x & "," & y & "," & LeftClick & ")")

But the Catch is - the LeftClick does not work...NOW if you take out the 'LeftClick' and just
replace as below

Excel.ExecuteExcel4Macro("CALL(""user32"",""SetCursorPos"",""JJJ""," & x & "," & y & "," &  ")")

It positions correctly & works great.

After the above step

do a IE focus OR app focus.

as shown below:

objIE.document.focus()

myShell.sendkeys ("{enter}")

and that would do the trick..


PS: If you find out about the LeftClick ...let me know :)

Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial