VBA : send a right-click to the application ?

Chris Davison

MrExcel MVP
Joined
Feb 15, 2002
Messages
1,790
is it possible to send a single mouse right-click to the application via VBA ?

I'm using sendkeys to get an accounting package to load up and print images but halfway through the procedure requires a mouse right-click on the image itself (no keyboard shortcut unfortunately), which I can't replicate via sendkeys

how would I therefore send a right-click ?

(help has "application.doubleclick" which looked promising, alas, nothing on "application.rightclick"....)

thanks
Chris
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
thanks Jacob, unfortunately though, it doesn't....

it does indeed rightclick in excel if I try that combo, but doesn't do anything in the accounting application when I sendkeys

(it's also not doing something else it ought to, so I'll check if there's a setting somewhere that's been switched off)

thanks
Chris
 
Upvote 0
...do an API call

reminds me of "call in the A-Team" lolol

yeah, our tecchie guy suspects maybe the citrix environment is corrupting the sendkeys info, so I'll wait for them to check that out and see what happens

(cool Lion avatar, he's still got blood round his mouth !)

cheers
Chris
 
Upvote 0
Hehe yeah call in Sergeant Bosco 'B.A.' Baracus. :lol:

I took that picture myself while I was on holiday in Namibia last year. The lion was having a good old chow down although his prey wasnt enjoying it much. Fabulous country with brilliant wildlife and a must see destination if you ever get to Africa. :-D

Hope you get the sendkeys sussed.

:beerchug:
 
Upvote 0
Chris - - can you post your relevant code; it may be a mouse button related piece of code you are needing.
 
Upvote 0
cheers Tom :-D

here's the code (which works, although the right click would make it perfect by enabling me to bring up a submenu before I print) :


Code:
Sub test_printing_images()

    'do first image

    'activate Agresso
    AppActivate ("Agresso"), True
    Application.StatusBar = "Loading 1 of " & Range(ThisWorkbook.Worksheets("test").Range("H3"), ThisWorkbook.Worksheets("test").Range("H65536").End(xlUp)).Rows.Count + 1 & " : " & ThisWorkbook.Worksheets("test").Range("H2").Value
    'load enquiry
    Application.SendKeys ("~")
    'TAB to clear-all button
    Application.SendKeys ("{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}")
    'hit clear-all button
    Application.SendKeys ("~")
    'TAB to transaction number indicator
    Application.SendKeys ("{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}")
    'select transaction number indicator
    Application.SendKeys ("{F2} {TAB}")
    'TAB to transaction number
    Application.SendKeys ("{TAB}{TAB}{TAB}")
    'insert transaction number
    Application.SendKeys (ThisWorkbook.Worksheets("test").Range("H2").Text)
    'TAB out
    Application.SendKeys ("{TAB}")
    'hit enter to run query
    Application.SendKeys ("~")
    'wait 5 seconds
    Application.Wait (Now + TimeValue("00:00:05"))
    'bring up tools menu
    Application.SendKeys ("%t")
    'select show invoice image
    Application.SendKeys ("~")
    'wait 5 seconds
    Application.Wait (Now + TimeValue("00:00:05"))
    
    'perform right-click
    'code needed to perform right click
    
    'select file menu
    Application.SendKeys ("%f")
    'print
    Application.SendKeys ("p")
    'wait 5 seconds
    Application.Wait (Now + TimeValue("00:00:05"))
    
    'do remaining images
    
    For Each cell In Range(ThisWorkbook.Worksheets("test").Range("H3"), ThisWorkbook.Worksheets("test").Range("H65536").End(xlUp))
        Application.StatusBar = "Loading " & cell.Row - 2 & " of " & Range(ThisWorkbook.Worksheets("test").Range("H3"), ThisWorkbook.Worksheets("test").Range("H65536").End(xlUp)).Rows.Count + 1 & " : " & ThisWorkbook.Worksheets("test").Range("H2").Value
        'load enquiry
        Application.SendKeys ("{F7}")
        'insert transaction number
        Application.SendKeys (cell.Text)
        'TAB out
        Application.SendKeys ("{TAB}")
        'hit enter to run query
        Application.SendKeys ("~")
        'wait 5 seconds
        Application.Wait (Now + TimeValue("00:00:05"))
    
        'perform right-click
        'code needed to perform right click
    
        'select file menu
        Application.SendKeys ("%f")
        'print
        Application.SendKeys ("p")
        Application.Wait (Now + TimeValue("00:00:05"))
    Next cell
    
    Application.StatusBar = False

End Sub
 
Upvote 0
Hi Chris

Here's some code to send a right click (well two actually, a down and an up). I don't have agresso \o/ so I used Outlook but the principal is identical.

You need to move the mouse to the right place first of all, so this needs to be consistent. If this is a problem, you might consider maximizing the window. Not sure what window in agresso you need, if the item is always in the same place etc etc, so this might get you started but not be the final product.

Code:
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal Y As Long) As Long
Private 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)
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
Private Const MOUSEEVENTF_RIGHTUP As Long = &H10


Sub foo()
    'activate the window
    AppActivate "Inbox - Microsoft Outlook"
    'move the cursor where you need it, I guessed at 200,200
    SetCursorPos 200, 200
    'send a down event
    mouse_event MOUSEEVENTF_RIGHTDOWN, 0&, 0&, 0&, 0&
    'and an up
    mouse_event MOUSEEVENTF_RIGHTUP, 0&, 0&, 0&, 0&
End Sub

HTH
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,082
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top