Skip to content

Instantly share code, notes, and snippets.

@kirushik
Created August 27, 2010 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirushik/553265 to your computer and use it in GitHub Desktop.
Save kirushik/553265 to your computer and use it in GitHub Desktop.
Работа в MSWord из Руби
#Original code by Carlos Tirado, http://www.ruby-forum.com/topic/99742#214485
require 'win32ole'
word = WIN32OLE.new('word.application')
word.visible = true
word.documents.count
# open/create new document
word.documents.add
# or open file
word.documents.open(path_to_file)
# type something
word.selection.typetext("Hello World!\n")
# select whole text
word.selection.wholestory
# delete selection
word.selection.delete
# move to start of document
word.selection.start = 0
word.selection.end = 0
# search for a string
word.selection.find.text = 'my search'
result = word.selection.find.execute
# read the selection
puts word.selection.text
# and position
puts word.selection.start
puts word.selection.end
# or set the position, and selection
word.selection.start = 20
word.selection.end = 23
puts word.selection.text
# printing
word.options.printbackground = false
word.activedocument.PrintOut
# SAVING document
word.activedocument.saveas file_name, wdFormatText
# notice the 2nd parameter which is a numeric constant
# indicating the file format to save in thusly:
# I believe omitting 2nd parameter would save in native .doc)
# wdFormatDocument = 0 (no conversion)
# wdFormatTemplate = 1
# wdFormatText = 2
# wdFormatTextLineBreaks = 3
# wdFormatDOSText = 4
# wdFormatDOSTextLineBreaks = 5
# wdFormatRTF = 6
# wdFormatUnicodeText = 7 # it repeats!
# wdFormatEncodedText = 7
# wdFormatHTML = 8
# wdFormatWebArchive = 9
# wdFormatFilteredHTML = 10
# wdFormatXML = 11
# close document
word.activedocument.close( true ) # presents save dialog box
word.activedocument.close( false ) # no save dialog, just close it
# quit word
word.quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment