VSTO customise outlook folder context menu - c#

I am basically looking to do this:
C# (outlook add-in) context menu on folders
But the problem is the it never fires the FolderContextMenuDisplay event.
Looking at the documentation it seems to be deprecated, but does anyone know what I should use instead?
Thanks!

By using Ribbon XML you can set the visibility of context menu.
In this post, I will show you how to show and hide items in a context menu by using Ribbon XML.
The context menu appears when a user right-clicks an Outlook folder.
First, add a Ribbon XML item to your Outlook project.
Then add the following code to your ThisAddIn class. This example assumes that your Ribbon is called Ribbon1 and that you are using C#:
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new Ribbon1();
}
Next, replace the contents of the Ribbon1.xml file with the following:
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
<contextMenus>
<contextMenu idMso="ContextMenuFolder">
<button idMso="FolderPropertiesContext" getVisible="IsVisible" />
</contextMenu>
</contextMenus>
</customUI>
But wait. There’s more!
In Ribbon1.cs file create this method.
public bool IsVisible(Office.IRibbonControl control)
{
string name = ((Outlook.Folder)control.Context).Name;
if (foldername == "Inbox")
{
return false;
}
return true;
}
When user right-clicks the folder, then we tell Outlook to not show the command by returning false. Otherwise, we tell Outlook to go ahead and make the command visible by returning true.

Related

VSTO Ribbon Button Opens Windows Form But Unable to Use another Instance of Excel without closing the form C#

I have a VSTO application that has multiple groups and buttons on the ribbon. When a user clicks on a button a specific form is loaded on Excel. It looks like when the form is open a user is unable to open another instance of Excel unless the form is closed in the first instance. Is there a way to seperate the Add-In from different instances?
I have code developed which uses getVisbility call back to decide whether to show the tab in the ribbon based on a specific workbook. However this doesnt allow users to use multiple Excel instances whilst a windows form is open. As soon as I close the form - a new instance of excel has been opened. The VSTO Excel tool has been developed on application level.
MainRibbon.xml
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load">
<ribbon>
<tabs>
<tab idMso="TabAddIns" label="MDS" insertBeforeMso="TabHome" getVisible="setVisbility" >
<group id="CreateLoadModel"
label="Create/Load Model">
<button id="createmodelbutton" label="Create New Model"
screentip="Text" onAction="OnCreateModel"
supertip="Create a new Model"
imageMso="GroupSmartArtCreateGraphic"/>
<button id="loadmodelbutton" label="Load Existing Model"
screentip="Text" onAction="OnLoadModel"
supertip="Load an Exisitng Model"
imageMso="FileOpen"/>
</group>
MainRibbon.cs
public bool setVisbility(Office.IRibbonControl control)
{
int nWorkbooks = Globals.ThisAddIn.Application.Workbooks.Count;
if (nWorkbooks == 0)
{
return false;
}
if (Globals.ThisAddIn.Application.ActiveWorkbook != null && Globals.ThisAddIn.Application.ActiveWorkbook.Name == "MDS.xlsm")
{
return true;
}
else
{
return false;
}
}
AddIn.cs
private void ThisAddIn_Startup(object sender, EventArgs e)
{
this.Application.WorkbookActivate += Application_WorkbookActivate;
this.Application.WorkbookOpen += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookOpenEventHandler(Application_WorkbookOpen);
}
private void Application_WorkbookActivate(Workbook Wb)
{
MainRibbon.ribbon.Invalidate();
}
If you open a modal dialog box in an VSTO button event handler, the Excel main thread will be blocked and Excel will not response to user input messages.
This includes also trying to open another Workbook. This behavior is similar to opening "Format Cells" dialog for example.
Solutions:
1) A quick workaround that does not require programming is clicking ALT button when trying to open a new workbook. Excel will show the message
asking you to open a new instance of Excel.
2) Another approach is opening a modalless dialog in c# (using Show function instead of ShowDialog). This is similar to Find/Replace window of Excel.
Please look at the following example for more details and how to get a result from such dialog.
https://www.codeproject.com/Articles/27010/Modal-and-Modeless-Dialog-Box-in-C

How to have event or run method after the document was Closed?

