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

问题描述 投票:0回答:1

我希望能够找到并替换.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

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

ms-word f# office-interop f#-interactive
1个回答
0
投票

我相信您需要保存文档。

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是一个布尔值,但看起来它实际上是一个枚举。

© www.soinside.com 2019 - 2024. All rights reserved.