AutoHotkey Community

It is currently May 26th, 2012, 10:17 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: October 23rd, 2009, 1:28 pm 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
:?: 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 8))


Last edited by Learning one on October 21st, 2010, 8:26 pm, edited 9 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 23rd, 2009, 1:48 pm 
Offline
User avatar

Joined: May 24th, 2009, 5:35 am
Posts: 2099
Location: Iowa, USA
Check out COM - specifically COM_L (see my signature). If I get a chance later, I will look at this more.

_________________
Image
Recommended: AutoHotkey_L
Basic Webpage Controls


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 23rd, 2009, 6:15 pm 
Offline
User avatar

Joined: May 24th, 2009, 5:35 am
Posts: 2099
Location: Iowa, USA
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.

_________________
Image
Recommended: AutoHotkey_L
Basic Webpage Controls


Last edited by jethrow on October 24th, 2009, 3:02 pm, edited 3 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 23rd, 2009, 11:22 pm 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
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):roll:...

I'm using AHK. Can this be done in AHK (not AHK_L)?
For now, I'll stick to my old way ( WinMenuSelectItem, ControlClick...)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 24th, 2009, 9:23 am 
Offline
User avatar

Joined: May 24th, 2009, 5:35 am
Posts: 2099
Location: Iowa, USA
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.

_________________
Image
Recommended: AutoHotkey_L
Basic Webpage Controls


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 25th, 2009, 12:06 am 
Offline

Joined: August 22nd, 2009, 11:23 pm
Posts: 294
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, :o

_________________
Image
"Man's quest for knowledge is an expanding series whose limit is infinity"


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 25th, 2009, 2:57 pm 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
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()


Report this post
Top
 Profile  
Reply with quote  
PostPosted: October 26th, 2009, 3:19 pm 
Wow - I am liking this!!! Saves a lot of time

EXCELLENT!!!

Thanks
:D


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 26th, 2009, 9:18 pm 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
First post updated...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 26th, 2009, 9:22 pm 
Offline

Joined: October 22nd, 2009, 10:26 pm
Posts: 95
Learning one wrote:
First post updated...


Holey macro :shock:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 27th, 2009, 1:02 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 27th, 2009, 4:37 pm 
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][/code]


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 27th, 2009, 4:41 pm 
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


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: AndyJenk, hyper_, JSLover, Leef_me, patgenn123, rbrtryn, XstatyK and 71 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group