I have an Word AddIn (VSTO) that will process the word document after it will be closed by the user.
Unfortunately, the DocumentBeforeClose event is raised even in cases where the document is not really going to close.
Ex: The event is raised before a dialog box is shown to the user prompting the user to save the document. Users are asked whether they want to save with a Yes, No, and Cancel button. If the user selects Cancel, the document remains open even though a DocumentBeforeClose event was raised.
For that reason is there is any way or method to make event or Method that will be raised or run after the document was closed .
I tried to do like this:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Globals.ThisAddIn.Application.DocumentBeforeClose += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(this.Application_DocumentBeforeClose);
// I want some thing like this
Globals.ThisAddIn.Application.DocumentAfterClose += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(this.Application_DocumentAfterClose);
}
public void Application_DocumentBeforeClose(Word.Document doc, ref bool Cancel)
{
MessageBox.Show(doc.Path, "Path");
}
// I want some thing like this
public void Application_DocumentAfterClose(string doc_Path)
{
MessageBox.Show(doc_Path, "Path");
}
As you already said, you can't be certain with the DocumentBeforeClose event handler that the document is actually closed afterwards. However, you can gain full control on the close process by overriding the File Close command:
Add commands to your Ribbon XML (for idMso FileClose):
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"
onLoad="OnLoad">
<commands>
<command idMso="FileClose" onAction="MyClose" />
</commands>
<ribbon startFromScratch="false">
<tabs>
<!-- remaining custom UI goes here -->
</tabs>
</ribbon>
</customUI>
Providing the respective callback methods in your code:
public void MyClose(IRibbonControl control, bool cancelDefault)
{
var doc = Application.ActiveDocument;
doc.Close(WdSaveOptions.wdPromptToSaveChanges);
// check whether the document is still open
var isStillOpen = Application.IsObjectValid[doc];
}
As full sample how to customize Word commands can be found on MSDN:
Temporarily Repurpose Commands on the Office Fluent Ribbon

VSTO Excel preserve Ribbon state

I have simple VSTO Excel 2013 Application level Add-in with custom Ribbon, which includes toggle button and checkbox. If I open two files (workbooks) I can see that the Ribbons do not preserve their state across multiple windows, meaning if I click on checkbox or toggle button on Second workbook the same checkbox state is shown on a first workbook and vise versa. I found an article which describes a similar situation for outlook : https://www.add-in-express.com/creating-addins-blog/2013/01/30/preserve-outlook-ribbon-controls-state/ but unfortunately the Inspector window event is not available in Excel. Any idea on how to deal with it?
You need to use callbacks instead of attributes in the Ribbon XML. Also when a user changes the active window you need to call the Invalidate/InvalidateControl method of the IRibbonUI interface to force Office applications (Excel in your case) call your callbacks for the current state of controls. It's pretty easy...
You can read more about the Ribbon UI (aka Fluent UI) in the following series of articles in MSDN:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)
Also you may find the following ones helpful:
Chapter 11: Creating Dynamic Ribbon Customizations (1 of 2)
Chapter 11: Creating Dynamic Ribbon Customizations (2 of 2)
I tried a sample with a toggle button with toggle on and then switched to another workbook the toggle button doesnt persist. But then if you stored the pressed value in a variable and return that on getPressed callback of a toggle button it worked.
Ribbon.xml
<?xml version="1.0" encoding="UTF-8"?>
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon>
<tabs>
<tab idMso="TabAddIns">
<group id="group1" label="group1">
<toggleButton id="toggleButton1" label="toggleButton1" size="large" getPressed="buttonPressed" onAction="buttonAction"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Ribbon.cs
private bool isButtonPressed = false;
public void buttonAction(Office.IRibbonControl control, bool isPressed)
{
isButtonPressed = isPressed;
}
public bool buttonPressed(Office.IRibbonControl control)
{
return isButtonPressed;
}

This addin caused outlook to start slowly

