customize shorcut (right-click) menu powerpoint - vba

I want to make appear a new element on the shortcut menu, known also as right-click menu. I want this element to execute a macro that I made. I check on the web but I could find a solution that really work. I put here which one I tried:
Public Sub customizeRightClick()
Dim pic As IPictureDisp
Set pic = LoadPicture("C:\path\pic.jpg")
For Each oCmdBar In Application.CommandBars
If oCmdBar.Type = msoBarTypePopup Then
If oCmdBar.Name = "Shapes" Then
Set cmdButton = oCmdBar.Controls.Add(Type:=msoControlButton)
With cmdButton
.Caption = "Edit Element"
.Tag = "Edit"
.Picture = pic 'Object of type IPictureDisp
.OnAction = "editMag"
End With
End If
End If
Next
End Sub
I check in the Watches and it add the control but when I use right-click on a shape the options doesn't appear. Maybe I am not putting where it have to be but I cannot find anywhere an explanation about where is the correct place to set the new element.

David's correct re 2007, but it seems they've added the ability to customize context menus back in PPT 2010:
http://social.msdn.microsoft.com/forums/office/en-US/c1eb22ba-6ca8-4c21-8100-62185355aa53/customize-rightclick-context-menu-in-powerpoint-2010

OK I have confirmed that there is:
Limited support for shortcut menu customizations in PPT 2007+
specifically. ....You cannot add any item to the shapes shortcut menu
(unless it is an activex control) in PPT 2007+.
http://answers.microsoft.com/en-us/office/forum/office_2007-customize/customizing-right-click-menu/76aff9b3-9253-40cf-bd21-e1f832144ad8
You may be able to do this with Ribbon/XML interface, but again it may be subject to the annoying limitation that they put on certain context menus. It might simply not be possible to do what you want to do in this version of PowerPoint.
http://msdn.microsoft.com/en-us/library/gg469862.aspx#odc_xl_ta_CustomExcelContextMenus_AddDynamicMenu

Related

Display Hyperlink Hand Cursor for Check Box on MS Access Form

How do I make check boxes in an ms access report have a hyperlink hand on hover?
If cc.ControlType = acCheckBox Then
'cc.DisplayAsHyperlink = acDisplayAsHyperlinkAlways
' cc.IsHyperlink = True
'cc.CursorOnHover = acCursorOnHoverHyperlinkHand
End If
But I get this error:
CursorOnHover is a property for CommandButton objects. A simple work-around would be to place a transparent button behind your check boxes and set the CursorOnHover property to acHyperlink hand from the Property Sheet.

CommandBars("Text").Controls not deleted when exiting the document - VBA word add-in

I'm trying to create a add in for MS Word with VBA.
It has a "AutoExec" procedure that creates a new item in the CommandBar("Text") collection (right click menu) and a "AutoExit" that deletes this created item.
As an example, I tried the code below that create an item "How Many Pages?", which executes a macro displaying the number of pages in the active document.
This is the AutoExec Code:
Public Sub AutoExec()
Dim objcommandbutton As CommandBarButton
Call MsgBox("AutoExec")
Set objcommandbutton = Application.CommandBars("Text").Controls.Add _
(Type:=msoControlButton, Before:=1)
objcommandbutton.Caption = "How Many Pages?"
objcommandbutton.OnAction = "HowManyPages"
End Sub
This is the AutoExit Code:
Public Sub AutoExit()
Dim objcommandbutton As CommandBarControl
Call MsgBox("AutoExit")
For Each objcommandbutton In Application.CommandBars("Text").Controls
If objcommandbutton.Caption = "How Many Pages?" Then
objcommandbutton.Delete
End If
Next objcommandbutton
End Sub
This is the Main Macro Code:
Public Sub HowManyPages()
If Documents.Count > 0 Then
Call MsgBox(ActiveDocument.BuiltInDocumentProperties("Number of Pages"))
Else
Call MsgBox("No document is currently active.")
End If
End Sub
When exiting the document, the Button previously added in CommandBars("Text") collection is not deleted. I see this when I open a new blank Word document and the button remains in the right click menu.
I know that the routine is performed correctly because there is a MsgBox instruction to verify it.
This only happen with the AutoExit subroutine of a add-in, that is, loaded as a add-in: running the code in a macro with vba module works fine.
Any help?
When working with the CommandBars object model in Word it's necessary to always specify the Application.CustomizationContext.
Word can save keyboard layouts and CommandBar customizations in various places: the Normal.dotm template, the current template or the current document. The default when you create a CommandBar addition may not be the3 same as the default when trying to delete something.
Since this is an add-in, I assume you want the change for the entire Word environment (any open document). In that case, use the context NormalTemplate. Use this before any calls to CommandBar:
Application.CustomizationContext = NormalTemplate
Note: for saving a customization in the current document: = ActiveDocument; for saving in the template attached to the current document: = ActiveDocument.AttachedTemplate.
I solved my problem with a workaround:
I was trying to "add" a template (.dotm) as a add-in (in the "Templates and Add-ins" window) to use my VBA project in a new document. That's why I was using the AutoExec() and AutoExit() procedures.
But only now I figure out that just "attaching" the .dotm template to the active document (in the same "Templates and Add-ins" window, as show in the figure below) makes the functions Private Sub Document_Open() and Private Sub Document_Close() to run normally. Which solves my problem.
Even so, I think there is a certain "issue" with the AutoExit() procedure when trying to change the CommandBars itens. But that's ok for now.
enter image description here

