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
Related
I am trying to emulate Ctrl-P in Excel 2013 where the print dialog box is shown on the left with the print preview on the right.
(Although where the preview displays, I always have to click "Show Print Preview" first. I can't find a way to force the preview to show every time).
I tried the following:
Application.Dialogs(xlDialogPrint).Show
This shows the old style dialog box where you need to click the "Preview" button
ActiveSheet.PrintPreview
This shows the preview but doesn't allow the printer to be changed from the same screen.
Something like this?
Excel
Option Explicit
Public Sub Example()
Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
End Sub
CommandBars.ExecuteMso Method (MSDN) is useful method in cases where there is no object model for a particular command.
For Outlook
Option Explicit
Public Sub Example()
Dim Inspector As Outlook.Inspector
Set Inspector = Application.ActiveInspector
If Not Inspector Is Nothing Then
Dim cmd As Office.CommandBars
Set cmd = Inspector.CommandBars
cmd.ExecuteMso ("FilePrintPreview")
Else
ActiveExplorer.selection(1).Display
Set cmd = ActiveInspector.CommandBars
cmd.ExecuteMso ("FilePrintPreview")
End If
End Sub
I am running a spreadsheet that has a macro. Within this macro there is a Userform. This userform has a button that when pressed opens the print preview of data in a spreadsheet. When I execute the "PrintPreview" method, all buttons the page setup tools are disabled, as can be seen in the image below.
Below is the code I'm running
Private Sub Workbook_Open()
UserForm1.Show
End Sub
The code below is inside UserForm1
Private Sub CommandButton1_Click()
Dim MySheet As Worksheet
Application.ScreenUpdating = False
Set MySheet = Worksheets("Plan3")
MySheet.Select
MySheet.Range("$A1:$AI166").Select
MySheet.PageSetup.PrintArea = "$A$1:$AI$166"
ActiveWindow.View = xlNormalView
Application.ScreenUpdating = True
Me.Hide
MySheet.PrintPreview
Me.Show
End sub
I couldn't find the reason why the Page Setup Tools buttons is disabled. Can someone help me?
Note 1: This code is inside a userform. The userform is loaded into the Workbook_Open() event.
Note 2: When I open the spreadsheet by double clicking on the file, the userform is loaded automatically. Clicking the CommandButton1 button displays the print preview screen, however the Page Setup Tools buttons is disabled.
If I open the Visual Basic editor and run the Workbook_Open(), clicking the CommandButton1 button shows the print preview screen and the Page Setup Tools buttons are enabled. That is, if the macro runs with the Visual Basic editor open the problem does not happen.
Note 3: This is only happening in the 2007 version of excel.
Note 4: I did a test by opening UserForm1 in "(vbModeless)" mode. In this case the problem did not occur. However in the application I am developing, I need to open the userform in "(vbModal)" mode.
My friend shared a .bas file with me and told me to save it as .xlam in the vbEditor to have it as an Addin.
I've browsed in Add-ins and am able to enable it in my workbook.
Is there a way I can assign a keyboard shortcut to the Add-in? There's only one sub in that add-in file now.
I tried writing another sub with
Application.onKey "+^{C}" ,'Calculate'
But it doesn't trigger the sub to be executed.
You don't assign a shortcut to an add-in. Rather, you assign a shortcut to a macro - that is, a Public Sub procedure in your standard module.
So your code file might look like this:
Option Explicit
Public Sub Calculate()
'...code...
End Sub
Open it in Notepad. I'll look like this:
Attribute VB_Name = "Module1"
Option Explicit
Public Sub Calculate()
'...code...
End Sub
Under Public Sub Calculate(), you want to add an attribute so that the file looks like this:
Attribute VB_Name = "Module1"
Option Explicit
Public Sub Calculate()
Attribute Calculate.VB_ProcData.VB_Invoke_Func = "C\n14"
'...code...
End Sub
This is exactly how Excel's macro recorder assigns macro hotkeys: no need for any Application.OnKey work-arounds.
Save the file, import it into your VBA project: Ctrl+Shift+C will now invoke that macro.
If you're using Rubberduck, forget all of the above and just go to your module in the VBA editor, find the procedure and annotate it like so:
Option Explicit
'#ExcelHotkey("C")
Public Sub Calculate()
'...code...
End Sub
Where "C" will make the hotkey Ctrl+Shift+C; I'd warmly recommend not using "c" to avoid hijacking Ctrl+C (Copy).
Bring up the code inspections toolwindow, hit the "refresh" button; under "Rubberduck Opportunities" there should be an inspection result warning about annotations & attributes being out of sync - select "add member attribute" from the "Fix" menu, and you're done - no need to export/edit/import or deal with any obscure syntax, and if you want to change the hotkey, simply edit the comment accordingly and re-synchronize annotations & attributes.
See VB_Attribute annotations for more information.
As for saving your VBA project as an add-in, simply save your VBA project's host workbook as a .xlam add-in file, then close Excel altogether and re-open it - load your .xlam from the Developer Ribbon tab's "Excel Add-ins" button (hit "Browse" to locate your .xlam file if it's not in the list).
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!
Having just upgraded to Excel 2013, I've moved my macros from a legacy custom toolbar to a custom ribbon menu. All works well, except for one thing. I used to have a macro that ran on AutoOpen, but could also be called manually via a button on the toolbar.
I call my macro from the ribbon using Sub myMacro(control As IRibbonControl) which works. But if I Call myMacro(control As IRibbonControl) in AutoOpen I get an "expected list separator" error. Conversely if I just Call myMacro() in AutoOpen I obviously get a "argument not optional" error. Bit of a Catch 22!
I know that I could just move my code to a third sub-routine, called by two separate macros in the ribbon and in AutoOpen, but before I admit defeat and do this I wonder if there is a way around this.
I have searched the web for a solution to this, but couldn't find anything that answered my particular query.
Thanks
Rob
A simple code as this will help?
Option Explicit
Sub AutoOpen()
Dim ctl As IRibbonControl
myMacro ctl
End Sub
Sub myMacro(control As IRibbonControl)
MsgBox "Hello World"
End Sub