C#, Word 2003 addIn and a toolbar button event - c#

I'm trying to write application-level add-in for Word 2003.
The plugin adds a button on a new commandbar - clicking the button saves active document and then performs some additional actions. When I launch Word 2003 and then click my commandbar button everything works fine.
However if I launch Word 2003, open a new Word window by clicking toolbar button "New document" on a "Standard" toolbar and then click my commandbar button it turns out that no action is performed. It seems that my toolbar button on a new opened window has no "onclick" event handler assigned. Do you have any idea how to solve the problem ?
My add-in code is based on the code below:
private Office.CommandBar commandBar;
private Office.CommandBarButton docSaveButton;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// prepare toolbar:
try
{
commandBar = Application.CommandBars["MY_TOOLBAR"];
}
catch (ArgumentException)
{
//...
}
if (commandBar == null)
{
commandBar = Application.CommandBars.Add("MY_TOOLBAR", 1, missing, true);
}
commandBar.Visible = true;
// addbutton:
docSaveButton = (Office.CommandBarButton)commandBar.Controls.Add(1, missing, missing, missing, missing);
docSaveButton.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIcon;
docSaveButton.Caption = "My save";
docSaveButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(docSaveButtonClick);
}
private void docSaveButtonClick(Office.CommandBarButton ctrl, ref bool cancel)
{
MessageBox.Show("Hello !", "Hello !", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Regards
JanK

I suspect your "add-in" is not loaded, but your toolbar is being persisted. Have you placed your "add-in" in one of Word's startup locations?
Frequently asked questions about the location of templates in Word 2003 or in Word 2007,
Q3: Where Are My Word Add-in Files Saved?,
http://support.microsoft.com/kb/826867.
Loading a Word Add-in, second bullet, http://msdn.microsoft.com/en-us/library/aa165426(office.10).aspx
•Automatically when Word starts, by
saving the template file in the Word
Startup folder on your computer. The
default path to this folder is
C:\Windows\Application
Data\Microsoft\Word\Startup; if you're
using user profiles, the default path
is
C:\Windows\Profiles\UserName\Application
Data\Microsoft\Word\Startup. You can
change this path in the Options dialog
box

Haven't done this in Word, but I believe in Outlook I got it to work by listening for NewWindow Events (called Explorers and Inspectors in Outlook), and re-adding the button when a new window is created (and using "true" as the last parameter in commandBar.Controls.Add to make the button "temporary" so you won't end up with two of 'em).
P.S. I agree it should work like you expect, and am not sure why this is needed (or how it should ever work if "temp" is "false").

I ran into the same problem and resolved it by setting the Tag property on the buttons. This is by design it seems.
http://support.microsoft.com/kb/826931

Related

How do capture when a worksheet is loaded using AddIn Express to show/hide a ribbon?

I've created an excel plugin using Add-In Express for .NET, but can't seem to figure out how to only show my ribbon when there is a certain set of data on the "Active" sheet.
If the worksheet doesn't contain a certain set of data in certain row of cells, I want to hide my ribbon since the buttons on the ribbon don't apply to all worksheets. When the user switches between different worksheets, I'd like to hide/show the ribbon as appropriate.
I tried using the AddinInitialize event, but this only fires once. I don't want to force the user to open the Excel file directly. They should be able to open Excel, then select the file from the File menu.
Here is what I have so far, but I don't know where to put it:
private void OnAddinInitialize(object sender, EventArgs e)
{
// note: this does not work all the time!
adxRibbonTab1.Visible = IsRibbonVisible();
}
private bool IsRibbonVisible()
{
var worksheet = ActiveSheet;
if (worksheet == null)
return false;
// only show ribbon when top row has certain column headings
var reader = new WorksheetReader(worksheet);
return reader.HasColumns(TopLeftCell, RequiredColumnNames);
}
I've tried creating an ADXExcelWorksheetEvents instance and overriding some of the events, but I'm not having much luck so far. I've not been able to find anything on their website for this type of workflow either.
I'd appreciate any help!
I found the answer. On the AddinModule designer, there is an "Events" item that I had to reference and then I was able to trap the following with my IsRibbonVisible() function:
WorksheetActivated
WorksheetDeactivated
WorkbookActivated
Problem solved!

How to release cursor after inserting text into Word Document using C#?

I'm creating a MS Word VSTO addin (Custom Task Pane) that inserts some text into a Word Document on the click of a button. However, I find that after the button is clicked and the text is inserted into the Document, the cursor doesn't automatically return back to the body of the Document. When I hit the space-bar, it triggers the button click event again, instead of inserting a space into the Document.
This is the c# code from the button click:
public void btnInsert_Click(object sender, EventArgs e)
{
Word.Application objApplication = Globals.ThisAddIn.Application;
Word.Selection objSelection = objApplication.Selection;
Word.Range objRange = objSelection.Range;
objRange.InsertAfter("Sample Text");
objRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
objRange.Select();
}
How do I get the cursor back into the Document once my sample text is inserted?
From comments How to send back MS word cursor focus on current document after click in Task Pane looks like this isn't directly possible.
For anybody else with this issue I found this minor modification of the workaround of F10 ('Show key tips') works:
System.Windows.Forms.SendKeys.Send("{F10}");
System.Windows.Forms.SendKeys.Send("{F10}");
This also seems to return focus to the active document (but with drawback of a 'beep'):
System.Windows.Forms.SendKeys.Send("%W");

Blocking Default Send button functionality and performing only custom action in outlook

Objective: IN ADD-IN, i need to block calendar notification from being sent to attendees but at the same time i want to perform some custom action when send button is clicked.. In short i need to perform only custom action associated with send button and NOT the default function of SEND button.
i am using VSTO2010,MS Office 2007, .net 4.
I need to this on Ms office 2003,2007,2010 (support for 2007 and 2010 will be enough).
You need to intercept the OnClick event of the Send button on the CommandBar. Yes, even though Outlook 2010 uses the ribbon and doesn't show any command bar, there is still a programmatic CommandBar that contains buttons. For compatibility, the ribbon buttons still cause the corresponding CommandBarButton object to raise events.
And to add to the complexity, the button you want doesn't exist until AFTER the "appointment" is turned into "meeting".
You'll need some fields:
private CommandBars mCommandBars;
private CommandBarButton mCommandButtonSendMeeting;
In your NewInspector event, add these lines
mCommandBars = this.Inspector.CommandBars;
mCommandBars.OnUpdate += CommandBars_OnUpdate;
elsewhere
private void CommandBars_OnUpdate()
{
if (mCommandButtonSendMeeting == null)
{
mCommandButtonSendMeeting = (CommandBarButton)CommandBars.FindControl(Missing.Value, 2617, Missing.Value, Missing.Value);
mCommandButtonSendMeeting.Click += CommandButtonSendMeeting_Click;
}
}
private void CommandButtonSendMeeting_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
CancelDefault = true;
// Do whatever here.
}
Here you have a different solution. You really do not have to touch the buttons, there's nothing to do with the buttons. You have to use the events that Outlook provides for these purposes. Just include this line in your Add-in_StartUp:
Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
And of course, the implementation of the event handler:
void Application_ItemSend(object Item, ref bool Cancel)
{
MessageBox.Show("Yihha!!");
Cancel = true;
}
This will intercept any message you send. Works on Outlook 2010.

C# VSTO Outlook 2007: Add icon for CommandBarPopup

Hi i want add image icon for my CommandBarPopup button at standard commandbar like Send & Receive Button.
Popup is working, i have Buttons with icons there but I need top icon in popup (next to small dropdown icon)
Code for creating popup:
moznosti = (Office.CommandBarPopup)standardToolbar.Controls.Add(
Office.MsoControlType.msoControlPopup,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
true);
// adding buttons to popup is ok:
nastaveni = (Office.CommandBarButton)moznosti.Controls.Add(1,missing, missing, missing, true);
nastaveni.Caption = "Na&stavení...";
global.SetImage(nastaveni, Properties.Resources.settings);
nastaveni.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
nastaveni.Click += new Office._CommandBarButtonEvents_ClickEventHandler(ButtonSettingsClick);
... etc...
but i need something like this:
moznosti.Picture = ... but there is no Picture variable (like in CommandBarButton object)
It's probably not possible to add an icon to a CommandBarPopup. At least not with VBA. (I'd be surprised if vsto exposed any more of the commandbar model than VBA does.) I don't have Outlook installed on my machine at the moment, so I can't check that implementation of VBA but I'm nearly certain that the commandbar model is the same throughout all office applications. Adding an icon to a popup definitely doesn't seem possible with Excel. (Just tried.)
If you're on Outlook 2007, why don't you just tweek the ribbon instead? A combobox or dropdown might do what you're after.
http://msdn.microsoft.com/en-us/library/bb226712(v=office.12).aspx
Yes, Nick is right. This is not possible. CommandBarPopup is a container control. You can add icon for sub items. Some information you can find also here
http://social.msdn.microsoft.com/Forums/vstudio/en-US/d9e38922-d974-47ee-b758-6002676dcdc6/can-we-add-an-image-to-commandbarpopup-control

