A reader asks "Can you tell me how to format text in Word using Ruby? I'm most interested in controlling justification, indenting paragraphs, changing fonts, using italics, bold, and underlining, etc."
This is the latest in a series of articles about Automating Word with Ruby.
There's a variety of formatting that you can apply to Range objects in Word. Let's look at some of the most common...
To change the font of a Range object, set its Font.Name value to a valid font name:
document.Words(1).Font.Name = 'Verdana'
To change the font size of a Range object, set its Font.Size value:
document.Words(1).Font.Size = 18
To set Bold, Italic, and Underline properties of a Range object, supply a boolean value:
document.Sentences(1).Font.Bold = true
document.Sentences(1).Font.Italic = true
document.Sentences(1).Font.Underline = true
To indent a paragraph, call its Indent method. For example, to indent all paragraphs in the document:
To un-indent a paragraph, call its Outdent method. For example, to un-indent only the first paragraph:
To align a paragraph, set its Alignment value to one of the following values:
Left = 0
Center = 1
Right = 2
Justify = 3
To center-align the second paragraph, for example:
document.Paragraphs(2).Alignment = 1
That's all for now, and I hope you find it helpful. As always, post a comment here or send me an email to request future topics.
Thanks for stopping by!