1

I want to be able to find and replace words in .docx files, I have working code but it doesn't actually do anything. Could anyone tell me how I can use this code to find and replace words?

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

No error messages, all seems to be working fine.

0

2 Answers 2

1

I believe you need to save the document.

doc.Close(SaveChanges = comarg false)

Should instead be

doc.Close(SaveChanges = comarg -1)

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

It appears that SaveChanges takes a WDSaveOptions

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

I originally recommended true because I thought SaveChanges was a boolean but it appears that it is actually an enum.

5
  • Hi thank you for your help, unfortunately this doesn't change anything. I have a word document in the same folder that i wanted to test. Nothing changes however
    – Nethalin
    Nov 6, 2019 at 15:14
  • I've changed to doc.Close(SaveChanges = comarg -1) The word document that i have in the same file of my project is still unchanged. I also noticed i'm getting two errors when running the program which is : > let closeDocument (doc : Document) = - printfn "Closing %s…" doc.Name - doc.Close(SaveChanges = comarg -1);; FTEST1.fsx(269,27): error FS0039: The type 'Document' is not defined. > doc.Close(SaveChanges = comarg -1);; FTEST1.fsx(272,1): error FS0039: The value, namespace, type or module 'doc' is not defined.
    – Nethalin
    Nov 6, 2019 at 16:16
  • You may want to update your question to include this new information. Nov 6, 2019 at 16:19
  • I've done so now, thank you for your help so far :)
    – Nethalin
    Nov 6, 2019 at 16:24
  • The errors disappeared after reevaluating It still doesn't change the word file thats in the same folder as my project
    – Nethalin
    Nov 6, 2019 at 17:37
0

This code now fully works and will find and replace text in a word document

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.