I'm trying to use vbscript.regex to highlight text in Word documents. It seems to work fine, though it doesn't highlight the text properly if there's a table in the file. Then the highlighting shifts.
Does anyone know how to fix that? Thanks for the help!
Code: Select all
needleArray := []
oWord := ComObjCreate("Word.Application")
regex := ComObjCreate("VBScript.RegExp")
regex.IgnoreCase := true
loop, read, %A_ScriptDir%\test.txt
{
loop, parse, A_LoopReadLine, `n
{
needle := needleArray.Push(A_LoopReadLine)
}
}
sourceFile := "test with table.docx"
filePath = %A_ScriptDir%\
sourceFullPath := filePath . sourceFile
SplitPath, sourceFullPath, OutFileName, OutDir, OutExtension, OutNameNoExt, OutDrive
oWord.Documents.Open(sourceFullPath)
oWord.Visible := 1
oWord.Activate
haystack := oWord.ActiveDocument.Range.Text
for index, needle in needleArray {
regex.Pattern := ""
regex.Pattern := needle
regexMatch := regex.Execute(haystack)
for item in regexMatch {
oWord.ActiveDocument.Range(item.FirstIndex, item.FirstIndex + item.Length).HighlightColorIndex := 4
}
}
oWord.Application.ActiveDocument.SaveAs(outDir . "\" . OutNameNoExt . "_xxx." . OutExtension)
oWord.Application.ActiveDocument.Close()
oWord.Application.Quit
sleep 100
oWord := ""
sleep 100
regex := ""
MsgBox,, Oh..., Done, done!
Exitapp
Aen(.*?)n nec l(.*?)m
Suspendisse dui purus, (.*?), nunc
Result w/o table is here:
https://imgur.com/a/4bgrLSz
Result w table:
https://imgur.com/a/01Upkwf