Handle File->New in Word 2007 - ms-word

I am writing a VSTO addin for Word 2007. When the user selects File->New, (or selects it from the quick access toolbar) I need to display a custom form instead of the standard new document dialog. How do I do this? I don't see an application event I can handle and I can't seem to find the buttont to add an event handler too.

Ok, just found it. You need to create a Ribbon xml and then add commands for those buttons. In this case the ribbon xml is
<commands>
<command idMso="FileNew" onAction="FileNewOverride"/>
<command idMso="FileNewDefault" onAction="FileNewOverride"/>
</commands>
and the code behind is
public void FileNewOverride(Office.IRibbonControl control, ref bool cancelDefault)
{
//do something
}
This how-to on MSDN shows you how to do it http://msdn.microsoft.com/en-us/office/dd361753.aspx

Related

How can I subscribe to an Open event of a Word Backstage using a Ribbon (Visual Designer) object?

I want to add a button to the Word Backstage. That button is supposed to be enabled/disabled only when one or more Word documents are open in Word.
Is there some event I can use to set the state of my Backstage controls the moment the Backstage is displayed?
NB: As a workaround, I could subscribe to the DocumentOpen event, but not to a DocumentClose event, as it doesn't exist. So that solution isn't safe to use.
The backstage UI provide the onShow callback which you may find helpful.
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
<backstage onShow="Backstage_OnShow">
<tab idMso="TabSave">
...
Also you may find the onHide callback helpful:
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<backstage onHide="onHide" onShow="onShow">
...
</backstage>
</customUI>
The callback should have the following signature:
public void Backstage_OnShow(object contextObject) {
}
You can use the IRibbonUI.Invalidate method to get your callbacks invoked. For example, if an add-in writer implements the getEnabled callback procedure for a button, the function is called once, the state loads, and then if the state needs to be updated, the cached state is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method.
Read more about that in the Dynamically Changing the Visibility of Groups and Controls in the Office 2010 Backstage View article.

word object model - active document event when clicking on FILE

I'm developing an MS Word add-in. In newer MS Word editions, there is the "FILE" option in the menu bar which opens an interface where you can select a recent document to open, open a new one, or an existing one. I am trying to find a way, through which I can know WHEN the user "leaves" the current document he is editing clicking on the FILE menu of Word. I cannot seem to find such an event. Is there a way to achieve this ?
The WindowDeactivate does not fulfill this purpose.
The reason I want to do this, is because for a custom spellchecker I'm writing, I'm highlighting the wrong words in an transparent (click through as well) form. So when the user in a recent version of Word clicks the FILE menu, the highlights are still there, as seen in the screenshot
TL:DR; is there a way to detect in MS Word when the user clicks the FILE option in the menu and the current document is not visible? I'm using add-in-express, so all the relevant word object model API is available.
I wonder how can I solve this, any help is appreciated.
edit: screenshot
Yes, you can detect and then execute code both when the File menu is clicked (displaying the Backstage View) and when the View's return arrow is clicked to remove the Backstage View and display the document. To do this use the onShow and onHide attributes with callbacks via a custom XML ribbon in your VSTO project (this will not work with a ribbon made with the Visual Designer).
Reference material can be found here:
Performing Actions When the Backstage View is First Displayed or Hidden
As this article uses VBA to expand on the concepts involved, I built a sample project demonstrating how onShow works using C# and Word 2016 (the documentation was written for Office 2010, but onShow and onHide will work in later versions of Word).
Solution Tree
Custom XML Ribbon (BackstageRibbon.xml)
Note that the <backstage> node, which activates the onShow attribute for the callback, follows the <ribbon> node in the XML.
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui"
onLoad="Ribbon_Load">
<ribbon>
<!--Ribbon XML goes here-->
</ribbon>
<backstage onShow="onShow">
</backstage>
</customUI>
Ribbon Code (BackstageRibbon.cs)
A bunch of this code is boilerplate, however public void onShow is the callback that executes your code based on the onShow attribute in the ribbon's custom XML. Also, public string GetCustomUI is where the C# is told to find the XML.
namespace Backstage_Events
{
[ComVisible(true)]
public class BackstageRibbon : Office.IRibbonExtensibility
{
private Office.IRibbonUI ribbon;
public BackstageRibbon()
{
}
#region IRibbonExtensibility Members
public string GetCustomUI(string ribbonID)
{
return GetResourceText("Backstage_Events.BackstageRibbon.xml");
}
#endregion
#region Ribbon Callbacks
//Create callback methods here. For more information about adding callback methods, visit https://go.microsoft.com/fwlink/?LinkID=271226
public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
this.ribbon = ribbonUI;
}
public void onShow(object contextObject)
{
//Code to be executed before Backstage View displays goes here
MessageBox.Show("Backstage Display Event Triggered!");
}
#endregion
Helpers //Region
}
}
ThisAddin.cs
You will also need to add:
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new BackstageRibbon();
}
after the ThisAddIn_Startup and ThisAddIn_Shutdown private voids in the ThisAddin.cs class to instantiate the custom ribbon.
Word will fire the Application.DocumentOpen event - you can see it live in OfficeSpy (I am its author - click Application button, go to the Events tab, look at the log at the bottom of the window).

