In my line of work I end up trying to move a large amount of text from web pages into things like Word and Excel. Getting the Text from the Web Browser is easy…Putting the text with formatting into an Office app could be a lot of work trying to parse through all the HTML and send the equivalent formatting commands with the text. Fortunately, Word and Excel had the ability to paste from the clipboard HTML and render it with the correct formatting! …Only problem is that for some reason, no one added HTML support in the Win32::Clipboard gem?!? (At least not in the 1.8.x version of Ruby, which I use.) So I spent a few hours looking over the existing clipboard.rb file, found a VBA example of an HTML Copy, and came up with the following code: Continue reading »
Ever wanted to have a spell check function in your Ruby Program? As long as you have MS Word installed, you can use the Spellcheck function from your program. Here’s the code:
require 'exo/iswindows'
if not RUBY_PLATFORM.isWindows?
puts "This program only runs under Windows!"
exit
end
require 'win32ole'
def spellcheck(scstring)
word = WIN32OLE.new('Word.Application')
doc = word.Documents.Add ## Blank document
word.Selection.Text = scstring
word.Dialogs(828).Show
### return the corrected text
if not scstring[/ /] ## only one word with no spaces in the string
# highlight the word first,
word.Selection.MoveLeft( 'Unit'=>2,
'Count'=> 1,
'Extend'=>2)
end
## multiple words end up already selected after the spell check
# then retrieve.
correct = word.Selection.Text
doc.close(0)
word.Quit
return correct
end
if __FILE__ == $0
puts "Corrected => #{spellcheck(ARGV[0])}"
end
The program opens a new document, pastes the text to check into the document, and then brings up the spellcheck dialog box with any words it can’t find in the dictionary and prompts you to correct the mistakes. Here’s an sample output:
C:\Server6\Dev\Ruby>ruby spellcheck.rb "this is a test srting to seee how the slpell check is working" Corrected => This is a test string to see how the spell check is working C:\Server6\Dev\Ruby>ruby spellcheck.rb antidisestablishmenttarianism Corrected => antidisestablishmentarianism
For some reason, if you check multiple words, the dialog auto-closes after the last ‘fix’ …but if its just one word, it stays open until you click the close button. Conversely, multiple words stay selected in the Word doc after the dialog closes, but with only one word, it does not stay selected, and you have to select it back in order to read it off the page of the document.
Over at the Ruby on Windows blog, David posted an article about how the latest versions of Office allow for saving files as PDF. Which prompted me to ask how you can find out what the versions are, since Office 2003 apps don’t have this option. So in between watching the AFC Championship game, I figured it out and came up with the following program:
require 'win32ole'
xl = WIN32OLE.new('Excel.Application')
xl.visible = false
ver = nil
case xl.Version
when "12.0"
ver = "Excel 2007"
when "11.0"
ver = "Excel 2003"
else
ver = "{Unknown Version => #{xl.Version}}"
end
puts " Excel Version: #{ver}"
xl.Quit
word = WIN32OLE.new('Word.Application')
word.visible = false
ver = nil
case word.Version
when "12.0"
ver = "Word 2007"
when "11.0"
ver = "Word 2003"
else
ver = "{Unknown Version => #{word.Version}}"
end
puts " Word Version: #{ver}"
word.Quit
ppt = WIN32OLE.new('Powerpoint.Application')
#ppt.visible = false ## Powerpoint 2007 does not allow itself to be hidden?!?
ver = nil
case ppt.Version
when "12.0"
ver = "Powerpoint 2007"
when "11.0"
ver = "Powerpoint 2003"
else
ver = "{Unknown Version => #{ppt.Version}}"
end
puts "Powerpoint Version: #{ver}"
ppt.Quit
ol = WIN32OLE.new('Outlook.Application')
#ol.visible = false
ver = nil
## Outlook seems to tack on a build number
tt = ol.Version.slice(0,4)
case tt
when "12.0"
ver = "Outlook 2007"
when "11.0"
ver = "Outlook 2003"
else
ver = "{Unknown Version => #{ol.Version}}"
end
puts " Outlook Version: #{ver}"
ol.Quit
I noticed in my last entry that I used a function that I didn’t describe. When sending filenames to applications like Excel 2007 or Word 2007, they are expecting the path to use the backslash ‘\’ character, rather than the Ruby default of a forwardslash ‘/’ that is returned from the DIR class. Older versions of Excel or Word don’t seem to care as they accept either one. But to be safe, I always use this built-in function from the Windows Scripting engine to make sure my paths are correct.
require 'win32ole'
def getAbsolutePathName(file)
fso = WIN32OLE.new('Scripting.FileSystemObject') # VBA File System commands
return fso.GetAbsolutePathName(file)
end






