VBA Add-In Not Working - excel

I have a fully functional Macro that I'm trying to convert to an Add-In so I can use it on all excel documents. However I can't get it create a menu. Please Help.
First Things First:
I'm using Office 2011-Excel on a Mac running Mountain Lion
My .xlam file is saved in the correct folder Applications/Microsoft Office 2011/Office/Add-Ins
I've installed it correctly using the Tools/Add-Ins menu
My code is in the This Worksheet section of the .xlam file
My Add-In's source code is viewable from all documents
My code as follows:
Option Explicit
Dim cControl As CommandBarButton
Private Sub Workbook_Open()
On Error Resume Next 'Just in case
Application.CommandBars("Worksheet Menu Bar").Controls("P Wave").Delete 'Delete any existing menu item that may have been left.
Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add 'Add the new menu item and Set a CommandBarButton Variable to it
With cControl 'Work with the Variable
.Caption = "P Wave"
.Style = msoButtonCaption
.OnAction = "runSheet()"
'Macro stored in a Standard Module
End With
On Error GoTo 0
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next 'In case it has already gone.
Application.CommandBars("Worksheet Menu Bar").Controls("P Wave").Delete
On Error GoTo 0
End Sub
I can't get my Macro menu to appear. Please help

Add your command buttons, or even your own Tab, on the Ribbon although you may have to learn a bit about RibbonX XML. It will give compatibility back to Excel 2007 (I think).
This link may get you started.
Also, This visual designer may be more exciting to get you going. I haven't used it but Andy Pope is the man on all things VBA!

Related

Excel Error when using button made from Custom UI Editor- Cannot run the macro "***". The macros may not be available in this workbook or all macros

I built a series of macros, all of which have worked with the custom ribbon I built using the Custom UI Editor.
I added one more macro and updated my Ribbon with a new button. I test the macro first and it works when run from the developer tab or just running directly from VBA and the sub heading is as follows: "Sub GetDES()".
Once I make it so it works with the button on the ribbon by changing to:
Sub GetDES(control as IRibboncontrol)
I get the "Cannot run macro..." error
Sub GetDES(control as IRibboncontrol)
'PURPOSE: Get description page and insert into each confirm page
Dim Fpath As String
Fpath = ActiveSheet.Range("I6").Value
With Application.FileDialog(msoFileDialogFilePicker)
.ButtonName = "Add this DES!"
.AllowMultiSelect = False
.InitialFileName = Fpath
.Filters.Add "All Pictures", "*.*"
.Show
If .Show = -1 Then
Dim img As Object
Set img = ActiveSheet.Pictures.Insert(.SelectedItems(1))
img.Left = 75
img.Top = shOutput.Range("B40").Top
img.Width = 450
Else
MsgBox ("Cancelled.")
End If
End With
End Sub
The only thing I can think of is I still have something weird happening when I run it without the Control. When I run it the dialouge picker opens twice. I'm wondering if that is my problem with running it from the button on the ribbon.
When I step through the code, the line
set img=Activesheet.Picture.Insert(.SelectedItems(1))
is what causes the file dialogue picker to open again.
Ideas? Thank you
To make sure the callback is invoked by the Office you may try to assign a sample sub to a button, for example, with a simple MsgBox statement to make sure it works correctly.
Sub GetDES(control as IRibbonControl)
MsgBox "test"
End Sub
If that works correctly you can try putting your original code. But you may try to avoid shorthand properties like ActiveSheet or With operator. Hope it helps.
I believe I stumbled across my issue. I built a new module that I wrote this code in, I named the module with the same name as the sub. When Testing, I never went to run it from the macro list, I just ran it from the VBA window. When I looked at the Macro's List window, I saw that the new macro I wrote in this workbook was not displaying like the other macros. As you see in the photo, "Confirm Tool-Master_V5.xlsm'!GetDES.GetDES"
I removed that module and took the copy, listed as GetDES3 and renamed it
GetDES(control as IRibboncontrol)
and it runs from my ribbon. I'm not sure why it looked different or if there was something to do with the module name and the sub being named the same that caused it to be like that. so I tested it. I wrote a test sub and named the module test and it did the same thing.
So, Lesson is, don't name your sub and the module with the same name

WorkbookA macro to create hyperlink in WorkbookB to call a macro in WorkbookA without Worksheet_FollowHyperlink event

