Ms word 自动将Grep Applescript转换为Word文档

Ms word 自动将Grep Applescript转换为Word文档,ms-word,grep,applescript,automator,Ms Word,Grep,Applescript,Automator,我正在使用Mac电脑,我正在为一家公司准备账户。我用Microsoft Word制作的每张工资单都有一个凭证号。因为一笔交易被错过了,所有的凭证号码都是错误的,所以现在有数百张错误的工资单。我想创建一个脚本,可以找到以下GREP(查找段落开头,文本:Vch,任何字符,直到\r): 并将其替换为零(从而删除整个句子) 我正在考虑使用Applescript,因为它可以打开文档,执行GREP查找(棘手的部分),保存文档并将其保存为pdf(所有需要的内容) 但显然我的知识让我失望。字典中的create

我正在使用Mac电脑,我正在为一家公司准备账户。我用Microsoft Word制作的每张工资单都有一个凭证号。因为一笔交易被错过了,所有的凭证号码都是错误的,所以现在有数百张错误的工资单。我想创建一个脚本,可以找到以下GREP(查找段落开头,文本:Vch,任何字符,直到\r):

并将其替换为零(从而删除整个句子)

我正在考虑使用Applescript,因为它可以打开文档,执行GREP查找(棘手的部分),保存文档并将其保存为pdf(所有需要的内容)

但显然我的知识让我失望。字典中的create range、execute find等命令都会带来错误

有经验的人能帮我设计剧本吗?有什么建议吗?应该是这样的:

Tell application "Microsoft Word"
  tell active document
  set myRange to create range start 0 end 0
    tell myRange
      execute find find "^Vch.+\r" replace with ""
    end tell
  end tell
end tell

非常感谢

没有特殊字符表示行的开头

要在段落开头搜索,脚本必须使用
return&“some text”

您可以使用“^p”作为段落标记,但如果将
匹配通配符属性设置为true,则该标记无效

要匹配整个段落,脚本必须使用
return&“some text”&return
,并且脚本必须使用
替换为return
删除一个段落标记,而不是两个

因为第一段不是以段落标记开头,所以脚本必须使用两个
executefind
命令

通配符是*


Tell application "Microsoft Word"
  tell active document
  set myRange to create range start 0 end 0
    tell myRange
      execute find find "^Vch.+\r" replace with ""
    end tell
  end tell
end tell
tell application "Microsoft Word" -- (tested on version 15.25,  Microsoft Office 2016)

    -- check the first  paragraph
    select (characters of paragraph 1 of active document)
    execute find (find object of selection) find text ("Vch*" & return) replace with "" replace replace one wrap find find stop with match wildcards and match case without match forward and find format

    --to search forward toward the end of the document.
    execute find (find object of selection) find text (return & "Vch*" & return) replace with return replace replace all wrap find find continue with match wildcards, match case and match forward without find format

    save active document

    -- export to PDF in the same directory as the active document
    set pdfPath to path of active document & ":" & (get name of active window) & ".pdf"
    set pdfPath to my createFile(pdfPath) -- create an empty file if not exists, the handler return a path of type alias (to avoid grant access issue)
    save as active document file name pdfPath file format format PDF
end tell

on createFile(f)
    do shell script "touch " & quoted form of POSIX path of f
    return f as alias
end createFile