Tested on Office 2003 Pro
Requires COM.ahk
<!-- m -->http://www.autohotke...pic.php?t=22923<!-- m -->
MS Word:
Test Script:
; WORD TEST SCRIPT com_CoInitialize() ; Initialize COM ; File Name to be created / opened FileName = test_word_document.doc Word_Open() ; Opens a New Document ; Word_OpenDoc(FileName) ; Opens an existing document ;Set Initial Values ; They are toggled later Bold := True Italic := False Caps := True FontSize = 4 Loop, 20 { FontSize += 2 ; Increase the Font Size ; Set the Current Font ; Word_SetCurrentFont(Font,FontSize,Bold,Italic,Caps) Word_SetCurrentFont("Arial",FontSize,Bold := !Bold,Italic := !Italic,Caps := !Caps) Word_InsertText("abcdefg`n") ; Insert The Text } MsgBox Going to save and exit. ;SaveAs DOES NOT PROMPT FOR OVERWRITE IF THE FILE NAME ALREADY EXISTS Word_SaveAs(A_ScriptDir . "\" . FileName) ;Close Word Word_Close() com_CoUnInitialize() exitapp
; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:15 ; Function Name: Word_InsertText() ; ; Insert text into the current document at the insertion point or overwriting selected area ; ; Syntax: ; Word_InsertText(Text) ; Parameters: ; 1) Text = the text string to insert into the document ; Return: ; Success = nothing ; Failure = nothing ; ************************************************************************** Word_InsertText(Text){ Word := Word_Attach("A") ; Attach to Active Window Selection := com_invoke(Word,"Selection") ; Get Insertion Point or Selected text com_invoke(Selection,"TypeText", Text) ; Put the text there com_release(Selection) ; cleanup com_release(Word) ; cleanup } ; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:16 ; Function Name: Word_Open() ; ; Start a new instance of word ; ; Syntax: ; Word_Open() ; Parameters: ; None ; Return: ; Success = nothing ; Failure = nothing ; ************************************************************************** Word_Open(){ Word := Word_Attach("N") ; Attach to New Window Documents := com_Invoke(Word, "Documents") com_invoke(Documents,"Add") ; Add a new document com_invoke(Documents,"Select") ; Select it com_release(Documents) ; cleanup com_release(Word) ; cleanup } ; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:17 ; Function Name: Word_Close() ; ; Close all instances of word ; may prompt for save dialog. ; ; Syntax: ; Word_Close() ; Parameters: ; None ; Return: ; Success = nothing ; Failure = nothing ; ************************************************************************** Word_Close(){ Word := Word_Attach("A") ; Attach to Active Window com_invoke(Word,"Quit") ; quit all instances com_release(Word) ; cleanup } ; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:18 ; Function Name: Word_SaveAs() ; ; Syntax: ; Word_SaveAs(SaveFileName) ; Parameters: ; 1) SaveFileName = The name to save the file as. Must be Full Path ; Return: ; Success = ; Failure = ; ************************************************************************** Word_SaveAs(SaveFileName){ Word := Word_Attach("A") ; Attach to Active Window ActiveDocument := com_invoke(Word,"ActiveDocument") ; get the active document com_invoke(ActiveDocument,"SaveAs", SaveFileName, wdFormatDocument := 0) ; Save the file com_release(ActiveDocument) ; cleanup com_release(Word) ; cleanup } ; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:18 ; Function Name: Word_Save() ; ; Save the current file ; ; Syntax: ; Word_Save() ; Parameters: ; None ; Return: ; Success = nothing ; Failure = nothing ; ************************************************************************** Word_Save(){ Word := Word_Attach("A") ; Attach to Active Window ActiveDocument := com_invoke(Word,"ActiveDocument") com_invoke(ActiveDocument,"Save") ; save file com_release(ActiveDocument) ; cleanup com_release(Word) ; cleanup } ; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:19 ; Function Name: Word_OpenDoc() ; ; Syntax: ; Word_OpenDoc(FileName) ; Parameters: ; 1) FileName = The file name to open ; Return: ; Success = nothing ; Failure = nothing ; ************************************************************************** Word_OpenDoc(FileName){ Word := Word_Attach("N") ; Attach to New Window Documents := com_Invoke(Word, "Documents") com_invoke(Documents,"Open",FileName) ; open file com_release(Documents) ; cleanup com_release(Word) ; cleanup } ; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:19 ; Function Name: Word_GetActiveTitle() ; ; Gets the title of the Active document ; ; Syntax: ; Word_GetActiveTitle() ; Parameters: ; None ; Return: ; Success = The title of the active document ; Failure = nothing ; ************************************************************************** Word_GetActiveTitle(){ Word := Word_Attach("A") ; Attach to Active Window ActiveDocument := com_invoke(Word,"ActiveDocument") Name := com_invoke(ActiveDocument,"Name") ; get the document name com_release(ActiveDocument) ; cleanup com_release(Word) ; cleanup return Name } ;get document text ; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:21 ; Function Name: Word_GetText() ; ; Get the text of the Active Document ; ; Syntax: ; Word_GetText() ; Parameters: ; None ; Return: ; Success = The document text ; Failure = nothing ; ************************************************************************** Word_GetText(){ Word := Word_Attach("A") ; Attach to Active Window ActiveDocument := com_invoke(Word,"ActiveDocument") Range := com_invoke(ActiveDocument,"Range") DocText := com_invoke(Range,"Text") ; get the document text com_release(Range) ; cleanup com_release(ActiveDocument) ; cleanup com_release(Word) ; cleanup return DocText } ;Get selected text ; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:21 ; Function Name: Word_GetSelection() ; ; Get the currently selected text in the active document ; ; Syntax: ; Word_GetSelection() ; Parameters: ; None ; Return: ; Success = the currently selected text ; Failure = nothing ; ************************************************************************** Word_GetSelection(){ Word := Word_Attach("A") ; Attach to Active Window Selection := COM_Invoke(Word,"Selection") SelText := com_invoke(Selection,"Text") ; get the selected text com_release(Selection) ; cleanup com_release(Word) ; cleanup return SelText } ; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:22 ; Function Name: Word_SetCurrentFont() ; ; Set the font of the selected text or the current font ; ; Syntax: ; Word_SetCurrentFont(sFontName="Times New Roman",sFontSize="10",bBold=0,bItalic=0,bCaps=0) ; Parameters: ; 1) sFontName="Times New Roman" = The name of the font to use ; 2) sFontSize="10" = The font size ; 3) bBold=0 = True or False Bold ; 4) bItalic=0 = True or False Italic ; 5) bCaps=0 = True or False All Caps ; Return: ; Success = nothing ; Failure = nothing ; ************************************************************************** Word_SetCurrentFont(sFontName="Times New Roman",sFontSize="10",bBold=0,bItalic=0,bCaps=0){ Word := Word_Attach("A") ; Attach to Active Window Selection := COM_Invoke(Word,"Selection") Font := COM_Invoke(Selection,"Font") COM_Invoke(Font,"Name=", sFontName) ; Font Name COM_Invoke(Font,"Size=", sFontSize) ; Font Size COM_Invoke(Font,"Bold=", bBold) ; Bold COM_Invoke(Font,"Italic=", bItalic) ; Italic COM_Invoke(Font,"AllCaps=", bCaps) ; Caps com_release(Font) ; cleanup com_release(Selection) ; cleanup com_release(Word) ; cleanup } ; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:24 ; Function Name: Word_Detach() ; ; com_release wrapper for consistency ; ; Syntax: ; Word_Detach(ObjWord) ; Parameters: ; 1) ObjWord = Handle to already opened object ; Return: ; Success = nothing ; Failure = nothing ; ************************************************************************** Word_Detach(ObjWord){ com_release(ObjWord) } ; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:26 ; Function Name: Word_Attach() ; ; Gets the active Word Object or creates a new one ; ; Syntax: ; Word_Attach(sInstance="A") ; Parameters: ; 1) sInstance="A" = Values: ; A - Active Object - Default ; N - New Object ; Return: ; Success = handle to a word object ; Failure = Nothing ; ************************************************************************** Word_Attach(sInstance="A"){ SetWinDelay, 0 ;Get The Last Used Instance if (sInstance = "A") { ; Active ObjWord := COM_GetActiveObject("Word.Application") COM_Invoke(ObjWord,"Visible=",True) COM_Invoke(ObjWord,"Activate") } Else if (sInstance = "N") { ; New ObjWord := com_CreateObject("Word.Application") COM_Invoke(ObjWord,"Visible=",True) COM_Invoke(ObjWord,"Activate") } return ObjWord } ; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:31 ; Function Name: Word_ObjGetHwnd() ; ; Gets the hWnd for a given Word object ; ; Syntax: ; Word_ObjGetHwnd(hObj) ; Parameters: ; 1) hObj = Handle to word object ; Return: ; Success = hWnd to hObj 's parent window ; Failure = nothing ; ************************************************************************** Word_ObjGetHwnd(hObj){ DetectHiddenWindows, On SetTitleMatchMode, 2 ;http://support.microsoft.com/kb/310744 ;Generate a unique title sCaption := "[" . UUID() . "]" ;set the title of the window com_invoke(hObj,"Caption=",sCaption) ;get the current caption sCaption := com_invoke(hObj,"Caption") ; Should not be necessary, may not be if !hWnd := WinExist(sCaption) ; Should not Happen !? MsgBox No Window Could Be found!`nhWnd: %hWnd%`nsCaption: %sCaption% ;return the caption to MS Word com_invoke(hObj,"Caption=","") return hWnd } ; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:36 ; Function Name: Word_GetActiveHwnd() ; ; Returns the hWnd for the active Word Object's Window ; ; Syntax: ; Word_GetActiveHwnd() ; Parameters: ; None ; Return: ; Success = hWnd ; Failure = nothing ; ************************************************************************** Word_GetActiveHwnd(){ hWnd := Word_ObjGetHwnd(hObj := Word_Attach()) Word_Detach(hObj) return hWnd } ; ************************************************************************** ; Author: ahklerner ; Language: AutoHotkey v1.0.47.06 ; Creation Date: 05/19/2008 14:45 ; Function Name: Word_Activate() ; ; Activate & Show the Active Word Object ; ; Syntax: ; Word_Activate() ; Parameters: ; None ; Return: ; Success = ; Failure = ; ************************************************************************** Word_Activate(){ WinActivate, % "ahk_id " . hWnd := Word_GetActiveHwnd() WinShow, % "ahk_id " . hWnd }