Writing standalone GUI applications with Visual Basic.NET is easy. What isn’t so easy is using a VB.NET application to modify the behavior of some other running application. If the other application happens to be MS Word, MS Excel, or MS Access, you can use Visual Basic for Applications (VBA). Otherwise, unless the other application exposes it’s own API, you’re typically stuck trying to use so-called “hooks“, which will quickly get you under the hood of the Windows OS to a depth where, trust me, you don’t want to go unless you’re prepared to spend a lot of time poring over Microsoft’s incomprehensible documentation trying to figure out how to build something that won’t ever work quite right no matter what you do, and will have a high likelihood of not working at all after the next Windows update.

Here’s a better way: use Autohotkey — the Autohotkey folks have already done all the tedious under the hood stuff, all we have to do is figure out how to trigger Autohotkey and tell it what we want to do. That turns out to be easy: in the VB app, use Sendkeys.Send to simulate a keystroke. For example, Sendkeys.Send(“+^%6”) simulates pressing control-shift-alt 6. Then just detect the (simulated) keypress in an Autohotkey script with something like

^+!6:: (here add whatever functionality you need to make the target app do what you want to do)

Since Autohotkey already has functionality for controlling just about anything you can think of in other applications — pressing buttons, selecting menu items, positioning the mouse cursor, entering key sequences — that means you can do the same from a VB app.  Easy.

Leave a Reply

Your email address will not be published. Required fields are marked *