Autohotkey.Dll, AutoHotkey.Interop, MS Office com objects Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Autohotkey.Dll, AutoHotkey.Interop, MS Office com objects

26 Jan 2017, 18:22

Many thanks in advance to all who can help. The code below works, but doesn't do what I want.
My goal is to create a Com object either from within an AutoHotKey.dll.ahktextdll script, or to create it with one of the AutoHotkey.Interop functions, or to at least operate on the Com object created in the public declarations of the .Net program.

I have been able to get Autohotkey.dll and Autohotkey.Interop to operate on the .Net-created object (not in this code), but the screen starts flashing, and all of a sudden I have twenty to fifty instances of Word! Not the desired result, to be sure. (I have not yet successfully created a "Word.Application" object from within code called by Autohotkey.dll or Autohotkey.Interop.)

The main reason I want to do this using AHK is that .Net's SendKeys() performance is abysmal, and AHK's is tremendous. No messing around with low-level keyboard hooks, the work's been done for me already. Or at least that's how I see it...

Note the "Word.Application" has to be created as a public object for the late binding I'm trying to do.
(https://msdn.microsoft.com/en-us/library/3xxe951d.aspx). I haven't tried reflection yet.

Also, it was interesting to see that the .Interop version's msgbox caption is "Autohotkey.DLL", while the call from ahktextdll to msgbox resulted in a caption (the program here is AHK-LB2) of "AHK-LB2-vshost.exe".

Again, many thanks. The code below is from a VB.Net Winform with one button.

Imports AutoHotkey.Interop
Public Class Form1

Public oWord = CreateObject("Word.Application")
Public iOp = AutoHotkeyEngine.Instance
Public ahk = CreateObject("AutoHotkey.Script")
Private Sub btnWord_Click(sender As Object, e As EventArgs) Handles btnWord.Click
SendAHKeys()
End Sub

Sub SendAHKeys()
ahk.ahktextdll("msgbox, The script called by the DLL works. Caption is program name + vshost.")
iOp.ExecRaw("msgbox, And the Autohotkey.Interop call also works. Note the caption(Autohotkey.Dll)")
oWord.Documents.Add
oWord.Visible = True
'how can I use AutoHotKey.dll to send text to
'oWord once it's open and visible?
'I can use the Script "ahk" to run Word, and send
'text to it, but what I want to do is operate on it as a
'Com object. Thanks in advance!

End Sub
End Class
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Autohotkey.Dll, AutoHotkey.Interop, MS Office com objects  Topic is solved

27 Jan 2017, 01:14

:superhappy: Success!!!!!!!!!! Add a reference to AutoHotkey.Interop to the VB program.
My problem was I was trying to use AHK escape characters within the AutoHotkey.Script. Dumb. As you can see, the "Word.Application" needed to be
""Word.Application"" for Visual Studio to see it.

Imports AutoHotkey.Interop
Public Class Form1
Public ahkWord As Object
Public iOp = AutoHotkeyEngine.Instance
Public ahk = CreateObject("AutoHotkey.Script")
Private Sub btnWord_Click(sender As Object, e As EventArgs) Handles btnWord.Click
SendAHKeys()
End Sub
Sub SendAHKeys()
ahk.ahktextdll("msgbox, The script called by the DLL works. Caption is program name + vshost.")
iOp.ExecRaw("msgbox, And the Autohotkey.Interop call also works. Note the caption(Autohotkey.Dll)")
ahk.ahktextdll("ahkWord := ComObjCreate(""Word.Application"")" & vbLf &
"ahkWord.Documents.Add" & vbLf &
"ahkWord.Visible := 1, ahkWord.Activate" & vbLf &
"Send This is AHK calling from Visual Basic.Net. Anybody home?")
End Sub
End Class

p.s. Script below is with the ahkWord object declared within the subroutine. Although this appears to violate the late binding rule about members declared as public, it works, although IntelliSense complains the variable "ahkWord" is not being used! See https://msdn.microsoft.com/en-us/library/0tcf61s1.aspx, Early and Late Binding. A note at the bottom of the page says:
"Late binding can only be used to access type members that are declared as Public. Accessing members declared as Friend or Protected Friend results in a run-time error." Hmmm ... :?:
Could this be working because ahk (the AutoHotkey.Script) is a public member? That's the only thing I can think of.

Imports AutoHotkey.Interop
Public Class Form1
'Public ahkWord As Object
Public iOp = AutoHotkeyEngine.Instance
Public ahk = CreateObject("AutoHotkey.Script")
Private Sub btnWord_Click(sender As Object, e As EventArgs) Handles btnWord.Click
SendAHKeys()
End Sub
Sub SendAHKeys()
Dim ahkWord As Object
ahk.ahktextdll("msgbox, The script called by the DLL works. Caption is program name + vshost.")
iOp.ExecRaw("msgbox, And the Autohotkey.Interop call also works. Note the caption(Autohotkey.Dll)")
ahk.ahktextdll("ahkWord := ComObjCreate(""Word.Application"")" & vbLf &
"ahkWord.Documents.Add" & vbLf &
"ahkWord.Visible := 1, ahkWord.Activate" & vbLf &
"Send This is AHK calling from Visual Basic.Net. Anybody home?")
End Sub
End Class

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: bobstoner289, peter_ahk, Spawnova and 345 guests