I am developing an Outlook Addin Project using C#.NET 4.5
But After I deploy, Sometimes outlook disables my addin, and shows this message.
"This addin caused outlook to start slowly"" I dont know whats wrong with my addin.
This has only a few codes, and ThisAddIn_Startup is empty in this project.
Here is the code...
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new Ribbon1();
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" önLoad="Ribbon_Load">
<contextMenus>
<contextMenu idMso="ContextMenuContactItem">
<menuSeparator id="mniMailItemMenuSeparator" />
<button id="customButton"
label="Call using Software" insertBeforeQ="Copy"
imageMso="Call"
önAction="OnMyButtonClick" />
</contextMenu>
<contextMenu idMso="ContextMenuMailItem">
<menuSeparator id="mailmenusep1" />
<button id="mailbutton" insertBeforeQ="Copy"
label="Call using Software"
imageMso="Call"
önAction="MailItemCallNumbers" />
</contextMenu>
</contextMenus>
</customUI>
Starting with Outlook 2013, Microsoft introduced new performance criteria for add-ins. For more information see http://msdn.microsoft.com/en-us/library/office/jj228679.aspx#ol15WhatsNew_AddinDisabling
In your case, Outlook is loading the .Net framework and is considering the cost as part of your add-in. Native COM add-ins tend to load faster than .Net add-ins.
As the article points out (in the section "System Administrator control over add-ins", there is a Group Policy setting that allows you to specify which add-ins are always enabled, always disabled (blocked), or configurable by the user.
Though not recommended, as Outlook does when "Always enable this Add-In" is selected (as described here), you could directly add your add-in to the following registry key.
HKCU\Software\Microsoft\Office\15.0\Outlook\Resiliency\DoNotDisableAddinList
For improving the performance of VTSO based add-in, see Performance Improvements Coming Soon to a Service Pack Near You
For changing when an add-in is loaded, see Delay-loading the CLR in Office Add-ins.
This can happen on an empty project if you build your add-in in debug mode and attach the debugger to it. The added time required to load all the symbols files into the debugger when it get attached might take longer than Outlook is expecting for a "stable" plugin to initialize.
You shouldn't worry about this unless it happens in a release build with no debugger attached. During development, you can simply tell outlook to never disable your plugin.
Try delay loading of your addins. Try setting Load Behaviour to 8 or 9 for On-Demand loading.
Check this for more
http://blogs.msdn.com/b/andreww/archive/2008/04/19/delay-loading-the-clr-in-office-add-ins.aspx
I know this is an older post, but I found something online that helped me out.
Your post says "This addin caused outlook to start slowly I dont know whats wrong with my addin." I found if you add a particular Environment Variable that more robust messages will appear.
From the Start menu, right click Computer -> Properties -> Advanced System Settings -> click the Environment Variables button. Click New button under the bottom pane called System Variables and add the following. Enter 'VSTO_SUPPRESSDISPLAYALERTS' with a variable value of 0 (Zero). Should look like this.
Now re-open the application that your add-in was attached too, for me it was Outlook. When the add-in starts the error message should now appear with a Details button, click and read through the Exception. For me it looked like this.

How to intercept clicking of a built-in Office Ribbon control

I'm wondering if it's possible to detect when a user has clicked the Header/Footer button in Excel so I can show some custom header/footer related ribbon controls on my add-in's tab and hide them when the user is not in header/footer edit mode.
Is it possible to hijack this button click somehow? I've seen applications hijack the Excel Save before. I'm looking for similar behavior just with the header/footer button.
I'm using C#, Visual Studio 2012 and Excel 2010. I've created my custom ribbon using the Ribbon XML approach.
One way I've discovered that worked was to use the <commands> section in the Ribbon XML (which I didn't know existed.) Apparently this mechanism allows you to re-purpose actions intrinsic to Excel but beware that not all controls support re-purposing the onAction callback)
<?xml version="1.0" encoding="UTF-8"?>
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<commands>
<command idMso="HeaderFooterInsert" onAction="testHeaderFooter"/>
</commands>
<ribbon>
<tabs>...
along with the associated event handler:
public void testHeaderFooter(Office.IRibbonControl control, bool cancelDefault)
{
MessageBox.Show("Testing.");
cancelDefault = false;
}
This link was very helpful:
http://social.msdn.microsoft.com/Forums/office/en-US/e1a60d16-053e-4697-b17c-b22d602f0400/intercept-the-onaction-event-of-a-gallery-element-of-excel-2007-ui-ribbon?forum=vsto

Categories