为什么这个操作在变成函数时会中断(AutoHotKey/AHK)
Why does this operation break when turned into a function (AutoHotKey/AHK)
以下操作可以在 AHK 内部进行更正。它用打开的 word 文档中的单词 tom 替换了单词 ted。
工作代码
; 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
但是,当我将完全相同的代码转换为函数时,它不起作用。这样写就不行了,去掉参数再把变量放回脚本里也不行。
损坏的代码(带参数)
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")
损坏的代码(没有参数)
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()
故障排除说明:
- Word 在两次操作期间都处于打开状态
- 我正在使用最新版本的 AHK
- 我试过 运行 坏掉的完全重启
- 没有特殊的库或其他 AHK 脚本运行
另外...我知道可以将类似的基于 COM 的 AHK 脚本放入函数中...参见示例:
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")
您在函数完成之前调用 return
。这会导致脚本停止处理该函数并向调用者 return。
ReplaceAndLink(FindText, ReplaceWith)
{
; Word Constants
vbTrue := -1
wdReplaceNone := 0
wdFindContinue := 1
return <---------- HERE
try
oWord := ComObjActive("Word.Application")
catch
return
尝试删除它,它应该会按预期执行。
当某事未执行时,一个简单的故障排除技巧是在代码中的某处放置一个 Soundbeep
or MsgBox
以查看您是否有一些无法访问的代码并从那里向后工作。
以下操作可以在 AHK 内部进行更正。它用打开的 word 文档中的单词 tom 替换了单词 ted。
工作代码
; 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
但是,当我将完全相同的代码转换为函数时,它不起作用。这样写就不行了,去掉参数再把变量放回脚本里也不行。
损坏的代码(带参数)
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")
损坏的代码(没有参数)
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()
故障排除说明:
- Word 在两次操作期间都处于打开状态
- 我正在使用最新版本的 AHK
- 我试过 运行 坏掉的完全重启
- 没有特殊的库或其他 AHK 脚本运行
另外...我知道可以将类似的基于 COM 的 AHK 脚本放入函数中...参见示例:
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")
您在函数完成之前调用 return
。这会导致脚本停止处理该函数并向调用者 return。
ReplaceAndLink(FindText, ReplaceWith)
{
; Word Constants
vbTrue := -1
wdReplaceNone := 0
wdFindContinue := 1
return <---------- HERE
try
oWord := ComObjActive("Word.Application")
catch
return
尝试删除它,它应该会按预期执行。
当某事未执行时,一个简单的故障排除技巧是在代码中的某处放置一个 Soundbeep
or MsgBox
以查看您是否有一些无法访问的代码并从那里向后工作。