I found it a bit odd that there doesn’t seem to be any tutorials on how to automate Microsoft Visio using Ruby. Most of the other Office apps have a bunch of pages on automation, but none on Visio….that or my Google Fu is leaving me
So…. Here’s a brief tutorial on how to automate Visio using Ruby. First thing you need to do is bring in the WIN32OLE module and create a new instance of the Visio application
require 'win32ole' visio = WIN32OLE.new('Visio.Application')
If you already have Visio running, you can connect to it with this line instead
visio = WIN32OLE.connect('Visio.Application')
Once you have your instance, its usually best to load in all the constants from the application. Visio has tons and tons, so if you are going to try and convert any VBA or C++ code over, you will need these.
class VisioConst # Empty class to hold constants end ... WIN32OLE.const_load(visio, VisioConst)
Be sure to put the class declaration below the ‘require’ statements so your code will read better. Now we will load in a Visio template just to make things easier on us. Continue reading »