Accessing Add-In from e-mail body context menu - vb.net

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

Related

Cannot get the Custom UI context menus working in Word 2016

I have written several custom UI Ribbon menus for both Excel and Word. I have working custom context (right click) menus with my Excel apps, but cannot seem to get them to work in Word. I used the same coding as with my working menus in Excel and wrote the XML using the "Custom UI Editor for Microsoft Office" so there should be no issue.
I will post my code. The real question is will this type of custom menu work in Word at all?
The XML in the ribbon code is (showing the last lines only):
</tabs>
</ribbon>
<contextMenus>
<contextMenu idMso="ContextMenuCell">
<dynamicMenu id="mnuMain" label= "BidSmart Contract" imageMso="DocumentTemplate" getContent="GetContent" insertBeforeMso="Cut"/>
</contextMenu>
</contextMenus>
The getContent sub, "GetContent" looks like this:
Sub GetContent(control As IRibbonControl, ByRef returnedVal)
Dim xml As String
xml = "<menu xmlns=""http://schemas.microsoft.com/office/2009/07/customui"">" & _
"<button id=""mnuCtxtEmail"" label=""Send and Email"" imageMso=""TableSharePointListsRefreshList"" screentip=""Send an Email to the Client, Designer or Contractor..."" onAction=""mnuEmail""/>" & _
"<button id=""mnuCtxtEnvelope"" label=""Print an Envelope"" imageMso=""WhatIfAnalysisMenu"" screentip=""Create an Envelope addressed to the Client, Designer or Contractor..."" onAction=""mnuEnvelope""/>" & _
"</menu>"
returnedVal = xml
End Sub
If you are using the same ribbon XML you won't get your XL context menu to work in WD. You need to find the correct idMso for the context menu in WD that you want to add your menu to. You can get the control ids here: Office 2016 control ids

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

customize shorcut (right-click) menu powerpoint

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

How to stop a Word Document immediately when newly created

I have a VS2010 Vb.net program that creates a Word 2007 file.
My Normal.dot file is customised to give me a new Tab with Buttons in that do specific things via VBA in the Normal.dot program when those Buttons are pressed.
This all works fine, however, I now want to add some functionality whereas as soon as the new Word document is created, it edits a Task in Outlook.
I have edited the 2 "This Document" Procedures and you can see my Normal.Dot file in the attached Screenshot.
When I run my VB.Net program to create a brand new Word 2007 document, the program does NOT stop on either of the message boxes, it just continues and opens the Word document as before, my code is below, what am I doing wrong ?!?
'Open or Create Word document for Editing
myNewsLetter = myFolder + myLeague + "News" + mySession + ".doc"
If File.Exists(myNewsLetter) Then
'do nothing
Else
myTemplate = myTempFolder + "NL Skeleton.doc"
File.Copy(myTemplate, myNewsLetter)
Create_Blank_Newsletter()
End If
'Open Word Newsletter, or switch to it if it's already open
Dim myFileOpen As Boolean
myFileOpen = IsFileOpen(myNewsLetter)
If myFileOpen = False Then
MSDoc = MSWord.Documents.Open(myNewsLetter)
End If
MSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
MSWord.Visible = True
MSWord.ActiveDocument.Bookmarks("\StartOfDoc").Select()
OK, sorted this, the full discussion can be found here ... http://www.vbaexpress.com/forum/showthread.php?p=286771#post286771
Basically, I'm not creating a NEW document, I am creating a new document via a Copy and then opening that existing document !!!

Adding controls to Excel workbook, via VSTO, resets everything in VBA

I have a VSTO Excel Addin solution which exposes certain methods to VBA through a ComVisible interface. Please refer this for a sample implementation. I am using VSTO 4 and Excel 2007/2010 for this.
Certain methods of the exposed interface insert a control (button, combo box or a Winform UserControl) when called from VBA. The method executes properly and inserts the control on the sheet as well.
public void AddButtonControl(string strControlName)
{
Microsoft.Office.Interop.Excel.Worksheet nativeWorksheet = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
Microsoft.Office.Tools.Excel.Worksheet vstoWorksheet = Globals.Factory.GetVstoObject(nativeWorksheet);
vstoWorksheet.Controls.AddButton(nativeWorksheet.Range["A1", "E5"], strControlName);
}
Now as soon as the control is inserted in the workbook, all the variables declared in VBA are reset. All code goes back to the state as if it were never executed.
My solution and an Excel workbook are shared here
Just build the solution and open the workbook SampleTest.xlsm
Click "Check string value", a message box saying "Default Text" would be displayed.
Click on button - "Add Button Dynamically". A button would be inserted at the top of the sheet. and a message saying - "Assigning text to string: Sample Text" would be displayed.
Now click "Check string value" again. It should have displayed "Sample Text" but a blank message box is displayed.
Any suggestions/workarounds are welcome.