Assign Outlook Categories to a RibbonMenu with buttons

I am currently creating a Outlook Add-in and need to include the Outlook Catagorize Menu to my add-in.
With this Foreach I´m Creating and adding the Buttons manually into the "Kategorisieren"-RibbonMenu and naming them the same as the catagory names. Now I´m trying to copy the color-images of the categories to the button Image which works by assigning them with the right OfficeImageId. Nevertheless there are some catagories which colors are not described as OfficeImageIds, but rather like objects f. e. the color peach as an object of OlCategoryColor.OlCategoryColorPeach . The Problem is that I cant use them as the others by assigning them to the button.Image or OfficeImageId. How do I get these little Icons in the right Color next to my buttons.
The next Task would be assigning the Category to the Appointment by clicking on the right button.
Categories categories = Globals.TerminAddIn.Application.Session.Categories;
foreach (Category category in categories)
{
var button = this.Factory.CreateRibbonButton();
this.Kategorisieren.Items.Add(button);
button.Label = category.Name;
}
this.Kategorisieren.ControlSize = RibbonControlSize.RibbonControlSizeLarge;
this.Kategorisieren.OfficeImageId = "CategorizeMenu";
this.Kategorisieren.ShowImage = true;
I'd recommend using the ribbon XML with callbacks where you can define the button's images and labels. At any point in time, you can update the buttons list or change categories with colors. Read more about this approach in the Walkthrough: Create a custom tab by using Ribbon XML article.
When you customize the Ribbon UI by using callback procedures, for each of the callbacks the add-in implements, the responses are cached. For example, if an add-in writer implements the getImage callback procedure for a button, the function is called once, the image loads, and then if the image needs to be updated, the cached image is used instead of recalling the procedure. This process remains in-place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached.
The Fluent UI (aka Ribbon UI) is described in-depth in the following series of articles:
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)

I have a VSTO application as an add-in to MS Word and I want to set keyboard shortcuts to the ribbon buttons

