Outlook VBA insert line of text - vba

I'm trying to write a piece of code (vba) that inserts a single line in the email i'm composing (open e-mail item). The code below is a first step i took
What works
if I run the code directly from the module (development window), the text is inserted.
What doesn't work
If I add the a macro (vba) in my ribbon and try to run it, nothing happens. The code only seems to work directly form the module (Play button).
What I want
Run macro (vba) from ribbon in active/open item;
Solution = A Module name can't contain a Macro of the same name.
Set font color to e.g. green;
Nice to have: insert text at bottom of page.
Code:
Sub InsertText()
Dim sText As String
sText = "Text to insert"
On Error GoTo ErrHandler
If TypeName(ActiveWindow) = "Inspector" Then
If ActiveInspector.IsWordMail And ActiveInspector.EditorType = olEditorWord Then
ActiveInspector.WordEditor.Application.Selection.TypeText sText
End If
End If
Exit Sub
ErrHandler:
Beep
End Sub

When you are adding it to the ribbon, are you adding it to the mail item ribbon or outlook ribbon? This gets confusing so I'm going to use mostly pictures to describe it.
This is it added to the mail item ribbon and it worked fine: -
If I place the button on the Outlook bar pushing the button does not work: -
This is because as soon as I push the button on the Outlook ribbon, the mail item is no longer the active window.
For confirmation, to add the button to to the mail item window, right click on the ribbon of a mail item and choose 'Customize the Ribbon...'
Press the 'New Tab' button in the lower right, change 'Choose commands from:' to 'Macros' and click on the macro in question from the list below it. Finally, click the 'Add > >' and then 'OK'.

Related

Excel VBA Adding 'paste special as value' icon to right click menu (cell context menu customization)

