preload
Feb 01

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 »

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
Tagged with:
Jan 31

I’ve been working with Visio lately and noticed that when my programs start it up, Visio was not coming up maximized. Since I wanted to be able to see my work full screen, I started looking for a way to force it to maximized mode. I quickly learned that there is no convenient ‘visio.Maximize’ method to use. Bummer. Other Office apps have a ‘.WindowState’ parameter that can be set to minimize, maxamize, restore, or hide the application window, but not Visio. Guess that would make it too easy ;)

For Visio, you can use the ‘ShowWindow’ function from the user32.dll to do the work:

require 'Win32API'

##Possible cmd values:
# Hidden => 0
# Restored => 1
# Minimized => 2
# Maximized => 3

def ShowWindow(wndHandle, cmd)
   wndShowWindow = Win32API.new("user32", "ShowWindow", ["p","i"], "i")
   wndShowWindow.call(wndHandle, cmd)
end

wndHandle‘ is the infamous Window Handle used in the lower-level Windows APIs. At least Visio has a way to return the handle. So in your program:
Continue reading »

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
Tagged with: