+ Reply to Thread
Results 1 to 12 of 12

Share a toolbar that calls macro from an add-in

Hybrid View

  1. #1
    Registered User
    Join Date
    07-16-2007
    Posts
    43

    Share a toolbar that calls an add-in macro

    I have created some macros that need to be used in several workbooks by more than one users, so I put them in an add-in and gave them instructions on how to get the add-in. I also created a toolbar with buttons calling those macros, attached it to a workbook the users can open, and instructed them to open that workbook once to get the toolbar.

    Both the add-in and the toolbar workbook are on a network drive that my users can access, but when they get the add-in, it gets copied to their own add-in folder.

    Here's my problem:
    When a user tries to click on one of the buttons in the toolbar, the button tries to call the macro using the path that would be valid on my computer to get to my add-in folder. This path includes my user name, so it doesn't go to their own add-in folder: it goes to my add-in folder on that computer if I ever logged in on that particular machine, or it goes nowhere.

    When I created the button, I attached the macros by referring to the add-in workbook by name only, without referring to a path, so I don't know why MY path ended up defined on that toolbar.

    How can I go around this? I manually relinked all the buttons for that user, and I can do it for the other users (there aren't that many users nor that many buttons), but I'd rather avoid it: I'll have to re-do it if the toolbar is updated and it just seems like more work than should be necessary.

    I'm also opened to suggestions on a better way to share those macros. They can't be attached to the workbooks because we use new workbooks every year - they're emailed to us by the people who create them and for a variety of reasons, pasting everything into a workbook of our own is not a good option.

    Thanks for any help,

    Brigitte
    Last edited by Brigitte_P; 03-19-2008 at 08:26 AM. Reason: Bumping

  2. #2
    Registered User
    Join Date
    07-16-2007
    Posts
    43

    Bump

    Just Bumping. Anyone has any idea? How do YOU share macros to be used in several workbooks?

    Brigitte

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    It's much better to add your ToolBar within the code, there's many examples this is one I have edited from one of my existing workbooks.

    Place this in a Standard Module, I have named the module mToolBars and it is added to the Data menu.

    Option Explicit
    '////////////////////////////////////////////////
    '
    '
    '   This module contains all code specific to the
    '   creation and deletion of the various buttons
    '   that are added to Excel's menus.'
    '
    '////////////////////////////////////////////////
    
    Const APPNAME As String = "MyAddin"
    
    Public Sub AddButtonsMain()
        On Error GoTo errHandler
        Call mToolbars.RemovesButtons
        'Add a button to the Data menu.
        AddButton Application.CommandBars("Data")
        Exit Sub
    errHandler:
       MsgBox "Toolbar could not be created"
    End Sub
    
    
    Private Sub AddButton(ComBar As CommandBar)
        'Adds the "JR Applications" button to the specified commandbar
    
        Dim ComPop As CommandBarPopup, ComBut As CommandBarButton
        Dim lngCurrentRow As Long
        Dim lngLastRow As Long
    
        On Error GoTo errHandler
    
        Set ComPop = ComBar.Controls.Add(Type:=msoControlPopup, before:=1)
        ComPop.BeginGroup = True
        ComPop.Caption = APPNAME
        Set ComBut = ComPop.Controls.Add(msoControlButton)
        ComBut.BeginGroup = True
        ComBut.Caption = "Load Form"
        ComBut.OnAction = "ShowDataForm"
        ComBut.Style = msoButtonIconAndCaption
        ComBut.FaceId = 137
    
        Exit Sub
    
    errHandler:
      MsgBox "The toolbar could not be added"
      End Sub
    
    
    Public Sub RemovesButtons()
        Dim ComBar As CommandBar
        Dim ComCtl As CommandBarControl
    
        On Error GoTo errHandler
        For Each ComCtl In Application.CommandBars("Data").Controls
            If ComCtl.Caption = APPNAME Then ComCtl.Delete
        Next ComCtl
        Exit Sub
    
    errHandler:
     
    End Sub
    This code is important to make sure that the ToolBar is loaded correctly & unloaded when not needed.
    Option Explicit
    
    Private Sub Workbook_AddinInstall()
        Call mToolbars.AddButtonsMain
    End Sub
    
    Private Sub Workbook_AddinUninstall()
    Call mToolbars.RemovesButtons
    End Sub
    Last edited by royUK; 03-19-2008 at 08:47 AM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Here's an example that adds it's own Toolbar
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    07-16-2007
    Posts
    43
    Thanks for the reply, I'll try that!

    Brigitte

  6. #6
    Valued Forum Contributor Macdave_19's Avatar
    Join Date
    03-14-2007
    Location
    Birmingham, England
    MS-Off Ver
    12.0
    Posts
    808

    You Could Try

    try changing the Username part of the file location by adapting this code

    Private Declare Function GetUserName Lib "advapi32.dll" _
        Alias "GetUserNameA" (ByVal lpbuffer As String, _
                              nsize As Long) As Long
    
    Sub get_user_name()
    Dim lpbuff As String * 25
    Dim ret As Long
    Dim strName As String
    ret = GetUserName(lpbuff, 25)
    strName = Left(lpbuff, InStr(lpbuff, Chr(0)) - 1)
    End Sub
    Something Like "C:\Documents and Settings\........" & strName

    P.s. code thanks to ROYUK (Legend)

    HTH
    Mr MaGoo
    Magoo.Inc MMVII

    If i've helped please add to my Rep by Clicking on the Blue Scales in the top right hand corner of the post

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1