Periodically I get a “how do I get started?” question about using automation Visio. So, in the interests of sharing, I’ll show 3 simple “Hello World” examples that you can build on for more complex tasks.
All these samples do the same thing: launch Visio 2007, create a new doc, and draw a rectangle with the text “Hello World”. Below is a sample of the output.
C# (Visual Studio 2008)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// First Add a reference to the Visio Primary Interop Assembly:
// In the "Solution Explorer", right click on "References", select "Add Reference"
// The "Add Reference" dialog will launch
// then in the ".NET" Tab select "Microsoft.Office.Interop.Visio"
// Click "OK"
using IVisio = Microsoft.Office.Interop.Visio;
namespace Visio2007AutomationHelloWorldCSharp
{
class Program
{
static void Main(string[] args)
{
IVisio.ApplicationClass visapp = new IVisio.ApplicationClass();
IVisio.Document doc = visapp.Documents.Add("");
IVisio.Page page = visapp.ActivePage;
IVisio.Shape shape = page.DrawRectangle(1, 1, 5, 4);
shape.Text = "Hello World";
}
}
}
F# (Visual Studio 2008)
#light
// Step 1 - Add a reference to the Visio Primary Interop Assembly
// In the "Solution Explorer", right click on "References", select "Add Reference"
// The "Add Reference" dialog will launch
// then in the ".NET" Tab select "Microsoft.Office.Interop.Visio"
// Click "OK"
let visapp = new Microsoft.Office.Interop.Visio.ApplicationClass()
let doc = visapp.Documents.Add("")
let page = visapp.ActivePage;
let shape = page.DrawRectangle(1.0, 1.0, 5.0, 4.0)
shape.Text <- "Hello World";
IronPython (IronPython 2.0 Beta 4)
import sys
import clr
import System
clr.AddReference("Microsoft.Office.Interop.Visio")
import Microsoft.Office.Interop.Visio
IVisio = Microsoft.Office.Interop.Visio
visapp = IVisio.ApplicationClass()
doc = visapp.Documents.Add("")
page = visapp.ActivePage
shape = page.DrawRectangle(1, 1, 5, 4)
shape.Text = "Hello World"