Thursday, June 4, 2009

Ruby & Word: Creating and Applying Styles

Microsoft Word uses the Styles model to apply a set of pre-defined formatting to text. Styles can also serve a second purpose, to tag sections of the document as normal, title, headings and such. You can then, for example, create a Table of Contents in Word based on the text that is formatted with the Heading styles.

Naturally, you can do all this with code (otherwise, I wouldn't be wasting your time here). Over the next few minutes, we'll walk through the process of creating a new Style, setting its properties, and then applying that style to text.

The Style object represents a single built-in or user-defined Word style. The Styles collection contains all the Style objects within a document. To reference the Styles collection, simply call the Styles method on the document:


doc = word.ActiveDocument
styles = doc.Styles

To create a new Style, we call the Add() method on the Styles object and pass it a hash defining the name and the type of the new Style. The following code creates a new Paragraph style named 'Code':

code_style = doc.Styles.Add({'Name' => 'Code', 'Type' => 1})

The Type property defines what StyleType your new Style is based on. Possible values are:

WdStyleTypeParagraph = 1
wdStyleTypeCharacter = 2
WdStyleTypeTable = 3
WdStyleTypeList = 4

The default is WdStyleTypeParagraph.

The Add() method returns a reference to the newly-created Style object. Now that you have your new Style object, you can customize it through various properties that you can set. As a starting point, you may want to base your new style on another style by setting the BaseStyle property:

code_style.BaseStyle = 'Normal'

Font properties can be defined...

code_style.Font.Name = 'Consolas'
code_style.Font.Size = 12
code_style.Font.Bold = false

...as well as paragraph spacing and background colors:

code_style.NoSpaceBetweenParagraphsOfSameStyle = true
code_style.Shading.BackgroundPatternColor = 15132390

Note that not all properties will apply to all style types.

Now that you've created your own Style, you might want to automatically apply it to some existing text. The following code iterates over each paragraph in the document (doc variable). For each paragraph that uses the 'Preformatted Text' style, the new 'Code' style is applied instead:

doc.Paragraphs.each do |paragraph|
if paragraph.Style.NameLocal == 'Preformatted Text'
paragraph.Style = 'Code'
end
end

There you have it. If you'd like to learn more here about Styles, or anything else related to automating Word with Ruby, please let me know.

Thanks for stopping by!

6 comments:

Anonymous said...

Hi David,

I haven't been following your blog recently since your postings dropped off in March and April. I am happy to see you posting again.

Is there any word on your book yet?

Also, do you do any Rails programing? I'd like to know how to kick off many of your scripts by creating docs on the fly through Rails.

At any rate, I'm glad your back.

Paul

David Mullet said...

Thanks, Paul!

The book is under development and will be released in a few months.

I haven't done much with Rails beyond setting up a simple website. But I'd be happy to try to answer any questions you might have.

David

Paul said...

I was hoping you might have some experience in generating MS docs on the fly from Rails based web sites.

All your post regarding Word, Excel and Powerpoint are great! I just need to be able to do some of these things remotely rather than have Ruby code executing on the client. This would be for internal applications only.

Who is publishing your book?

Thanks David!

Paul said...

Also....

Do you schedule and automate any of the Ruby programs you create to interface with MS apps? A rudimentary reporting system perhaps?

Mohit Sindhwani said...

I'm glad to see your posts back! I've learnt whatever little I know about Ruby + Windows Automation from your site. I've recently been toying with the idea of using TexTile + Word automation to create some sort of simple documents and one of the things I need to do is to insert stuff into Word documents (the other big thing is figuring out how to change RedCloth).

Thanks for all your help.

David Mullet said...

@Paul-

I haven't done much with Rails, beyond a website or two. Is your website Windows-based? Or Linux? Feel free to email me to discuss details.

@Mohit:

Thanks for the kind words. Good luck to you on your TexTile/Word project.

David