com - Why does this operation break when turned into a function (AutoHotKey/AHK) -
the following operation works correcting inside ahk. replaces word ted word tom inside open word document.
working code
; word constants vbtrue := -1 wdreplacenone := 0 wdfindcontinue := 1 return #ifwinactive, ahk_exe winword.exe ^7:: try oword := comobjactive("word.application") catch return findtext := "ted" replacewith := "tom" ofind := oword.selection.find ohyperlinks := oword.activedocument.hyperlinks ofind.clearformatting ofind.replacement.clearformatting while ofind.execute(findtext, vbtrue, false,,,,, wdfindcontinue,,, wdreplacenone) ohyperlinks.add(oword.selection.range, "http://www.autohotkey.com",,, replacewith) return
however, when turn exact same code function not work. not work when written way, , not work if parameters removed , variables put script.
broken code (with parameters)
replaceandlink(findtext, replacewith) { ; word constants vbtrue := -1 wdreplacenone := 0 wdfindcontinue := 1 return try oword := comobjactive("word.application") catch return ofind := oword.selection.find ohyperlinks := oword.activedocument.hyperlinks ofind.clearformatting ofind.replacement.clearformatting while ofind.execute(findtext, vbtrue, false,,,,, wdfindcontinue,,, wdreplacenone) ohyperlinks.add(oword.selection.range, "http://www.autohotkey.com",,, replacewith) return } #ifwinactive, ahk_exe winword.exe ^7:: replaceandlink("ted", "tom")
broken code (without parameters)
replaceandlink(findtext, replacewith) { ; word constants vbtrue := -1 wdreplacenone := 0 wdfindcontinue := 1 return try oword := comobjactive("word.application") catch return findtext := "ted" replacewith := "tom" ofind := oword.selection.find ohyperlinks := oword.activedocument.hyperlinks ofind.clearformatting ofind.replacement.clearformatting while ofind.execute(findtext, vbtrue, false,,,,, wdfindcontinue,,, wdreplacenone) ohyperlinks.add(oword.selection.range, "http://www.autohotkey.com",,, replacewith) return } #ifwinactive, ahk_exe winword.exe ^7:: replaceandlink()
troubleshooting notes:
- word open during both operations
- i using newest version of ahk
- i have tried running broken 1 on clean restart
- no special libraries or other ahk scripts running
also... know similar com based ahk scripts can placed functions... see example:
linkcreator(findtext, replacewith) { oword := comobjactive("word.application") oword.selection.find.clearformatting oword.selection.find.replacement.clearformatting oword.selection.find.execute(findtext, 0, 0, 0, 0, 0, 1, 1, 0, replacewith, 2) } f2:: linkcreator("store", "town")
you're calling return
before function can finish. causes script stop processing function , return caller.
replaceandlink(findtext, replacewith) { ; word constants vbtrue := -1 wdreplacenone := 0 wdfindcontinue := 1 return <---------- here try oword := comobjactive("word.application") catch return
try removing , should execute expected.
a simple troubleshooting tip when isn't executing place soundbeep
or msgbox
somewhere in code see if have unreachable code , work backwards there.
Comments
Post a Comment