When I developed this app (in C# Visual Studio 2008) I asked the same question (actually managed to find an answer on the MS forum, for which I deserve a prize of some sort). The answer from MS was that the only way to set keyboard shortcuts to your own methods is to write a macro which invokes the method (via COM. obviously) and set the shortcut to invoke that macro.
This is really not the answer I want to hear. VSTO makes it possible to build a really nice application with very good use of the ribbon, etc., but then you have to go to the trouble of exposing the entire thing through COM and build another interface into it via the macros. Which, in addition to being a waste of time, totally circumvents all the security that MS have built into support of VSTO Add-ins.
My question is: Is this really necessary (the whole COM/macro thing), or is there a way that I can assign a keyboard shortcut to my own ribbon items? Word 2007? Word 2010?
Thanks
Its too late to answer but worth sharing.
I have used Keyboard shortcuts in my project. Basically this shortcut key should bring a WPF form called SignOff. This WPF form can be displayed either by clicking a button in from the ribbon tab or by using keyboard shortcut(Ctrl+ Shift +S).
There are four places where I need to write my code.
The action method for the ribbon button called on click event.
public void btnInsertSignoff_Click(IRibbonControl control)
{
InsertSignoff();//This method displays the sign off form
}
The following code binds a key board shortcut with VBA code in the Addin Startup / Document Change event
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Globals.ThisAddIn.Application.KeyBindings.Add(WdKeyCategory.wdKeyCategoryCommand, "InsertSignOff ", Globals.ThisAddIn.Application.BuildKeyCode(WdKey.wdKeyControl, WdKey.wdKeyShift, WdKey. wdKeyS));
}
This link shows how to call VBA Code using Keyboard shortcuts.
http://www.wordbanter.com/showthread.php?t=31813
I followed this example but instead of VBA I did that in VSTO Addin Startup event but the second parameter " InsertSignOff " is a VBA method in step 4.
Wrote another method called InsertSignOff (Following exposing VSTO method to VBA).
[ComVisible(true)]
public interface IAddInAdapter
{
void InsertSignOff ();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddinAdapter : IAddInAdapter
{
public void InsertSignOff()
{
InsertSignoff();//This method displays the sign off form
}
}
This link explains how to expose VSTO code to VBA
http://msdn.microsoft.com/en-us/library/bb608604.aspx
Created a .dotm file in the user templates location “C:\Users\\AppData\Roaming\Microsoft\Templates” which contains the following VBA code
Sub InsertSignOff ()
Dim addIn As COMAddIn
Dim automationObject As Object
Set addIn = Application.COMAddIns("Wordaddin.AddinAdapter")
Set automationObject = addIn.Object
automationObject.InsertSignOff
End Sub
Hope this helps some one.
You can use the global key hook up method mentioned over here: https://learn.microsoft.com/en-us/archive/blogs/vsod/using-shortcut-keys-to-call-a-function-in-an-office-add-in

How do you contribute a command to an editor context menu in Eclipse

I want to contribute a command to the context menu of any text editor when text is selected. In "the old days", I would have done this using objectContribution and a nested action with "enablesFor='+'".
How does one do this using commands instead of actions?
If I revisit my answer "Eclipse RCP: Actions VS Commands", you need a Command handler.
This thread seems to sum up your options:
One is a common pattern, to instantiate the handler in the view itself and have the handler simply look at the view selection and control its own enabled state.
The handler API allows it to fire an event on enabled change, see org.eclipse.core.commands.AbstractHandler.
The other is to create a property tester that can get your view selection.
IWorkbenchPart p = page.findViewReference("your.id").getPart(false);
if (p!=null) {
p.getSite().getSelectionProvider().getSelection() ... whatever
}
Your view would monitor its own selection change events, and call org.eclipse.ui.services.IEvaluationService.requestEvaluation(String) (source here) for that property (which would cause all core expressions using that property tester to be re-evaluated).
The important point is that simply changing views would not cause a re-evaluation (and not change enabled state).
You can set up the property tester to be specific to each view that you need this for, or create one com.example.views.localSelection and use args to specify the view id.
I read more about the variables available in command expressions, and I came close to figuring it out on my own, but failed. I then asked a similar question on the eclipse newsgroup and was led in the right direction. Here's an example of how to do mostly what I was looking for:
<command
commandId="org.marcesher.blogcodeformatter.commands.wikiFormatterCommand"
tooltip="Format And Copy to Clipboard"
id="org.marcesher.blogcodeformatter.popup.wikiFormatterCommand">
<visibleWhen>
<with variable="selection">
<instanceof value="org.eclipse.jface.text.ITextSelection"/>
</with>
</visibleWhen>