| View previous topic :: View next topic |
| Author |
Message |
Learning one
Joined: 04 Apr 2009 Posts: 1000 Location: Croatia
|
Posted: Fri Oct 23, 2009 12:28 pm Post subject: [Solved] Run MS Word macro by AHK |
|
|
Question:
How to run specific MS Word macro directly by AHK?
Answer:
Use this function:
AutoHotkey_L - recommended
| Code: | RunMSWordMacro(MacroName) { ; for AHK_L
oWord := ComObjActive("Word.Application")
oWord.Run(MacroName)
}
/* ; Example:
1::RunMSWordMacro("MyMacro") ; press 1 to run macro called MyMacro
*/ |
AutoHotkey
| Code: | #Include Com.ahk ; by Sean http://www.autohotkey.com/forum/viewtopic.php?t=22923
RunMSWordMacro(MacroName) {
COM_Init()
Word := COM_GetActiveObject("Word.Application")
COM_Invoke(Word, "Run", "!" MacroName)
COM_Release(Word)
COM_Term()
}
/* ; Example:
1::RunMSWordMacro("MyMacro") ; press 1 to run macro called MyMacro
*/ |
Original post:
Hi,
does anyone know how to run specific MS Word macro directly by AHK?
Macro code Visual basic!
| Code: |
Sub Black()
'
' Black Macro
' Macro recorded 22/10/2009
'
Selection.WholeStory
With Selection.Font
.Color = 15395562
End With
Selection.MoveLeft Unit:=wdCharacter, Count:=1
ActiveDocument.Background.Fill.ForeColor.RGB = RGB(0, 0, 0)
ActiveDocument.Background.Fill.Visible = msoTrue
ActiveDocument.Background.Fill.Solid
End Sub
|
Although I don't know Visual basic, I can see that this macro is subroutine
called "Black". How can I directly run this subroutine by AHK? (Withouth commands like MouseClick, ControlClick or WinMenuSelectItem - I know how to do this on that way )
Last edited by Learning one on Thu Oct 21, 2010 7:26 pm; edited 9 times in total |
|
| Back to top |
|
 |
jethrow
Joined: 24 May 2009 Posts: 1900 Location: Iowa, USA
|
Posted: Fri Oct 23, 2009 12:48 pm Post subject: |
|
|
Check out COM - specifically COM_L (see my signature). If I get a chance later, I will look at this more. _________________
- in case I forgot to smile
Basic Webpage Controls
COM Object Reference |
|
| Back to top |
|
 |
jethrow
Joined: 24 May 2009 Posts: 1900 Location: Iowa, USA
|
Posted: Fri Oct 23, 2009 5:15 pm Post subject: |
|
|
Ok, could someone tell me why the following doesn't work (AHK_L & COM_L): | Code: | ControlGet, hWnd, hWnd,, _WwG1, ahk_class OpusApp ; get Active Word Document
word := COM_AccessibleObjectFromWindow(hWnd,-16).Application ; get Active Word Document
word.Selection.WholeStory() ; <-- works fine
word.Selection.Font.Bold := True ; "True" <-- neither work
; Note - IsObject(word.Selection.Font) returns True |
EDIT - Sean updated COM_L so that this now works. _________________
- in case I forgot to smile
Basic Webpage Controls
COM Object Reference
Last edited by jethrow on Sat Oct 24, 2009 2:02 pm; edited 3 times in total |
|
| Back to top |
|
 |
Learning one
Joined: 04 Apr 2009 Posts: 1000 Location: Croatia
|
Posted: Fri Oct 23, 2009 10:22 pm Post subject: |
|
|
Thank's for your interest, jethrow.
I checked out COM functions about 2 weeks ago, and concluded that this is too complicated for me (for now) ...
I'm using AHK. Can this be done in AHK (not AHK_L)?
For now, I'll stick to my old way ( WinMenuSelectItem, ControlClick...) |
|
| Back to top |
|
 |
jethrow
Joined: 24 May 2009 Posts: 1900 Location: Iowa, USA
|
Posted: Sat Oct 24, 2009 8:23 am Post subject: |
|
|
| Learning one wrote: | | I'm using AHK. Can this be done in AHK (not AHK_L)? |
The thing is AHK_L supports objects, whereas AHK currently doesn't. It can be done with AHK, but the syntax will be more difficult. _________________
- in case I forgot to smile
Basic Webpage Controls
COM Object Reference |
|
| Back to top |
|
 |
