C# VSTO Outlook 2007: Add icon for CommandBarPopup - c#

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

Related

VSTO add-in: Find Monitor where Outlook is displayed

Good day everyone.
All my users have 2 monitors. I have a VSTO Outlook add-in which is basically a ribbon button and a form. If my ribbon button is clicked, I create and display a new form. I don't want my form to be displayed on the same monitor where outlook is displayed. I want to open it on the other monitor. This what I usually do in normal winforms apps:
Screen[] allScreens = Screen.AllScreens;
int i = allScreens.Count<Screen>();
if (i > 1)
{
foreach (Screen s in allScreens)
{
if (s.DeviceName != Screen.FromControl(this).DeviceName)
{
myForm.Location = s.WorkingArea.Location;
myForm.maximise = true;
myForm.Show();
}
}
}
else
{
this._outlookAuths.Show(this);
}
But there is no 'this' pointer when working in the Ribbon class. I tried to get the monitor in which the Ribbon button is shown, but it is not part of the normal winforms button class. Tried to get my form's parent, which should be MS Outlook but that returns null. Just as a sidenote, CentreParent property on my form is also not working, but CentreParent is kind of the opposite of what I want. Thanks in advance.
Use Screen.FromHandle instead. You can retrieve HWND of the Outlook windows by casting Explorer or Inspector OOM objects to the IOleWindow interface and calling IOleWindow.GetWindow.
Use Application.ActiveWindow / ActiveExplorer / ActiveInspector depending on your definition of "where Outlook is displayed".

Adding CustomTaskPane without pane options

I have a CustomTaskPane in Outlook which I'm adding with following code in C#:
var pane = Globals.TrackingAddIn.CustomTaskPanes.Add(new MyControl(), " ");
pane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionRight;
pane.Width = 700;
And in Outlook it looks like this:
Current layout
Is it possible to hide buttons marked in red (pane options and close?) I tried setting negative margin to the control and various values of DockPositionRestrict, but it doesn't change anything. I'd like my control to be shown immediately below the ribbon.
No, the buttons cannot be hidden.
If we are talking about Custom Task Panes - there is no trivial way to hide thease controls.
But instead of using custom task panes you may consider using Advanced Outlook Form Regions. Or just use Windows API for subclassing Outlook windows, see Adjacent Windows In Outlook for more information. The Creating Adjacent Windows In Outlook sample code in C++ is available.

c# Excel - Display ribbon on exit

I'm using VSTO to design an application with an Excel interface. I want to hide the ribbon on startup (shouldn't be needed in the application) and re-display it on exit (if the user had it originally displayed), to avoid irritating people who use the application and want a ribbon the next time they open Excel.
I can hide the ribbon using essentially the following code in ThisWorkbook_Startup (from this question Excel 2007 Minimize the Ribbon programatically but Not the menu bar):
Office.CommandBars cbs = null;
cbs = Application.CommandBars;
foreach (Office.CommandBar commandBar in cbs)
{
if (commandBar.Name == "Ribbon")
{
this.Application.ActiveWindow.Activate();
Application.SendKeys("^{F1}", true);
}
}
However, the same code or similar variations from the previously referenced question do not seem to work when placed in either the ThisWorkbook_Shutdown or ThisWorkbook_BeforeClose methods. The code is hit but never seems to execute - the ribbon is never restored.
Is there another way to restore the ribbon on exit?
Thanks,
Andrew

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.

C#, Word 2003 addIn and a toolbar button event

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

Categories