I have encountered a fun (I think) problem.
Let's say I have an .xlam Excel Add-In file which contains a macro
(any macro), even let it be Msgbox "Hello World!".
Let's say that I run this Add-In on new Excel Workbooks each time
(received from outside).
I would like the Add-In to insert a hyperlink into the Excel Workbook that would be opened.
I would also like that the inserted hyperlink would call the macro in the Add-In.
Now what I know/what I've tried:
1) If I was opening the same Excel Workbook each time I could just add the 'Follow Hyperlink' event: Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) into the Worksheet where I would like my hyperlink to be ran,
however,
Since I open a new file each time it's not going to have this piece of code in the Worksheet so the 'Follow Hyperlink' even won't work.
2) Now to solve the problem in point "1" I know that I could probably create another macro, which would insert the Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) code into the newly opened Excel Workbook i.e. a VBA code which writes a VBA code, however I don't think that's a very elegant way of solving this problem.
To sum up: I would like the (macro book) to insert a hyperlink into another workbook, and when that hyperlink would be clicked I would like it to run a macro from the 'macro book' WITHOUT using the Worksheet_FollowHyperlink event.
Any ideas? Thanks!
Try using the next simple way of accomplishing what you need, in fact:
Private Sub AddSheetEventCallProc()
'It needs a reference to 'Microsoft Visual Basic for Applications Extensibility x.x'
Dim wb As Workbook, wProj As VBIDE.VBProject, wCom As VBIDE.VBComponent
Dim wMod As VBIDE.CodeModule, Hrng As Range, LineI As Long
Set wb = Workbooks.Add 'any existing workbook can be used here
Set Hrng = wb.Worksheets(1).Range("A1") 'use here the cell you need
'Add the hyperlink:
Hrng.Hyperlinks.Add Anchor:=Hrng, _
Address:="", SubAddress:=Hrng.Address, TextToDisplay:="Call Macro"
With wb
Set wProj = .VBProject
Set wCom = wProj.VBComponents(Worksheets(1).codename)
Set wMod = wCom.CodeModule
With wMod
On Error Resume Next
LineI = .CreateEventProc("FollowHyperlink", "Worksheet"): LineI = LineI + 1
.InsertLines LineI, " If Target.Range.Address = """ & Hrng.Address & """ Then": LineI = LineI + 1
.InsertLines LineI, " Application.Run ""ADDINPRESA.xlam!testMsgbox""": LineI = LineI + 1
.InsertLines LineI, " End If"
On Error GoTo 0
End With
End With
End Sub
The second (more elaborate) way will be to make your add-in create a new Ribbon Tab:
Having the addin, it is good to create a new standard module naming it 'modRibbon', or similar. It is not mandatory, but it is good to separate the ribbon specific module from the one doing the add-in job. So, in this one where you must create the Subs able to be called by controls of the newly Tab to be created:
Option Explicit
Sub Test1(control As IRibbonControl)
appTest1 'use here your Sub name
End Sub
Sub Test2(control As IRibbonControl)
appTest2
End Sub
Sub Test3(control As IRibbonControl)
appTest3
End Sub
It is useful to copy this code in a Notepad window, in order to use it when the xml file will be created.
Then, create the module where the Subs called by the above ones will exist:
Sub appTest1()
MsgBox "It's coming...", , "Test1"
End Sub
Sub appTest2()
MsgBox "It's coming...", , "Test2"
End Sub
Sub appTest3()
MsgBox "It's coming...", , "Test3"
End Sub
Now, you need a, so named, OfficeRibbonEditor application. I use OfficeRibbonXEditor downloaded from here. Please, downoad 'OfficeRibbonXEditor-NETFramework.zip'. After downloading it, extract the file from archive and just use it. No installation need.
Now, run it and press Open button and browse for your add-in workbook and press OK. Select the open add-in and press 'Insert' menu. Select "Office 2010+ Custom UI Part" for Office 2010 or above. For 2007, select the other option. If you need the add-in to be compatible with both cases, both xml files will be inserted. I will show how to create a ribbon Tab only for first variant.
Double click 'customUI14.xml`, which has been created and copy the next code. Now, it is assorted with the above subs, but, in order to make it call your specific subs, use the Notepad window where you copied the Subs name:
Now, close the add-in from Excel and press Validate Button. If you made a mistake in the xml code, the line with the mistake will be highlighted. If everything OK, press 'Save' button and open your add-in. It will create a new Ribbon Tab (TestTab) having three buttons....
There are on the internet ways to find all imageMSO types. It is also possible to use your own icons. I did it some years before. I do not remember exactly how, but you need this option I can check my files archive.
For clarifying issues, a very good resource would be the Ron de Bruin site, which clarifies the way of doing such a Ribbon Tab, from more then ten years. You can find it here.
And the add-in does not 'import' the whole Ribbon. It only creates a new Tab adding it two the existing one and make it disappear when closed.
It is also good to know that when you modify the xml, all modifications done in the VBA add-in code will be lost and viceversa... That's why it is good to take care of closing the other one in the moment you modify something. If necessary, I can post a link to such an add-in done now, but, I would like to believe that it is easy to create one by yourself.
This is the way I create a new Ribbon Tab, but there are some other ways, too. When I will find some time, I will try doing it only in VBA. Practically, you have to read all subs you need to be called by your new Tab, then unzip the Excel file, create or modify the XML file in order to call your subs and rezip the file...

