Here is the rest of my code, sorry if it’s abit messy i’m new to fsharp.
Yes it’s in a module.
Basically this is a combination of different things that i’ve combined to try and get this to work, I want it to find and replace words in word documents.
Thanks again for your help
#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 m_word : ApplicationClass option ref = ref None
let OpenWord() = m_word := Some(new ApplicationClass())
let GetWordInstance() = Option.get !m_word
let CloseWord() = (GetWordInstance()).Quit()
//let things = m_word.contents.Value.Documents
let comarg x = ref (box x)
let OpenDocument filePath =
printfn “Opening %s…” filePath
m_word.contents.Value.Documents.Open(comarg filePath)
let PrintDocument (doc : Document) =
printfn "Printing %s..." doc.Name
// let PrintDocument (doc : Document) =
//
// printfn “Printing %s…” doc.Name
//
// doc.PrintOut(
// Background = comarg true,
// Range = comarg WdPrintOutRange.wdPrintAllDocument,
// Copies = comarg 1,
// PageType = comarg WdPrintOutPages.wdPrintAllPages,
// PrintToFile = comarg false,
// Collate = comarg true,
// ManualDuplexPrint = comarg false,
// PrintZoomColumn = comarg 2, // Pages ‘across’
// PrintZoomRow = comarg 2) // Pages ‘up down’
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
// m_word.Activate doc
doc.Content.Find.Execute(
comarg findText,
matchCase,
matchWholeWord,
matchWildCards,
matchSoundsLike,
matchAllWordForms,
forward,
wrap,
format,
comarg replaceWithText,
replace,
matchKashida,
matchDiacritics,
matchAlefHamza,
matchControl)
let fileName = Path.Combine(System.Windows.Forms.Application.StartupPath, “document.docx”)
let wordApp = new Microsoft.Office.Interop.Word.ApplicationClass(Visible = true)
let comarg x = ref (box x)
let aDoc = wordApp.Documents.Open(comarg fileName)
aDoc.Activate()
FindAndReplace(wordApp, “{id}”, “12345”)
// example useage
let findandreplaceinfolders folder findText replaceText =
Directory.GetFiles(folder, “*.docx”)
|> Array.iter
(fun filePath ->
let doc = OpenDocument filePath
FindAndReplace doc test McTesty
CloseDocument doc)
// Notes
// m_word : the application
// doc : the document
//
// print EXAMPLE
// doc.Print
//
// f&r EXAMPLE
// m_word.Activate(doc)
// m_word.Selection.Find.Execute
//
// doc.Selection.Find.Execute
let CloseDocument (doc : Document) =
printfn “Closing %s…” doc.Name
doc.Close(SaveChanges = comarg false)
// -------------------------------------------------------------
let currentFolder = SOURCE_DIRECTORY
open System
open System.IO
OpenWord()
printfn "Printing all files in [%s]..." currentFolder
Directory.GetFiles(currentFolder, "*.docx")
|> Array.iter
(fun filePath ->
let doc = OpenDocument filePath
FindAndReplace doc
CloseDocument doc)
CloseWord()
printfn “Press any key…”
Console.ReadKey(true) |> ignore