Automating Microsoft Word can involve hundreds of objects, each with its own properties and methods. You can use the ole_methods method, or Word's built-in Object Browser, to browse the objects, properties, and methods. For further details, see this article.
If you've read some of my articles about automating Excel with Ruby, you will see similarities between the two object models.
Let's start by looking at Microsoft Word's top-level Application object.
You'll use the win32ole library to create a new instance of the Word application object:
require 'win32ole'
word = WIN32OLE.new('Word.Application')
...or to connect to an existing instance of the Word application object:
word = WIN32OLE.connect('Word.Application')
document = word.ActiveDocument
I often use the
connect method for ad hoc scripts to perform a series of actions on a Word document (or Excel workbook) that I already have open.
Note that a new instance of Word will be not visible by default. To show it, set the application object's Visible property to true:
word.Visible = true
You may want to first hide Word until your data input and formatting process is complete, then make it visible. This may speed things up, and prevents the user from interfering with your program (and vice versa).
You'll spend most of your time working with Document objects, but first let's look at some of the properties and methods for the Application object.
Call the
Version method to determine the version of Word, which is returned as a decimal string (ie, "11.0"):
if word.Version.to_i => 11
# do something
else
# do something else
end
You can set the
PrintPreview property to place Word in print preview mode:
word.PrintPreview = true
word.PrintPreview = false # return to previous view mode
The
System object includes a number of (read-only) properties about the system that Word is running on. Most of these will be of no use whatsoever to you, but a few may prove helpful:
word.System.OperatingSystem # => "Windows NT"
word.System.HorizontalResolution # => 1440
word.System.VerticalResolution # => 900
word.System.LanguageDesignation # => "English (U.S.)"
The
FontNames, PortraitFontNames, and LandscapeFontNames methods return collections of font names:
for font in FontNames
puts font
end
The
Tasks method returns a collection of Task objects, representing tasks being run on the PC. Most of these tasks will be processes that you have no interest in. But you can, for example, use this to determine quickly if a particular application is currently running and, if so, address that application's window:
if word.Tasks.Exists('Firefox')
word.Tasks('Firefox').Activate
word.Tasks('Firefox').WindowState = 1 # maximize
word.Tasks('Firefox').WindowState = 2 # minimize
word.Tasks('Firefox').WindowState = 0 # restore to normal
end
Calling
ole_methods on one of these Task objects reveals what we may do with it:
irb(main):004:0> word.Tasks('Firefox').ole_methods
=> [QueryInterface, AddRef, Release, GetTypeInfoCount,
GetTypeInfo, GetIDsOfNames, Invoke, Application, Creator,
Parent, Name, Left, Left, Top, Top, Width, Width, Height,
Height, WindowState, WindowState, Visible, Visible,
Activate, Close, Move, Resize, SendWindowMessage,
GetTypeInfoCount, GetTypeInfo, GetIDsOfNames, Invoke]
So we see that we can, for example, hide, unhide, move, resize, and close other application windows through the Task object:
word.Tasks('Firefox').Visible = false
word.Tasks('Firefox').Visible = true
word.Tasks('Firefox').Top = 100
word.Tasks('Firefox').Left = 300
word.Tasks('Firefox').Height = 500
word.Tasks('Firefox').Width = 500
word.Tasks('Firefox').Close
You should, naturally, use the Tasks collection with care, as you could wreak havoc otherwise.
To quit Word, call the
Quit method:
word.Quit # Do not save changes
word.Quit(0) # Do not save changes
word.Quit(-1) # Save changes
word.Quit(-2) # Prompt user to save changes
That's all for now. Next up, we'll look at working with the Document object. As always, let me know if there are any specific topics you would like to see discussed here.