AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Solved] Run MS Word macro by AHK

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Learning one



Joined: 04 Apr 2009
Posts: 1000
Location: Croatia

PostPosted: Fri Oct 23, 2009 12:28 pm    Post subject: [Solved] Run MS Word macro by AHK Reply with quote

Question Question:
How to run specific MS Word macro directly by AHK?

Wink 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 Cool)


Last edited by Learning one on Thu Oct 21, 2010 7:26 pm; edited 9 times in total
Back to top
View user's profile Send private message Visit poster's website
jethrow



Joined: 24 May 2009
Posts: 1900
Location: Iowa, USA

PostPosted: Fri Oct 23, 2009 12:48 pm    Post subject: Reply with quote

Check out COM - specifically COM_L (see my signature). If I get a chance later, I will look at this more.
_________________
Very Happy - in case I forgot to smile
Basic Webpage Controls
COM Object Reference
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
jethrow



Joined: 24 May 2009
Posts: 1900
Location: Iowa, USA

PostPosted: Fri Oct 23, 2009 5:15 pm    Post subject: Reply with quote

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.
_________________
Very Happy - 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
View user's profile Send private message Visit poster's website Yahoo Messenger
Learning one



Joined: 04 Apr 2009
Posts: 1000
Location: Croatia

PostPosted: Fri Oct 23, 2009 10:22 pm    Post subject: Reply with quote

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)Rolling Eyes...

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
View user's profile Send private message Visit poster's website
jethrow



Joined: 24 May 2009
Posts: 1900
Location: Iowa, USA

PostPosted: Sat Oct 24, 2009 8:23 am    Post subject: Reply with quote

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.
_________________
Very Happy - in case I forgot to smile
Basic Webpage Controls
COM Object Reference
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
txquestor



Joined: 22 Aug 2009
Posts: 294

PostPosted: Sat Oct 24, 2009 11:06 pm    Post subject: Reply with quote

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, Surprised
_________________

"Man's quest for knowledge is an expanding series whose limit is infinity"
Back to top
View user's profile Send private message
Learning one



Joined: 04 Apr 2009
Posts: 1000
Location: Croatia

PostPosted: Sun Oct 25, 2009 1:57 pm    Post subject: Reply with quote

Thank's txquestor.

This works on my computer: Smile
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
View user's profile Send private message Visit poster's website
Greener than a newb
Guest





PostPosted: Mon Oct 26, 2009 2:19 pm    Post subject: SNAP!!! That sh*t is fast Reply with quote

Wow - I am liking this!!! Saves a lot of time

EXCELLENT!!!

Thanks
Very Happy
Back to top
Learning one



Joined: 04 Apr 2009
Posts: 1000
Location: Croatia

PostPosted: Mon Oct 26, 2009 8:18 pm    Post subject: Reply with quote

First post updated...
Back to top
View user's profile Send private message Visit poster's website
nostroke



Joined: 22 Oct 2009
Posts: 95

PostPosted: Mon Oct 26, 2009 8:22 pm    Post subject: Reply with quote

Learning one wrote:
First post updated...


Holey macro Shocked
Back to top
View user's profile Send private message
Greener than a newb
Guest





PostPosted: Tue Oct 27, 2009 12:02 pm    Post subject: Reply with quote

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





PostPosted: Tue Oct 27, 2009 3:37 pm    Post subject: Reply with quote

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
Code:
Back to top
Greener than newb
Guest





PostPosted: Tue Oct 27, 2009 3:41 pm    Post subject: Reply with quote

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
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group