Sean> Thanks! That seems to work. However, for some reason, it only works like twice before the script bombs out.
Here are the functions as I modified them from the originals in this script to what I gather is the proper syntax for AHK_L:
Code:
; Revisions by silveredge78 to be usable for COM_L based on syntax from Sean.
; v0.1.1, 11/19/09
; - Revising the other functions to use the COM_L syntax.
; - Clean up documentation & code to my standards.
;
; v0.1.0, 11/17/09
; - Initial attempts at modifying Word_GetText() using unprotect/protect
; features.
;
; **************************************************************************
; 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 := Word.Selection ; Get Insertion Point or Selected text
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 Active Window
Documents := Word.Documents
Documents.Add() ; Add a new document
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
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 := Word.ActiveDocument ; Get the active document
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 := Word.ActiveDocument ; Get the active document
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 Active Window
Documents := Word.Documents
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 := Word.ActiveDocument ; Get the active document
Name := 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 := Word.ActiveDocument ; Get the active document
ProtectionType := ActiveDocument.ProtectionType ; Determine the protection level of the document
;MsgBox, Debug:`nProtectionType: %ProtectionType% ; For testing, enabled
If ProtectionType <> -1 ; If protection is set to wdAllowOnlyFormFields, then Unprotect
ActiveDocument.Unprotect()
Range := ActiveDocument.Range
DocText := Range.Text ; Read the text of the file
If ProtectionType <> -1 ; If protection had been set, then reprotect
ActiveDocument.Protect(ProtectionType,True)
COM_Release(Range) ; Cleanup
COM_Release(ProtectionType) ; Cleanup
COM_Release(ActiveDocument) ; Cleanup
COM_Release(Word) ; Cleanup
Return, DocText ; Return filetext
}
; **************************************************************************
; 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 := Word.Selection
SelText := Word.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 := Word.Selection
Font := Selection.Font
Font.Name(sFontName) ; Font Name
Font.Size(sFontSize) ; Font Size
Font.Bold(bBold) ; Bold
Font.Italic(bItalic) ; Italic
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 Window - Default
{
ObjWord := COM_GetActiveObject("Word.Application") ; Attach to Active Window
ObjWord.Visible(True)
ObjWord.Activate()
}
Else If (sInstance = "N") ; New Window
{
ObjWord := COM_CreateObject("Word.Application")
ObjWord.Visible(True)
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
Random, UUID, 10000, 999999
sCaption := "[" . UUID . "]" ; Generate a unique title - http://support.microsoft.com/kb/310744
hObj.Caption(sCaption) ; Set the title of the window
sCaption := hObj.Caption ; Get the current 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%
hObj.Caption() ; Return the caption to MS Word
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
}
The two that I use in my script are Word_GetText() and from there it calls Word_Attach(). I cannot figure out why it is unstable.
Can someone take a look at my code and tell me what I'm messing up and is it due to the COM_L implementation? Or is it how it's compiling somehow? I'm at a loss. I use the above noted functions in my include, as well as COM_L. Here is my code:
Code:
^+c::
GoSub, ActiveWordCheck ; Check to make sure Word is the active window
WordText := Word_GetText()
GoSub, ClearVariables
Loop, Parse, WordText,
{
If A_Index = 2
PatientName := RegExReplace(A_LoopField, "\s$|\s\s$", "")
If A_LoopField contains scribed HPI
Goto, VersionError
}
Loop, Parse, WordText,
{
If A_Index = 4
Account := CopyCheck(A_LoopField)
If A_Index = 27
Allergies := CopyCheck(A_LoopField)
If A_Index = 81
FH_Gen := CopyCheck(A_LoopField)
If A_Index In 83,89,95,101,107,114,120,126,132,139,145,151,157
Medications := CopyCheck(A_LoopField,Medications)
If A_Index In 86,92,98,104
PMD := CopyCheck(A_LoopField,PMD)
If A_Index = 88
FH_CAD := CopyCheck(A_LoopField)
If A_Index = 94
FH_Cancer := CopyCheck(A_LoopField)
If A_Index = 100
FH_CVA := CopyCheck(A_LoopField)
If A_Index = 106
FH_DM := CopyCheck(A_LoopField)
If A_Index In 111,117,123,129
CPL := CopyCheck(A_LoopField,CPL)
If A_Index = 113
FH_HTN := CopyCheck(A_LoopField)
If A_Index = 119
FH_Thyroid := CopyCheck(A_LoopField)
If A_Index = 125
SH_Gen := CopyCheck(A_LoopField)
If A_Index = 131
SH_Caffeine := CopyCheck(A_LoopField)
If A_Index In 136,142,148,154
Surgeries := CopyCheck(A_LoopField,Surgeries)
If A_Index = 138
SH_ETOH := CopyCheck(A_LoopField)
If A_Index = 144
SH_Tobacco := CopyCheck(A_LoopField)
If A_Index = 150
SH_Drugs := CopyCheck(A_LoopField)
}
MsgBox, 0, %ScriptName%, Copy completed for %PatientName% (%Account%).
Return
^+v::
GoSub, ActiveWordCheck ; Check to make sure Word is the active window
WordText := Word_GetText()
Loop, Parse, WordText,
{
If A_Index = 2
NewPatientName := RegExReplace(A_LoopField, "\s$|\s\s$", "")
If A_Index = 4
NewAccount := CopyCheck(A_LoopField) ; Account Number
}
If (Account != NewAccount) Or (PatientName != NewPatientName)
{
NewDocumentID := WinExist("A")
MsgBox, 262452, %ScriptName% - Warning, The patient name or account number for this `npaste does not match the currently stored copy.`n`nCopy Information:`nPatient Name: %PatientName%`nAccount #: %Account%`n`nPaste Information`nPatient Name: %NewPatientName%`nAccount #: %NewAccount%`n`nWould you like to proceed with the paste?
IfMsgBox, No
Return
WinActivate, ahk_id %NewDocumentID%
}
SendInput, {Ctrl Up}{Shift Up}
SendInput, ^{Home}
Sleep, 200
SendInput, {Tab 9}
Sleep, 100
SendInput, %SH_Gen%{Tab}
Sleep, 100
SendInput, %Allergies%{Tab}
Sleep, 100
SendInput, %PMD%{Tab}
Sleep, 100
SendInput, %FH_CAD%{Tab}
Sleep, 100
SendInput, %Medications%{Tab 2}
Sleep, 100
SendInput, %FH_Cancer%{Tab}
Sleep, 100
SendInput, %FH_CVA%{Tab}
Sleep, 100
SendInput, %FH_DM%{Tab}
Sleep, 100
SendInput, %CPL%{Tab}
Sleep, 100
SendInput, %FH_HTN%{Tab 2}
Sleep, 100
SendInput, %FH_Thyroid%{Tab}
Sleep, 100
SendInput, %FH_Gen%{Tab}
Sleep, 100
SendInput, %SH_Caffeine%{Tab}
Sleep, 100
SendInput, %SH_ETOH%{Tab}
Sleep, 100
SendInput, %Surgeries%{Tab}
Sleep, 100
SendInput, %SH_Drugs%{Tab}
Sleep, 100
SendInput, %SH_Tobacco%{Tab 2}
Sleep, 100
SendInput, %SH_Occupation%
Sleep, 100
MsgBox, 0, %ScriptName%, Paste complete for %NewPatientName% (%NewAccount%).
Return
ActiveWordCheck:
Loop
{
If WinActive("ahk_class OpusApp") = 0x0
{
MsgBox, 262197, %ScriptName%, There is not a Microsoft Word Document active. If you would like to`nretry`, please select the appropriate Microsoft Word Document and`npress Retry. Otherwise`, press Cancel to end the hotkey.
IfMsgBox, Cancel
Exit
}
Else
Break
Sleep, 100
}
Return
Any help would be greatly appreciated - Either with my code or my compiling/instability woes.