Skip to content

Instantly share code, notes, and snippets.

@burque505
Last active March 16, 2019 18:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burque505/b1df40a200727e8aaf1cf4b66627a827 to your computer and use it in GitHub Desktop.
Save burque505/b1df40a200727e8aaf1cf4b66627a827 to your computer and use it in GitHub Desktop.
Automate Word with AutoHotkey module and red-lang
; You will need AutoHotkey.dll for this to work.
; It can be downloaded from https://github.com/HotKeyIt/ahkdll (source)
; or http://hotkeyit.github.io/v2/ for binaries (v1 on same page)
; Only tested with v1 - you will of course need MS Word on your machine.
; Use 32-bit AutoHotkey dll only!
; If red can't find it in your script directory, please put
; the dll in SysWOW64. (By all means try including it in your script
; directory first, if you like.)
; YOU MAY NEED TO REMOVE THESE PRE-HEADER COMMENTS BEFORE COMPILING
; You may have issues with libRed.dll, depending on whether
; it was built as [cdecl] or [stdlib], but I am NOT SURE ABOUT THIS YET.
; Edit: It's been reported that you may need to remove '#u16' from the beginning of your strings,
; depending on whether your AutoHotkey.dll is ANSI or Unicode.
; EDIT: It's reported now that the ANSI dll must not have '#u16', and the Unicode dll requires it.
; Thanks to Toomas Voglaid for pointing out inconsistencies in the
; preliminary Msgbox.
Red/System []
#import
[
"kernel32.dll" stdcall [
Sleep: "Sleep" [
dwMilliseconds [integer!]
]
]
; New library to load
"AutoHotkey.dll" cdecl [
RunScript: "ahktextdll" [
code [c-string!]
]
Ready: "ahkReady" [ return: [logic!]
]
Exec: "ahkexec" [
code2 [c-string!]
]
]
]
cmdstr: #u16 {
wd := ComObjCreate("Word.Application")
wd.Visible := true
doc := wd.Documents.Add()
wd.Selection.TypeText("Hello, World!")
return
!Escape::ExitApp
}
; per @giesse, @9214, #u16 is required.
; THANKS!!!!!!!!!!!!!
RunScript cmdstr
Exec #u16 "Msgbox, Click OK to start Word automation.`r`nPress Alt-Escape to quit.`r`nBoth red and AHK will exit.`r`nWord will remain open.`r`nYou can modify this behavior in code."
while [Ready]
[
Sleep 100
]
print {Done}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment