Welcome to MSDN Blogs Sign in | Join | Help

Visio Automation: Three Hello World Samples – C#, F#, and IronPython

 

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.

image

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"
Published Monday, October 20, 2008 2:30 PM by saveenr

Attachment(s): Visio2007HelloWorldSamples-(2008-10-21).zip

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# infoblog &raquo; Visio Automation: Three Hello World Samples ??? C#, F#, and IronPython

# re: Visio Automation: Three Hello World Samples – C#, F#, and IronPython

I am quite interested in automating Visio with IronPython / Python. I have a gui that I have developed in Visual Basic but haven't coded the callbacks yet; I would like to convert this to IronPython. Do you do consulting work in this area?

Thanks,

James

Tuesday, November 04, 2008 7:10 PM by James Bonanno

# re: Visio Automation: Three Hello World Samples – C#, F#, and IronPython

I don't have Iron Python

I tried with Python 2.6.2 and complains about not finding clr (from the line of import clr).

Where can I get this clr.py file for Python 2.6.2?

Thanks

-Sam Wong

samcywong@hotmail.com

Saturday, July 11, 2009 5:33 PM by Sam Wong

# re: Visio Automation: Three Hello World Samples – C#, F#, and IronPython

@Sam

The python sample as shown requires IronPython. There are two options avaialble to you if you want to use Python 2.6.2 (CPython)

Option #1 Python for .NET: http://pythonnet.sourceforge.net/

This is a module for CPython that allows it to work with .NET objects

The code sample above may have to be modified a bit to run

Option #2 Use COM automation via Python win32extensions

See this blog post: http://blogs.msdn.com/saveenr/archive/2009/02/03/python-vs-ironpython-differences-in-automating-via-com.aspx

Saturday, July 11, 2009 9:03 PM by saveenr

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker