Ms word 使用f#Microsoft.Office.Interop.Word搜索并替换

Ms word 使用f#Microsoft.Office.Interop.Word搜索并替换,ms-word,f#,office-interop,f#-interactive,Ms Word,F#,Office Interop,F# Interactive,我希望能够在.docx文件中查找和替换单词,我有可用的代码,但实际上它什么都不做。谁能告诉我如何使用这个代码查找和替换单词 open System #light #I @"C:\Users\netha\Documents\FSharpTest\packages\Microsoft.Office.Interop.Word\lib\net20" #r "Microsoft.Office.Interop.Word.dll" module FTEST1 = open Microsoft.O

我希望能够在
.docx
文件中查找和替换单词,我有可用的代码,但实际上它什么都不做。谁能告诉我如何使用这个代码查找和替换单词

open System

#light
#I @"C:\Users\netha\Documents\FSharpTest\packages\Microsoft.Office.Interop.Word\lib\net20"
#r "Microsoft.Office.Interop.Word.dll"

module FTEST1 =

    open Microsoft.Office.Interop.Word
    open System.IO

    let comarg x = ref (box x)

    let printDocument (doc : Document) =
        printfn "Printing %s..." doc.Name

    let findAndReplace (doc : Document, findText : string, replaceWithText : string) =

        printfn "finding and replacing  %s..." doc.Name

        //options
        let matchCase = comarg false
        let matchWholeWord = comarg true
        let matchWildCards = comarg false
        let matchSoundsLike = comarg false
        let matchAllWordForms = comarg false
        let forward = comarg true
        let format = comarg false
        let matchKashida = comarg false
        let matchDiacritics = comarg false
        let matchAlefHamza = comarg false
        let matchControl = comarg false
        let read_only = comarg false
        let visible = comarg true
        let replace = comarg 2
        let wrap = comarg 1
        //execute find and replace

        doc.Content.Find.Execute(
                    comarg findText, 
                    matchCase, 
                    matchWholeWord,
                    matchWildCards, 
                    matchSoundsLike, 
                    matchAllWordForms, 
                    forward, 
                    wrap, 
                    format, 
                    comarg replaceWithText, 
                    replace,
                    matchKashida,
                    matchDiacritics, 
                    matchAlefHamza, 
                    matchControl)

    let wordApp = new Microsoft.Office.Interop.Word.ApplicationClass(Visible = true)

    let openDocument fileName = 
        wordApp.Documents.Open(comarg fileName)

    // example useage
    let closeDocument (doc : Document) =
        printfn "Closing %s…" doc.Name
        doc.Close(SaveChanges = comarg false)

    let findText = "test"
    let replaceText = "McTesty"

    let findandreplaceinfolders folder findText replaceText =
        Directory.GetFiles(folder, "*.docx")
        |> Array.iter (fun filePath ->
            let doc = openDocument filePath
            doc.Activate()
            // printDocument doc
            findAndReplace(doc, findText, replaceText)
            closeDocument doc)

    let currentFolder = __SOURCE_DIRECTORY__

    printfn "Printing all files in [%s]..." currentFolder
    findandreplaceinfolders currentFolder

    wordApp.Quit()

printfn "Press any key…"
Console.ReadKey(true) |> ignore

没有错误消息,一切正常。

我认为您需要保存文档

doc.Close(SaveChanges = comarg false)
应该是

doc.Close(SaveChanges = comarg -1)

似乎SaveChanges采用了WDSaveOptions

wdDoNotSaveChanges = 0 | wdPromptToSaveChanges = -2 | wdSaveChanges = -1

我最初建议使用true,因为我认为SaveChanges是一个布尔值,但它实际上是一个枚举。

这段代码现在可以完全工作,并将查找和替换word文档中的文本。

open System

#light

#I@"C:\Users\netha\Documents\FSharpTest\packages\Microsoft.Office.Interop.Word
\lib\net20"
#r "Microsoft.Office.Interop.Word.dll"

module FTEST1 =

open Microsoft.Office.Interop.Word
open System.IO

let comarg x = ref (box x)

let printDocument (doc : Document) =
    printfn "Printing %s..." doc.Name

let findAndReplace (doc : Document, findText : string, replaceWithText : string) =

    printfn "finding and replacing  %s..." doc.Name

    //options
    let matchCase = comarg false
    let matchWholeWord = comarg true
    let matchWildCards = comarg false
    let matchSoundsLike = comarg false
    let matchAllWordForms = comarg false
    let forward = comarg true
    let format = comarg false
    let matchKashida = comarg false
    let matchDiacritics = comarg false
    let matchAlefHamza = comarg false
    let matchControl = comarg false
    let read_only = comarg false
    let visible = comarg true
    let replace = comarg 2
    let wrap = comarg 1
    //execute find and replace

    let res = 
        doc.Content.Find.Execute(
            comarg findText, 
            matchCase, 
            matchWholeWord,
            matchWildCards, 
            matchSoundsLike, 
            matchAllWordForms, 
            forward, 
            wrap, 
            format, 
            comarg replaceWithText, 
            replace,
            matchKashida,
            matchDiacritics, 
            matchAlefHamza, 
            matchControl)

    printfn "Result of Execute is: %b" res

let wordApp = new Microsoft.Office.Interop.Word.ApplicationClass(Visible = true)

let openDocument fileName = 
    wordApp.Documents.Open(comarg fileName)

// example useage
let closeDocument (doc : Document) =
    printfn "Closing %s…" doc.Name
    // wdDoNotSaveChanges = 0 | wdPromptToSaveChanges = -2 | wdSaveChanges = -1
    doc.Close(SaveChanges = comarg -1)

let findText = "test"
let replaceText = "McTesty"

let findandreplaceinfolders folder findText replaceText =
    Directory.GetFiles(folder, "*.docx")
    |> Array.iter (fun filePath ->
        let doc = openDocument filePath
        doc.Activate()
        // printDocument doc
        printfn "I work"
        findAndReplace(doc, findText, replaceText)
        closeDocument doc)


let currentFolder = __SOURCE_DIRECTORY__

printfn "Printing all files in [%s]..." currentFolder
findandreplaceinfolders currentFolder findText replaceText

wordApp.Quit()

printfn "Press any key…"
Console.ReadKey(true) |> ignore

嗨,谢谢你的帮助,不幸的是这并没有改变任何事情。我想测试的文件夹中有一个word文档。没有任何改变,但是我已经更改为doc.Close(SaveChanges=comarg-1),我在项目的同一个文件中的word文档仍然没有更改。我还注意到在运行该程序时出现两个错误:>let closeDocument(doc:Document)=-printfn“Closing%s…”doc.Name-doc.Close(SaveChanges=comarg-1);;FTEST1.fsx(269,27):错误FS0039:未定义类型“文档”。>文档关闭(SaveChanges=comarg-1);;FTEST1.fsx(272,1):错误FS0039:未定义值、命名空间、类型或模块“doc”。您可能希望更新您的问题以包含此新信息。我现在已经更新了,感谢您迄今为止的帮助:)重新评估后错误消失,但仍然不会更改与我的项目位于同一文件夹中的word文件