Programatically adding and modifying command buttons (active X control) - Excel VBA

Hello this have been giving me a headache for a while.
I want to simply be able to remove existing buttons and add new ones. I already have ready made functions xbutton_click() and ybutton_click(). I tried adding buttons like this:
Set btn= ActiveSheet.Buttons.Add(Range("B3").Left, Range("B3").Top, Range("B3").Width, Range("B3").Height)
With openForm
.OnAction = "new_Click"
.Caption = "new"
.Name = "newButton"
End With
but those seem to only work with macros which is not what I want. I realized that when I manually add a button, I add the active X control button. I want that. It has a name property, which can be changed, and its function is basically name_click(), perfect!
Now when it comes to adding them its a headache. I have surfed the internet but it is full of outdated solutions (I am using 2013). I could only add new command buttons, but couldn't change their caption, name, nothing.
this is the code for adding a "command button" (not "button"):
Set objBtn = ws.OLEObjects.Add(ClassType:="Forms.CommandButton.1", link:=False, _
displayasicon:=False, Left:=celLeft, Top:=celTop, Width:=celWidth, Height:=celHeight)
Adding the ActiveX Command Button works, but I cannot change its properties like "Caption". Can someone please guide me through this maze?
Edit
objBtn.Name = "newName" works. But objBtn.Caption = "newCaption" gives an error 438.
For OLEObjects, you can't do it just a caption this should solve it
Dim objBtn As OLEObject
Set objBtn = ws.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, _
DisplayAsIcon:=False)
objBtn.Object.Caption = "Example"
Notice you were missing .Object.

Word VBA: Sharing variables between documents

I feel like this should be obvious, I'm just not finding the answer anywhere.
I have a Word document with several Public variables declared and defined in a variety of procedures. One procedure opens another Word document, which has a form that loads on open. For the life of me I cannot get that form in the second document to use values from the original document's variables. Am I misunderstanding the nature of Public variables? Everything I've found seems to indicate that those values should be visible to the newly opened document.
So to open the second document, I'm using the code below (some of it is declared and set elsewhere, but I think this is the relevant stuff:
Public iMarker as boolean
Sub OpenDoc()
If check_ExA.Value = True Then 'a checkbox on a userform
docName = docsPath & "/" & "seconddocumentpath.docm"
iMarker = True
formAddDocs.Hide
Set addDoc = Documents.Open(docName)
End Sub
from there, the new document has a form that shows on Open, and in the initialization, I want it to be able to see if the iMarker variable is true, among some other strings, etc.
Private Sub UserForm_Initialize()
If iMarker = True Then
'do some other stuff
End If
End Sub
I tried changing that Initialize sub to public and that did nothing. It won't see that iMarker is true when the second document opens. Any suggestions?
To make a public variable of doc01 ("source doc") available to doc02 ("target doc") document, go like follows:
open the "source doc"
open the "target doc"
click Tools-> References
from Available References listbox of the References dialog box click the "Project" of the "source doc"
You can recognize it from the path appearing near the dialog box bottom
Otherwise
close Reference dialog box
go to "source doc" and rename its project (Tools->project properties, edit "project name" textbox and click "OK") with a meaningful name (say "MainProject")
go back to "target doc" project
open References dialog box and now from Available References listbox click the "MainProject" reference
Save and close target doc

Accessing Add-In from e-mail body context menu

Using Visual Studio 2015 to build an addin for Outlook 2013. I have already built this addin for Excel 2013. The addin is to be accessed from the context menu in the body of an e-mail.
The following snippet is typically how I have added the button to the Excel context menu but can't seem to find how to do this for an outlook e-mail:
Dim contextmenu As Office.CommandBar
Dim DDHButton As Office.CommandBarButton
contextmenu = Application.CommandBars("cell")
DDHButton = contextmenu.Controls.Add(Type:=Office.MsoControlType.msoControlButton, Before:=20)
With DDHButton
.FaceId = 2308
.Caption = "Button Name"
.Tag = "New Button"
.TooltipText = "etc. etc."
End With
I have tried to alter this to the following:
contextmenu = Application.ActiveExplorer.CommandBars("ContextMenuMailItem")
And many other variations of this line to no avail.
I have also come across suggestions of creating a ribbon and editing the xml file but have also had absolutely no luck with this either!
Any suggestions?
Your context menu must come from the ribbon XML returned by your addin. See https://msdn.microsoft.com/en-us/library/office/ee692172(v=office.14).aspx