How to programmatically enable the "Options..." button in the MS Word Print Setup dialog

I'm trying to display the MS Word "Print Setup" dialog in a VSTO AddIn for Microsoft Word 2003. I can display the dialog box, but the options button at the bottom left corner of the dialog is always disabled as per the following screen capture.
The relevant code for what I've done so far is:
private void printSetup_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
Dialog dialog = App.Dialogs[WdWordDialog.wdDialogFilePrintSetup];
Object missing = Type.Missing;
dialog.Show(ref missing); // Note that the param is TimeOut
}
Can anyone tell me what I have to do to enable the Options button? I know it can be done because we are replacing a template that used to do this in VBA and the button is enabled there...
Regards,
Ben
The properties of dialog boxes are only available through late-binding and since you are using C# you'll need to use InvokeMember to get and set values related to the dialog you are working with.
From the documentation of the WdWordDialog Enumeration you know that for the WdWordDialog.wdDialogFilePrintSetup dialog an Options attribute is available. The link is for Office 2007, but for the case in hand it should suffice.
With this knowledge you can do something like this to set the dialog attribute value:
object objectDialog = (object)dialog;
object[] args = new object[1];
args[0] = (object) null; // Specify value for Options attribute just as in VBA
objectDialog.GetType().InvokeMember(
"Options",
BindingFlags.SetProperty,
null,
objectDialog,
args);
I now have a solution that works that I got from a colleague.
While it doesn't solve the more general case of launching this dialog from any VSTO C# code, it does work for launching this dialog correctly as a result of clicking a toolbar button (which is what we are trying to do). So this fixes the problem for us.
I'm actually of the opinion now that this is a bug (feature?) of MS Word and that there isn't any general way of displaying this dialog from code and having the "Options..." button enabled. I think it can only work if the dialog is invoked automatically by MS Word due to it being hooked up to the CommandBar as a built-in control. I've seen the same behaviour in VBA as well as through VSTO which tends to support the theory that it's a Word limitation/bug.
So we previously had code like this:
public MyCommandBar()
{
_myBar = App.CommandBars.Add("My Toolbar", 1, Type.Missing, true);
// Add a button to call our custom event handler
_printSetup = (CommandBarButton)
_myBar.Controls.Add(MsoControlType.msoControlButton,
Type.Missing, Type.Missing, 1, true);
_printSetup.Click += printSetup_Click(); // Call the handler shown in my original question
// More stuff...
}
And when modified to call the built-in control by changing the second argument (Id) to Controls.Add() from Type.Missing to 511 (the ID for the File Print Setup dialog) like this the "Options..." button is enabled like one would expect:
public MyCommandBar()
{
_myBar = App.CommandBars.Add("My Toolbar", 1, Type.Missing, true);
// Call the built-in File Print Setup dialog automagically
_printSetup = (CommandBarButton)
_myBar.Controls.Add(MsoControlType.msoControlButton,
511, Type.Missing, 1, true);
// More stuff...
}
Hopefully this helps others who run into this problem.

Categories