#NoEnv #SingleInstance Force SetWorkingDir %A_ScriptDir% SetBatchLines -1 docFile := A_ScriptDir "5.docx" doc1 := new RandomDoc(docFile) if Total := doc1.Find("【【*】】", True, 2, 2) { ; 搜索后文件不会被关闭,接着可以进行多次的随机复制 index := doc1.Copy() MsgBox,, 随机复制, 已复制第 %index% 个结果(共 %Total% 个结果),可粘贴到 Word 或者图像编辑器。 index := doc1.Copy(3) ; 复制第三个结果 MsgBox,, 复制指定的结果, 已复制第 %index% 个结果(共 %Total% 个结果),可粘贴到 Word 或者图像编辑器。 doc1 := "" ; 这样可以关闭打开的文件,或者脚本退出时会自动关闭文件。 } else MsgBox, 找不到文字 Return ; TO DO: 增加随机合并多个文件 Class RandomDoc { __New(docFile) { this.oWord := ComObjCreate("Word.Application") this.oWord.Documents.Open(docFile, 0, 1) ; Open in ReadOnly mode this.docFile := docFile } ; 关闭脚本时会自动关闭文件 __Delete() { this.oWord.Quit } ; 手动关闭文件 Quit() { this.oWord.Quit } ; -------------------------------------------------- ; 功能: ; 查找文字,并保存每个结果的位置到数组 this.FoundPos ; 返回: ; 结果个数 ; -------------------------------------------------- Find(FindText, MatchWildcards = False, nLTrim = 0, nRTrim = 0) { this.FoundPos := [] ; http://msdn.microsoft.com/es-uy/library/microsoft.office.interop.word.selection_members.aspx s := this.oWord.Selection s.Find.ClearFormatting While s.Find.Execute(FindText, 0, 0, MatchWildcards, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0) { if (s.Start this.FoundPos.MaxIndex() ) Random, ResultIndex, 1, % this.FoundPos.MaxIndex() Clipboard := "" s := this.oWord.Selection s.Start := this.FoundPos[ ResultIndex ] s.Find.ClearFormatting s.Find.Execute(this.FindText, 0, 0, this.MatchWildcards, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0) this.nLTrim ? ( s.Start += this.nLTrim ) : "" this.nRTrim ? ( s.End -= this.nRTrim ) : "" s.Copy ClipWait Return ResultIndex } }