This project is read-only.
Word

Category:
Office Interop
Description: This sample create a Word instance, insert text, table, and chart into a word document. Please make sure Word 2010 installed on your computer.
Code:
let wordSample() = 
try
// open a new Word document
let word = Word.ApplicationClass()
word.Visible <- true
let doc = word.Documents.Add()
// insert a sentence
let paragraph = doc.Content.Paragraphs.Add()
paragraph.Range.Text <- "F# sample"
paragraph.Range.Font.Bold <- 1
paragraph.Format.SpaceAfter <- 24.0F //24 pt sapcing
paragraph.Range.InsertParagraphAfter();
//insert a table
let oEndOfDoc = "\\endofdoc" :> obj
let wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range
let table = doc.Tables.Add(wrdRng, 3, 5)
table.Range.ParagraphFormat.SpaceAfter <- 6.F
for r in [1..3] do
for c in [1..5] do
table.Cell(r, c).Range.Text <- sprintf "row %d col %d" r c
table.Rows.[1].Range.Font.Bold <- 0
table.Rows.[1].Range.Font.Italic <- 1
//insert chart
let classType = "MSGraph.Chart.8" :> obj
let wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
let shape = wrdRng.InlineShapes.AddOLEObject(ref classType)
let chart = shape.OLEFormat.Object
let chartApp = chart.GetType().InvokeMember("Application", Reflection.BindingFlags.GetProperty, null, chart, null)
let parameters = [|4|] |> Array.map (fun n -> n :> obj)
chart.GetType().InvokeMember("ChartType", Reflection.BindingFlags.SetProperty, null, chart, parameters) |> ignore
with _ -> printfn "%s" "please check your Word installation"
Execution Result:
 

Last edited Sep 14, 2011 at 4:05 AM by ttliu2000, version 1

Comments

No comments yet.