Need help with Menu Items in Visio Add-in.

Started by yoursashok, March 18, 2010, 10:45:57 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

yoursashok

Hi,

I'm developing a Visio Add-in in C# using Visual Studio. The Add-in will create a Menu Bar and Menu Item in the Visio Application. On clicking the Menu Item a file chooser will be opened. Everything works fine when the Visio is opened for the first time. But after adding a new Document via the Add-in code or in the Visio Application, the Menu Item click is not getting fired or is not coming to the Button Click listener. The Menu Bar and Menu Item are still visible, but the event is not getting triggered.

Any help on this is greatly appreciated.

Thanks,
Ashok.

yoursashok

Hi,

Wanted to share more information so that someone here can help me out. I had created a new CommandBar and a CommandBarButton in the Visio which is shown as a new Menu Bar with a single Menu Item. I have a button click menu handler, which opens a FileChooser dialog and imports a file to it. The MenuBar and MenuItem are all created in the OnStartupComplete function. The methods used are as follows,

       public void AddMenuToMenuBar(Microsoft.Office.Interop.Visio.Application application, string caption)
       {
           if (application != null)
           {
               try
               {
                   applicationCommandBars =
                   (CommandBars)application.CommandBars;
                   menuBar = applicationCommandBars.ActiveMenuBar;

                   // Add a pop-up menu to the end of the active menu bar.
                   menuPopup =
                   (CommandBarPopup)menuBar.Controls.Add(MsoControlType.msoControlPopup, 1, "", 10, true);
                   menuPopup.Caption = caption;
               }
               catch (Exception err)
               {
                   LogMessageToFile(err.Message);
                   throw;
               }
           }
       }

       public void AddMenuItemToMenuBar(CommandBarPopup menuPopup, string menuItemCaption)
       {
           if (menuPopup != null)
           {
               try
               {
                   menuItem = (CommandBarButton)menuPopup.Controls.Add(MsoControlType.msoControlButton, 1, "", 1, true);
                   menuItem.Caption = menuItemCaption;
                   menuItem.Style = MsoButtonStyle.msoButtonCaption;
                   menuItem.Visible = true;
                   menuItem.Click += new _CommandBarButtonEvents_ClickEventHandler(MenuItem_Click);
               }
               catch (Exception err)
               {
                   LogMessageToFile(err.Message);
                   throw;
               }
           }
       }


And my MenuItem_Click function is as follows,

       private void MenuItem_Click(CommandBarButton control, ref bool cancelDefault)
       {
           try
           {
               OpenFileDialog ofd = new OpenFileDialog();
               ofd.Filter = "All files (*.*)|*.*";
               ofd.InitialDirectory = "C:\\";
               ofd.Title = "Choose a File to generate Visio Diagram";
               string file = (ofd.ShowDialog() == DialogResult.OK) ? ofd.FileName : null;
               LogMessageToFile("File Chosen for creating visio " + file);
               if (file == null)
               {
                   MessageBox.Show("No File Chosen");
               }
               else
               {
                   GenerateVisioDiagram(file);
               }
           }
           catch (Exception ee)
           {
           }
       }


In the GenerateVisioDiagram, I open a new document to the existing application,

       public void GenerateVisioDiagram(string inputFile)
       {
           try
           {
               Microsoft.Office.Interop.Visio.Documents documents = visioApplication.Documents;


               Microsoft.Office.Interop.Visio.Document currentDocument = documents.Add("");

               // Set the Name of the page
               Microsoft.Office.Interop.Visio.Page currentPage = currentDocument.Pages[1];

               // Other Logic to generate a diagram in the page

        }


For some reasons, the Menu Item that I created stops firing any events after the documents.Add("") method call.

I have the commandBars, commandBar, commandBarButtom and the commandBarPopUp all declared as class variables as follows,

       CommandBarButton menuItem;
       CommandBar menuBar;
       CommandBars applicationCommandBars;
       CommandBarPopup menuPopup;


If any of you are aware of the reason why the Menu Item click event is not fired after the first time, please share it here. Any help will be greatly appreciated. If there are other C# examples to perform these operations, please share that information too. Sorry I'm new to this area.

Thanks,
Ashok.

yoursashok

Hi,

The problem got solved after adding OnAction property of the CommandBarButton. Thought of sharing this.

Thanks,
Ashok.

Yacine

In the name of the whole forum: we are sorry!
;D ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D
Yacine