txquestor
Joined: 22 Aug 2009 Posts: 294
|
Posted: Sat Oct 24, 2009 11:06 pm Post subject: |
|
|
Try this, I use it in Excel & Word
| Code: |
; MSWord_RunVBAmacro
COM_Init()
Word := COM_GetActiveObject("Word.Application")
name := "'" COM_Invoke(Word, "ActiveDocument.name") "'!Macro1" ; where Macro1 is the name of the existing MSWord Macro
COM_Invoke(Word, "Run", name)
COM_Release(name)
COM_release(Word)
COM_Term()
|
Have Fun,  _________________
"Man's quest for knowledge is an expanding series whose limit is infinity" |
|
| Back to top |
|
 |
Learning one
Joined: 04 Apr 2009 Posts: 1000 Location: Croatia
|
Posted: Sun Oct 25, 2009 1:57 pm Post subject: |
|
|
Thank's txquestor.
This works on my computer:
| Code: |
; Run MSWord Macro
MacroName = Black ; name of the existing MSWord Macro to run. Modify...
COM_Init()
Word := COM_GetActiveObject("Word.Application")
COM_Invoke(Word, "Run", "!"MacroName)
COM_Release("!"MacroName)
COM_Release("Word")
COM_Term()
|
|
|
| Back to top |
|
 |
Greener than a newb Guest
|
Posted: Mon Oct 26, 2009 2:19 pm Post subject: SNAP!!! That sh*t is fast |
|
|
Wow - I am liking this!!! Saves a lot of time
EXCELLENT!!!
Thanks
 |
|
| Back to top |
|
 |
Learning one
Joined: 04 Apr 2009 Posts: 1000 Location: Croatia
|
Posted: Mon Oct 26, 2009 8:18 pm Post subject: |
|
|
| First post updated... |
|
| Back to top |
|
 |
nostroke
Joined: 22 Oct 2009 Posts: 95
|
Posted: Mon Oct 26, 2009 8:22 pm Post subject: |
|
|
| Learning one wrote: | | First post updated... |
Holey macro  |
|
| Back to top |
|
 |
Greener than a newb Guest
|
Posted: Tue Oct 27, 2009 12:02 pm Post subject: |
|
|
Could you add the code to open a specific document (ie Test.rtf)
and then running the macro?
Would this be able to handle 2 macros?
| Code: | | "C:\Program Files\Microsoft Office\Office\WINWORD.EXE" test.rtf /q /n /mFilePrintDefault /mFileExit |
|
|
| Back to top |
|
 |
Greener than newb Guest
|
Posted: Tue Oct 27, 2009 3:37 pm Post subject: |
|
|
NVM - I got it:
Directory = L:\Today
[/code]Filename := " " Directory "\Test.rtf"
MacroName = PrtAF10851 ; name of the existing MSWord Macro
COM_Init()
Word := COM_CreateObject("Word.Application")
com_invoke(Word,"Visible=",False)
Docs := COM_Invoke(Word,"Documents")
COM_Invoke(Docs,"Open",Filename)
COM_Invoke(Word, "Run", "!"MacroName)
COM_Release("!"MacroName)
COM_Release(Docs)
COM_Release(Word)
COM_Term()
Sleep 2000 |
|
| Back to top |
|
 |
Greener than newb Guest
|
Posted: Tue Oct 27, 2009 3:41 pm Post subject: |
|
|
NVM - I got it:
| Code: |
Directory = L:\Today
Filename := " " Directory "\Test.rtf" ;Path of File
MacroName = PrtAF10851 ; name of the existing MSWord Macro
COM_Init()
Word := COM_CreateObject("Word.Application")
COM_Invoke(Word,"Visible=",False)
Docs := COM_Invoke(Word,"Documents")
COM_Invoke(Docs,"Open",Filename)
COM_Invoke(Word, "Run", "!"MacroName)
COM_Release("!"MacroName)
COM_Release(Docs)
COM_Release(Word)
COM_Term()
Sleep 2000 |
|
|
| Back to top |
|
 |
|