« Another blog for Visio Developers | Main | Notes on Using Client Applications to Generate Server-Side Graphics »
Monday
Oct202008

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"

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (1)

Thanks any other visio addin related code is there to Share pls

November 26, 2014 | Unregistered CommenterGuna

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>