DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 23 posts at DZone. View Full User Profile

Ruby & Word: Creating And Applying Styles

06.07.2009
| 3566 views |
  • submit to reddit
        From the <a href="http://rubyonwindows.blogspot.com/">Ruby on Windows</a> blog...

require 'win32ole'

word = WIN32OLE.connect('Word.Application')
doc = word.ActiveDocument

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

code_style.BaseStyle = 'Normal'
code_style.Font.Name = 'Consolas'
code_style.Font.Size = 12
code_style.Font.Bold = false
code_style.NoSpaceBetweenParagraphsOfSameStyle = true
code_style.Shading.BackgroundPatternColor = 15132390

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

Further details and discussion <a href="http://rubyonwindows.blogspot.com/2009/06/ruby-word-creating-and-applying-styles.html">here</a>.