Run vba word macro using AHK Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
asad41163
Posts: 268
Joined: 29 Jul 2014, 14:31

Run vba word macro using AHK

18 Sep 2017, 18:04

Hi guys,
I have this vba word macro:

Code: Select all

Sub correct()
' correct Macro
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "p "
        .Forward = True
        .Wrap = wdFindContinue
    End With
    Selection.Find.Execute
    Selection.TypeText Text:="o "
End Sub
and I want to run this macro using AHK, like this:

Code: Select all

 #Include Com.ahk            ; COM
        RunMSWordMacro(correct)
        {
            COM_Init()
            Word := COM_GetActiveObject("Word.Application")
            COM_Invoke(Word, "Run", "!"correct)
            COM_Release(Word)
            COM_Term()
        }
        ;    the hotkey is 1
    1::R	unMSWordMacro("correct")

but it dose not work.
Please, any body do it for me.
Best Regards
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Run vba word macro using AHK  Topic is solved

18 Sep 2017, 18:49

Hi, asad41163,
Now Com.ahk is outdated, AHK supports COM natively.
Try:

Code: Select all

1:: RunMSWordMacro("correct")

RunMSWordMacro(name)  {
   GetWord().Run(name)
}

GetWord()  {
   if !hwnd := WinActive("ahk_class OpusApp")
      hwnd := WinExist("ahk_class OpusApp")
   ControlGet, ControlHwnd, Hwnd,, _WwG1, ahk_id %hwnd%
   Return AccObjectFromWindow(ControlHwnd, -16).Application
}

AccObjectFromWindow(hWnd, idObject = 0)
{
   static IID_IDispatch   := "{00020400-0000-0000-C000-000000000046}"
        , IID_IAccessible := "{618736e0-3c3d-11cf-810c-00aa00389b71}"
        , OBJID_NATIVEOM := 0xFFFFFFF0, VT_DISPATCH := 9, h := DllCall("LoadLibrary", Str, "oleacc", Ptr)
        
   VarSetCapacity(IID, 16), idObject &= 0xFFFFFFFF
   DllCall("ole32\CLSIDFromString", Str, idObject = OBJID_NATIVEOM ? IID_IDispatch : IID_IAccessible, Ptr, &IID)
   if DllCall("oleacc\AccessibleObjectFromWindow", Ptr, hWnd, UInt, idObject, Ptr, &IID, PtrP, pAcc) = 0
      Return ComObjEnwrap(VT_DISPATCH, pAcc, 1)
}
asad41163
Posts: 268
Joined: 29 Jul 2014, 14:31

Re: Run vba word macro using AHK

19 Sep 2017, 16:24

Hi, Joined
thank you very much
I'll try it right away
Best Regards
asad41163
Posts: 268
Joined: 29 Jul 2014, 14:31

Re: Run vba word macro using AHK

20 Sep 2017, 19:47

Hi, Joined
thank you very much
it's work very good
thank you very much

Best Regards
tachmonite37
Posts: 1
Joined: 14 Dec 2019, 13:58

Re: Run vba word macro using AHK

22 Jul 2020, 07:13

teadrinker wrote:
18 Sep 2017, 18:49

Code: Select all

#SingleInstance, Force ; Allow only one running instance of the script
#Persistent ; Keep the script permanently running until terminated
#NoEnv ; Avoid checking empty variables for environment variables
#Warn ; Enable warnings to assist with detecting common errors
;#NoTrayIcon ; Disable the tray icon of the script
;SendMode, Input ; Method for sending keystrokes and mouse clicks
;SetWorkingDir, %A_ScriptDir% ; Set the working directory of the script
;SetBatchLines, -1 ; Run the script at maximum speed
;SetWinDelay, -1 ; The delay to occur after modifying a window
;SetControlDelay, -1 ; The delay to occur after modifying a control
;OnExit, ExitSub ; Run a subroutine or function when the script exits
;~Pause::Suspend         			; Pause and suspend AHK script


1:: RunMSWordMacro("StepLeftSentence")   ;Change to name of VBA Macro here StepLeftSentence  or StepRightSentence  or  CursorToStartOfSentence

RunMSWordMacro(StepLeftSentence)  {        ;Change to name of VBA Macro here StepLeftSentence  or StepRightSentence  or  CursorToStartOfSentence
	GetWord().Run(StepLeftSentence)       ;Change to name of VBA Macro here StepLeftSentence  or StepRightSentence  or  CursorToStartOfSentence
	}

    GetWord()  {
       if !hwnd := WinActive("ahk_class OpusApp")
          hwnd := WinExist("ahk_class OpusApp")
       ControlGet, ControlHwnd, Hwnd,, _WwG1, ahk_id %hwnd%
       Return AccObjectFromWindow(ControlHwnd, -16).Application
    }

    AccObjectFromWindow(hWnd, idObject = 0)
	{
		static IID_IDispatch   := "{00020400-0000-0000-C000-000000000046}"
            , IID_IAccessible := "{618736e0-3c3d-11cf-810c-00aa00389b71}"
            , OBJID_NATIVEOM := 0xFFFFFFF0, VT_DISPATCH := 9, h := DllCall("LoadLibrary", Str, "oleacc", Ptr)
		
		VarSetCapacity(IID, 16), idObject &= 0xFFFFFFFF
		DllCall("ole32\CLSIDFromString", Str, idObject = OBJID_NATIVEOM ? IID_IDispatch : IID_IAccessible, Ptr, &IID)
		if DllCall("oleacc\AccessibleObjectFromWindow", Ptr, hWnd, UInt, idObject, Ptr, &IID, PtrP, pAcc) = 0
			Return ComObjEnwrap(VT_DISPATCH, pAcc, 1)
	}
[Mod edit: [code][/code] tags added.]

==================================
Hi. I tried using this script adapted from your script but I get this error (in the attachment). Can you please help me to fix it?
Attachments
AHK-StepLeftSentence_Macro (3).png
AHK-StepLeftSentence_Macro (3).png (12.88 KiB) Viewed 995 times
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Run vba word macro using AHK

22 Jul 2020, 07:47

Hi @tachmonite37
It's not an error, it's a warning. You can just remove #Warn from your script, and warnings won't appear.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 199 guests