Automatically display the IDE when a workbook is opened

I'm trying to automatically display the IDE when Excel is launched.
Is there a way to simulate a click on the "Visual Basic Editor" icon in the ribbon? I looked into Application.CommandBars but there's nothing about the Ribbon.
Private Sub Workbook_Open()
' Display Visual Basic Editor
End Sub
The Commandbars object has an ExecuteMso method that allows you to "push" any Ribbon button, so:
Application.CommandBars.ExecuteMso ("VisualBasic")
As noted by Comintern, the Application qualification is necessary when using this in a Workbook_Open event, otherwise you'll get an error 91.
To find the mso, go into the Quick Access toolbar's Ribbon menu, find what you want and hover:
It would be
Private Sub Workbook_Open()
' Display Visual Basic Editor
Application.VBE.MainWindow.Visible = True
End Sub
If you get Programmatic Access error:
Programmatic Access To Visual Basic Project Is Not Trusted - Excel

Excel VBA Userform Initialize Error

I've had an Excel VBA addin I've developed over many years. Many users have used this addin on Windows on both Excel 2007 and Excel 2010; it has even worked on Macbook. For the first time, a user is getting an error in Windows Excel 2010 when clicking a button I've added to a ribbon to bring up a form.
The error is 'Compile error: Variable not defined'. I am using Option Explicit throughout the code, but there is no variable being defined when the userform initializes. I've compiled from within the VBA editor using Debug...Compile VBAProject and get no errors. I've googled this and read many posts but nowhere has this error been associated with a userform initializing.
I have this code in the click event of a button I've added to a new ribbon:
Sub DisplayForm4Calc1(ByVal control As IRibbonControl)
On Error Resume Next
frmAA.Show
End Sub
I have this code in frmAA, just initializing some controls:
Option Explicit
Public myPrevious As Boolean
Private Sub UserForm_Initialize()
On Error Resume Next
refNumerator.value = ""
refDenominator.value = ""
MultiPage.value = 3
End Sub
The error is highlighting the line 'Private Sub UserForm_Initialize()'.

Distributing excel macro

I have created a excel macro, now i want it distribute to others who need to use it.
All i want is if some one could please help me with steps to attach the macro to a custom tool bar button and then save both the custom tool bar and to .xla file.
Then the users can store the .xla file on to XLSTART director and when excel is launched custom tool bar appears and readyto use.
This code adds a new menu option, and references VBA methods, so is similiar to what you want.
Add these to the workbook VBA:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
MenuBars(xlWorksheet).Menus("NewMenu").Delete
End Sub
Private Sub Workbook_Open()
On Error Resume Next
MenuBars(xlWorksheet).Menus("NewMenu").Delete
Dim statMenu As Menu
Set statMenu = MenuBars(xlWorksheet).Menus.Add(Caption:="NewMenu")
statMenu.MenuItems.Add Caption:="Item 1", OnAction:="RunFirstItem"
statMenu.MenuItems.Add Caption:="Item 2", OnAction:="RunSecondItem"
End Sub
Then add some methods RunFirstItem & RunSecondItem to the module code.
Save as an .XLA and off you go.

Resources