使用 f# Microsoft.Office.Interop.Word 搜索和替换

Search and replace using f# Microsoft.Office.Interop.Word

我希望能够在 .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)

https://docs.microsoft.com/en-us/office/vba/api/word.wdsaveoptions

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