Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how to make a commandbar button in the Visio application ide that runs a routine

Posted on 2009-04-29
6
Medium Priority
?
783 Views
Last Modified: 2013-11-25
I am trying to create a button within the Visio Application Ide and then  make it run a macro. The code below creates the toolbar and button but when I click the button it does not run the macro. Any suggestions much appreciated.

Sub DevSeperateUI()
    Dim CommandBarName As String
    Dim but As CommandBarButton
    
    
    CommandBarName = "Code Utilities"
    Dim i As Integer
    Dim vbideVBE As VBIDE.Vbe
    Dim MyBar As CommandBar
    Set vbideVBE = Visio.Application.Vbe
    For i = 1 To vbideVBE.CommandBars.Count
        If vbideVBE.CommandBars(i).Name = CommandBarName Then
            vbideVBE.CommandBars(i).Delete
        End If
    Next
    Set MyBar = vbideVBE.CommandBars.Add(CommandBarName, 1)
    MyBar.Visible = True
    Set but = MyBar.Controls.Add(msoControlButton)
    but.Visible = True
    With but
        .Caption = "Code Manager"
        .FaceID = 2
        .OnAction = "Hello_World"
        .Enabled = True
    End With
       
End Sub
 
Sub Hello_World()
Call MsgBox("Hello world")
 
End Sub

Open in new window

0
Comment
Question by:dilesh99
  • 3
  • 2
6 Comments
 
LVL 31

Expert Comment

by:Scott Helmers
ID: 24272783
The issue appears to be in calling from a VBE command bar to code in the application. The code below adds a command bar button to the application instead of the to VBEditor and it works correctly. (NOTE: if your code is in the ThisDocument module, you need to change
   .OnAction = "Hello_World"
to
   .OnAction = "ThisDocument.Hello_World"

However, I tried the fully qualified
   .OnAction = "XYZ!Module1.Hello_World"
where XYZ is the project name and that didn't work either. Consequently, I think one of two things is required: 1) different syntax to pass control to the application code from the VBE command bar, or 2) setting a reference to the application (I tried this but a reference to the current project is implied, so this probably isn't the right thing either).

Any Office VBA experts who can tell us what's missing??

Scott


 Sub App_UI()
    Dim CommandBarName As String
    Dim but As CommandBarButton
    
    CommandBarName = "Code Utilities"
    Dim i As Integer
    Dim MyBar As CommandBar
    For i = 1 To Application.CommandBars.Count
        If Application.CommandBars(i).Name = CommandBarName Then
            Application.CommandBars(i).Delete
        End If
    Next
    Set MyBar = Application.CommandBars.Add(CommandBarName, 1)
    MyBar.Visible = True
    Set but = MyBar.Controls.Add(msoControlButton)
    but.Visible = True
    With but
        .Caption = "Code Manager"
        .FaceID = 2
        .OnAction = "Hello_World"
        .Enabled = True
    End With
       
End Sub
 
Sub Hello_World()
Call MsgBox("Hello world")
 
End Sub

Open in new window

0
 

Author Comment

by:dilesh99
ID: 24274034
Scott,

Thank you for your note.

As you say, the issue is how to add the commandbar and button on the VBEditor side as opposed to the application. The reason we want to do this is because we are creating a coding helper for our VBA code and need a way to initiate it from a button while we are in the VBA environment. The original code does create the commandbar and button in the VBEditor but we cannot make it connect to the routine we want to call..
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 24333799
This has taken a lot of research and adaption but at the end it's easy enough

Refer to the following link for the general process:

http://support.microsoft.com/kb/280607

The two code sets I have used for the purpose, (with your functionality) are pasted below

Chris
' A NORMAL CODE MODULE
Dim oBtns As New Collection
 
Function A()
   Dim cb As CommandBar
   Dim oEvt As CBTnEvent
   Dim CommandBarName As String
    
   CommandBarName = "Code Utilities"
 
   Set oBtns = Nothing
   On Error Resume Next
   Application.VBE.CommandBars(CommandBarName).Delete
   On Error GoTo 0
   Set cb = Application.VBE.CommandBars.Add(CommandBarName)
   cb.Visible = True
   cb.Position = msoBarTop
 
   Set oEvt = New CBTnEvent
   Set oEvt.oBtn = cb.Controls.Add(msoControlButton)
   With oEvt.oBtn
      .Style = msoButtonIconAndWrapCaption
      .Caption = "Code Manager"
   End With
   oBtns.Add oEvt
End Function
 
'THE CLASS MODULE
Public WithEvents oBtn As CommandBarButton
 
Private Sub oBtn_click(ByVal ctrl As Office.CommandBarButton, CancelDefault As Boolean)
   MsgBox "Hello"
End Sub

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 31

Expert Comment

by:Scott Helmers
ID: 24338606
Nice work, Chris! As you say, in the end it's easy but it's critical to know that you need to revert to an event because onaction won't work in this case.

Regards,
Scott
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 24338709
Exactly ... and I didn't know that but started to get the idea from web searches before I happened across the MS page which was fo me clear and presumably for everyone else too.

For info you will not be surprised it seems to work in the other apps (where I have tried it) as well BUT not Visio2000 it seems.

Chris
0
 
LVL 31

Expert Comment

by:Scott Helmers
ID: 24338860
I'm not too surprised about Visio 2000 -- if I recall, that was the first version Microsoft released after they acquired Visio Corp. and I don't think they did very much to Office-ify the software until Visio 2002.

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Loops Section Overview

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question