This post is about how to use LINQPad to enable you to write C# against Visio in a similar way you use VBA.
What I like about VBA is that it’s fast, and I’m not talking about code execution necessarily, but the time from being in the Visio interface to actually writing and executing some code. It’s just Alt+F11 and off you go.
If only I could write C# in a similar, light-touch way. Well LINQPad appears to offer just that.
{Disclosure – Just to be clear, I’m lucky enough to get a free licence for LINQPad. However I’m writing about it because I like it and think it’s a great tool. If I didn’t, then I just wouldn’t put pen to paper.]
Although there is a very useful free edition, some of the features I’m going to talk about are only in the paid editions, so be sure to check out the comparison table on the Purchase page.
So what is LINQPad?
LINQPad is a standalone tool that primarily allows you to write and test LINQ queries. You can point at a number of data sources, query them directly and see the results. It’s lightweight and fast to use, which sounds a bit like the VBE!
To support the ability to create queries LINQPad allows you to write in VB.Net, C#, SQL, ESQL and F# and can actually be used to execute standalone C#, but in order to talk to Visio you first need to setup the appropriate references.
Walkthrough
When you open LINQPad you’ll see a default tab representing the your first query.
All queries are isolated from one another and each sits in its own process. So this is a blank canvas and the first thing you need to do is add a reference to the Visio interop assembly.
- Hit F4 to show the references dialog:
- Hit ‘Add’, filter for Visio, and you’ll be presented with a number of versions from the interop assembly depending on your Visual Studio version:
- Once you’ve added the reference, you’ll see your new reference appear in the list:
- Now you’ve got a reference to the correct assembly you then need to add the associated Using/Imports namespaces so that they’re available to your code. So, click on the Additional Namespace Imports tab and add that as well. If you want to add an alias then you can do that by editing directly in the dialog:
Visio = Microsoft.Office.Interop.Visio
Talking to Visio
With all of the references in place you can now start to write some code. You’re supported with code completion and the power of the .NET framework, so for (a simple) example:
1: try
2: {
3: Application vApp = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Visio.Application");
4: }
5: catch (Exception ex)
6: {
7: "Couldn't find running Visio instance".Dump();
8: }
1: void Main()
2: {
3: // Write code to test your extensions here. Press F5 to compile and run.
4: }
5:
6: public static class MyExtensions
7: {
8: // Write custom extension methods here. They will be available to all queries.
9: public static Application GetRunningVisio()
10: {
11: Application vApp = null;
12: try
13: {
14: vApp = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Visio.Application");
15: }
16: catch (Exception ex)
17: {
18: "Couldn't find running Visio instance".Dump();
19: }
20:
21: return vApp;
22: }
23: }
24:
25: // You can also define non-static classes, enums, etc.
- AccelItems
- AccelTables
- Addons
- Colors
- Comments
- Connects
- DataColumns
- DataRecordsets
- Documents
- Fonts
- GraphicItems
- Hyperlinks
- Layers
- Masters
- MasterShortcuts
- MenuItems
- Menus
- MenuSets
- OLEObjects
- Pages
- Path (exposes an array of points)
- Paths
- Selection
- Shapes
- StatusBarItems
- StatusBars
- Styles
- ToolbarItems
- Toolbars
- ToolbarSets
- ValidationIssues
- ValidationRules
- ValidationRuleSets
- Windows