+ Reply to Thread
Results 1 to 2 of 2

How to automatically run add-in when workbook opens

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-01-2010
    Location
    Niles, Michigan
    MS-Off Ver
    Excel 2010
    Posts
    137

    How to automatically run add-in when workbook opens

    I have this Add-In code below that works for me fine when I run it under macro's but how can I get this to run every time this excel file is open? this will be a shared file and most people don't even know how to get to macro's to run them let alone what they are. So I would like to have this Menu Command run when a user opens it so it will always be there. I have copied this from a place before and it works but I just can't get it to run automatically when the workbook opens.

    Sub DeleteMenu()
        On Error Resume Next
        CommandBars(1).Controls("Add Employee or Product Code").Delete
    End Sub
    Sub CreateMenu()
    
    Dim HelpMenu As CommandBarControl
    Dim NewMenu As CommandBarPopup
    Dim MenuItem As CommandBarControl
        'delete the menu if it already exists
        Call DeleteMenu
    
        'find the Help Menu
        Set HelpMenu = CommandBars(1).FindControl(ID:=30010)
    
        If HelpMenu Is Nothing Then
            'add menu to the end
            Set NewMenu = CommandBars(1).Controls _
                          .Add(Type:=msoControlPopup, temporary:=True)
        Else
            'add the menu before help
            Set NewMenu = CommandBars(1).Controls _
                          .Add(Type:=msoControlPopup, before:=HelpMenu.Index, _
                               temporary:=True)
        End If
    
        'Add Caption
        NewMenu.Caption = "&Add Employee or Product Code"
    
        'First Menu item
        Set MenuItem = NewMenu.Controls.Add _
                       (Type:=msoControlButton)
        With MenuItem
            .Caption = "&Add New Product Code"
            .FaceId = 162
            .OnAction = "AddNewProductCode"
        End With
    
        'Second Menu item
        Set MenuItem = NewMenu.Controls.Add _
                       (Type:=msoControlButton)
        With MenuItem
            .Caption = "Add Employee"
            .FaceId = 162
            .OnAction = "AddEmployee"
        End With
    
        'Third Menu item
        Set MenuItem = NewMenu.Controls.Add _
                       (Type:=msoControlButton)
        With MenuItem
            .Caption = "Don't click me!"
            .FaceId = 536
            .OnAction = "checknames"
        End With
    
    
    
    End Sub
    
    Function CommandBarExists(n)
    Dim cb As CommandBar
        For Each cb In CommandBars
            If UCase(cb.name) = UCase(n) Then
                CommandBarExists = True
                Exit Function
            End If
        Next cb
        CommandBarExists = False
    
    End Function

  2. #2
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: How to automatically run add-in when workbook opens

    If you want this to run whenever a workbook opens then you need to save your macro to a standard module in that workbook, and in the "ThisWorkbook" module put something like the below which would call your macro when the workbook opens:
    Private Sub Workbook_Open()
    DeleteMenu
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 07-12-2014, 06:04 AM
  2. [SOLVED] Macro to run a clock in excel automatically when the workbook opens
    By Shellybelly in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-29-2014, 12:48 PM
  3. Replies: 2
    Last Post: 02-17-2013, 05:57 PM
  4. Replies: 1
    Last Post: 06-19-2012, 06:33 AM
  5. 2003 opens new sheet, 2010 opens new workbook
    By AllTheGearNoIdea in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-13-2011, 10:29 AM
  6. Workbook automatically opens after closing
    By SJB in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-08-2010, 09:12 PM
  7. Personal workbook opens when Excel opens
    By SheriTingle in forum Excel General
    Replies: 3
    Last Post: 03-29-2005, 08:06 PM

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