5,448,104 members and growing! (16,612 online)
Email Password   helpLost your password?
Languages » C# » COM Interop     Advanced

Creating and Deleting Custom Menus in Visio using C#

By Shashidhar Rao Nellutla

Creating the Visio custom menus and handling the click event from C# programs.This project requires that Visio application should run and it requires a default drawing to be open.
C#, Windows, .NET 1.0, .NET 1.1, .NET 2.0, .NETVisual Studio, VS.NET2002, VS.NET2003, Dev

Posted: 1 Jul 2004
Updated: 1 Jul 2004
Views: 35,081
Bookmarked: 7 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.19 Rating: 2.82 out of 5
2 votes, 33.3%
1
0 votes, 0.0%
2
1 vote, 16.7%
3
1 vote, 16.7%
4
2 votes, 33.3%
5

Introduction

There are several ways to handle the menus in the Microsoft Visio application, but there are no events generated when the user clicks the menu. The best way to handle the menus and menu event handling is through CommandBars. Most of the sites I went never had a solution. Some of the forums mentioned an idea of how to implement. I needed something quicker to find a solution. Then I got through CommandBars. Hope this article will help you in all aspects in handling the menu bar.

With this article, you can not only to handle the menus in Microsoft Visio, but also the menus in some other Microsoft applications. E.g.: Microsoft Word, Microsoft Excel, Microsoft Outlook, etc.

This article concentrates more on Microsoft Visio. In this article, I will show you how to create custom menus and how to handle menu click event handlers.

This demo needs 2 components to be added: those are Microsoft Office 11.0 Object Library, Microsoft Visio 11.0 Type Library.

Code:

This demo contains CustomMenu class which will take care of setting the custom menus, and deleting a custom menu from the standard menu bar of the Visio application.

Let us start with the using declaration:

using Visio=Microsoft.Office.Interop.Visio;

The above statement includes Microsoft Visio interop services.

Secondly, private variables declaration:

private Microsoft.Office.Core.CommandBars commandBars;
private Microsoft.Office.Core.CommandBar commandBar;
private Microsoft.Office.Core.CommandBarButton commandBarButton;

The above variables are use to hold the standard CommandBars of Microsoft Visio application.

private Visio.Application visioApplication;
object visioObject = Marshal.GetActiveObject("Visio.Application");
this.visioApplication = visioObject as Visio.Application;

The above statements holds the visio application reference.

To get the built in menus from the Visio application, the following statement will work:

Visio.UIObject uiObject = this.visioApplication.BuiltInMenus;

After adding the menu to the menu bar, it should be set to standard menu bar of the Visio application with the following statement:

this.visioApplication.ActiveWindow.Document.SetCustomMenus(uiObject);

To get the standard CommandBars of the Visio application, the following statement has to be executed:

commandBars= 
  (Microsoft.Office.Core.CommandBars) this.visioApplication.CommandBars;

Note:

*This demo requires that Visio application should be run, and it requires a default drawing to be open.

Conclusion:

CommandBars can be used for a lot of things: creating menu bars, tool bars, etc. I feel CommandBar is the best way to handle Microsoft applications in your code, and you can play with the applications. I hope you can make good use of this code in your own projects.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Shashidhar Rao Nellutla


Completed Masters in Computer Applications.
Occupation: Software Developer (Senior)
Location: United Kingdom United Kingdom

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralToolBar in Visiomembersunil kumar verma19:00 16 Oct '07  
GeneralSub menu in a menu item(Visio)membersunil kumar verma19:56 7 Oct '07  
GeneralCustom menu for visio 2007memberNithya R19:41 8 Jul '07  
GeneralRe: Custom menu for visio 2007memberShashidhar Rao Nellutla4:39 30 Jul '07  
QuestionTime delay when running the code under VS2005memberRealCosmo1235:55 4 Apr '07  
GeneralProblem with Marshal.GetActiveObject in webservicemembersubhashchandra2:35 12 Dec '05  
QuestionMultiple menu ItemsmemberDr Troll7:13 31 Aug '05  
Generalone suggestionmembersaritha_konda21:38 5 Jun '05  
GeneralCreating and Deleting Custom Menus in Visio using C#memberJayashri8:00 30 Dec '04  
GeneralRe: Creating and Deleting Custom Menus in Visio using C#memberArmoghan Asif0:01 21 Jul '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Jul 2004
Editor: Smitha Vijayan
Copyright 2004 by Shashidhar Rao Nellutla
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project