I wonder how to add new icon to right click menu using VBA code which will be the shortcut to paste special as value Excel functionality (which can be found in Excel 2010, but not in 2007). Adding icon itself isn't the problem, but is there generic method (not VBA macro) which could be associated with that icon ?
Below code which add icon associated with ToggleCaseMacro macro (which isn't defined here) :
Sub AddToCellMenu()
Dim ContextMenu As CommandBar
Dim MySubMenu As CommandBarControl
' Delete the controls first to avoid duplicates.
Call DeleteFromCellMenu
' Set ContextMenu to the Cell context menu.
Set ContextMenu = Application.CommandBars("Cell")
' Add one built-in button(Save = 3) to the Cell context menu.
ContextMenu.Controls.Add Type:=msoControlButton, ID:=3, before:=1
' Add one custom button to the Cell context menu.
With ContextMenu.Controls.Add(Type:=msoControlButton, before:=2)
.OnAction = "'" & ThisWorkbook.Name & "'!" & "ToggleCaseMacro"
.FaceId = 59
.Caption = "Toggle Case Upper/Lower/Proper"
.Tag = "My_Cell_Control_Tag"
End With
End Sub
MSDN page with above code
You just need to add:
ContextMenu.Controls.Add Type:=msoControlButton, ID:=370, Before:=2

How to make a reusable button from a macro?

Having a working macro, I'd like to reuse it. Namely, to create a button on a toolbar (with a name + icon) that will launch a macro. Tried some tutorials (example: http://www.officetooltips.com/excel/tips/create_a_toolbar_button_or_menu_item_to_run_a_macro.html), but I'd also like to pack the creation code in some file, so that when clicked, the macro would be installed.
Is there an easy way to do it, and if yes, than how? (the best option would work for both Windows and Mac)
Update 4/20: great comment about the complexity of building an add-in. That being said, I'm sure the old timers here would say that something worth doing is worth doing right :). Here is a short walkthrough for creating an add-in:
(1) Save an xlsm or xlsb file with a name that's easy to increment for versions of your add-in.
(2) Add the following scripts into ThisWorkbook to ensure that you create a menu bar when the workbook is opened and when the workbook is activated:
Private Sub Workbook_Open()
Call CreateMenuBar
End Sub
Private Sub Workbook_Activate()
Call CreateMenuBar
End Sub
(3) Create a new module and add the following code to create, delete and update your menu bar:
Option Explicit
Sub CreateMenuBar()
Dim MenuObject As CommandBarPopup
Dim MenuItem As Object
Dim SubMenuItem As Object
'clear the old menu bar
Call DeleteMenuBar("&MyMenuBar")
'create the menu bar and drop down options
Set MenuObject = Application.CommandBars(1).Controls.Add(Type:=msoControlPopup, _
before:=10, Temporary:=True)
MenuObject.Caption = "&MyMenuBar"
MenuObject.OnAction = "UpdateMenuBar"
'first level menu option
Set MenuItem = MenuObject.Controls.Add(Type:=msoControlPopup)
MenuItem.Caption = "&First Menu Stuff"
'link to first script
Set SubMenuItem = MenuItem.Controls.Add(Type:=msoControlButton)
SubMenuItem.Caption = "&First Script"
SubMenuItem.OnAction = "Script1"
'link to second script
Set SubMenuItem = MenuItem.Controls.Add(Type:=msoControlButton)
SubMenuItem.Caption = "&Second Script"
SubMenuItem.OnAction = "Script2"
'first level menu option
Set MenuItem = MenuObject.Controls.Add(Type:=msoControlPopup)
MenuItem.Caption = "&Second Menu Stuff"
'link to third script
Set SubMenuItem = MenuItem.Controls.Add(Type:=msoControlButton)
SubMenuItem.Caption = "&Third Script"
SubMenuItem.OnAction = "Script3"
End Sub
Sub DeleteMenuBar(MenuName As String)
On Error Resume Next
Application.CommandBars(1).Controls(MenuName).Delete
On Error GoTo 0
End Sub
Sub UpdateMenuBar()
'do special checks, like verifying sheets, in this routine
End Sub
(4) Verify your scripts work and save the file.
(5) Save the file again as an xlam or xla file and distribute that to users. Boom!
--Original post below--
Here's what an add-in looks like on a Windows Excel instance:
And here's what an add-in looks like on a Mac Excel instance:
An add-in can be very handy if you develop many scripts for a fleet of users and want to ensure they're all using the same code.
I posted this on another question, but it wasn't what they were looking for. Would this work for you?
In the options for Excel, click on Customize Ribbon. Above the list of things you can add there should be a dropdown box where you can select Macros. The list should then be populated with macros to add to your ribbon!

Calling a Macro in Word Com Addin on msocontrolbutton.onaction

I am developing a com Add In For Word 2007.
I am trying to create a popup menu and each control button in that is calling a macro present inside the same ThisAddin Class.
But in word when i click on the control button in popup menu i get the following error :
"The macro cannot be found or has been disabled because of your macro
security settings"
I have tried a lot of search but unable to get any related info. so i think it might not be a issue at all or not a feature at all.
The code for menu generation is as below :
cbrCmdBar = MyWordAddIn.Globals.ThisAddIn.Application.CommandBars.Add(Name:="MyPopup", Position:=Office.MsoBarPosition.msoBarPopup, MenuBar:=False, Temporary:=True)
With cbrCmdBar.Controls
With .Add(Type:=Office.MsoControlType.msoControlButton)
.Caption = "Point"
.OnAction = "StopPoint"
.FaceId = 70
End With
End With
And the macro is :
Public Sub StopPoint()
MsgBox("Popup Control Button Selected")
End Sub
On the other hand , if i put the macro in template of the ActiveDocument It works fine.
Please help.
Below FaceID, add:
.OnAction = "=StopPoint()"

Automatically add a single menu item of an add-in in Excel

I am using Microsoft Excel 2010 for Windows.
I have already developed an add-in addin.xlam, which contains a sub main. addin.xlam is at the right place so that it is visible and selectable via the menu Developer -> Add-Ins. When I open a normal workbook test.xlsm, and press Alt + F11, I can see the code of addin.xlam is loaded.
My aim is to add a single menu item to the menu bar of Excel, to allow users to launch main of add-in.xlam. By following this link, my code in addin.xlam is as follows:
Option Explicit
Dim cControl As CommandBarButtonPrivate
Sub Workbook_AddinInstall()
On Error Resume Next 'Just in case
'Delete any existing menu item that may have been left.
Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete
'Add the new menu item and Set a CommandBarButton Variable to it
Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add
'Work with the Variable
With cControl
.Caption = "Super Code"
.Style = msoButtonCaption
.OnAction = "main" 'Macro stored in a Standard Module
End With
On Error GoTo 0
End Sub
Private Sub Workbook_AddinUninstall()
On Error Resume Next 'In case it has already gone.
Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete
On Error GoTo 0
End Sub
This code is well placed in ThisWorkbook of addin.xlam, it is also visible in test.xlsm. But I can't see any change in the menu bar.
Does anyone know what happens?
The AddinInstall and AddinUninstall events are only fired when the the addin is "installed" or "uninstalled" using the Excel Addin Manager.
IMHO this can lead to problems, so I always recommend using the Workbook_Open and Workbook_BeforeClose events instead.
Charles is right, you need to replace Workbook_AddinInstall() with Workbook_Open(), and replace Workbook_AddinUninstall() with Workbook_BeforeClose().
furthermore, you need CommandBarButton not CommandBarButtonPrivate.
good luck!

How do I use vba to make a new button in Powerpoint and how do I make a progress bar after I click on it?

I have some code that I execute when a slide show presentation begins but because it takes some time, I want to create a button in a Powerpoint toolbar to execute this code.
So, I click on the button and after that I want to happear a progress bar!
How can I use vba to create a new button in a Powerpoint toolbar and how can I make a progress bar when I click on it?
Oh, at the end of the progress bar I want to execute the slide show.
How can I do that as well?
Thanks in advance!
1.
How can I use vba to create a new
button in a Powerpoint toolbar
Try this code:
Sub AddButton()
Dim cb As CommandBar
Set cb = Application.CommandBars.Add("additional_toolbar", msoBarTop, , True)
With cb.Controls.Add(msoControlButton)
.Caption = "click me"
.OnAction = "macro_name"
.Style = msoButtonCaption
End With
cb.Visible = True
End Sub
2.
how can I make a progress bar when I
click on it?
I would suggest creating UserForm with ProgressBar control on it.
3.
try this to begin slide show [source]:
Sub BeginSlideShow()
ActivePresentation.SlideShowSettings.Run
End Sub
Additionally:
there is no equivalent of PERSONAL (from Excel) in PowerPoint , only place for storing code is presentation itself, so you need open presentation before executing any code.