Excel VSTO Add-In: Cannot reactivate Excel after MessageBox.Show("Test"); - c#

I am implementing a VSTO Excel Add-In which displays some modal dialogs. These dialogs are not shown as own windows in the windows taskbar. But under certain circumstances these dialogs are not are vanishing from the top of Excel and cannot got back by using the task bar!
The whole story (reproducable with Windows XP - 7, Excel 2007 - 2010):
I do open Excel and create two or more new workbooks
I do show a modal dialog, let's say via "MessageBox.Show" I open "notepad" and maximize its window
I try to reactivate one of the excel workbook windows via the windows
taskbar
I expect: Excel will come up with my modal MessageBox to
appear on top
The actual result is: Neither the MessageBox nor any of the Excel workbooks comes up, when you click a workbook item in the windows taskbar!
Why???
I can reactivate Excel via Ctrl + Tab. Then my modal dialog / MessageBox is correctly on the top. It's easy to reproduce if you have Visual Studio and Excel. Please help!
Greets, Jörg
Code-Sample:
Just create an empty Visual C# / Office / 2010 / Office 2010 Add-In
Replace the content of "ThisAddIn.cs" by the following code:
-
namespace ExcelAddIn6
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
NativeWindow excelWindowThatIsTheOwner = null;
try
{
//Get excel main window to set owner (however does not help anyway)
excelWindowThatIsTheOwner = new NativeWindow();
excelWindowThatIsTheOwner.AssignHandle(new IntPtr(Globals.ThisAddIn.Application.Hwnd));
//Create two more workbooks to have more than one...
Globals.ThisAddIn.Application.Workbooks.Add();
Globals.ThisAddIn.Application.Workbooks.Add();
Globals.ThisAddIn.Application.WindowState = Excel.XlWindowState.xlMaximized;
//Show modal dialog (here: a message box, but )
MessageBox.Show(owner: excelWindowThatIsTheOwner,
text: "I am a modal MessageBox.\nNow bring another application to the foreground and then try to bring excel back via the windows taskbar...");
//Problem stays the same for modal forms
var myForm = new Form();
myForm.ShowInTaskbar = false;
myForm.ShowDialog(excelWindowThatIsTheOwner);
}
finally
{
//Cleanup
if (excelWindowThatIsTheOwner != null) excelWindowThatIsTheOwner.ReleaseHandle();
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#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
}

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

Hide header text and header toolbar in Outlook addin

I was searching for few days and cannot find any solution. Do you know how to hide the header text and header "toolbar" in the adjoining Outlook addin?
I dont want to have the text "My custom addin" and i dont want to have this "+" and line.
You might be happier with a "Custom Task Pane." It still has some UI associated with it, but it's prettier.
Or, you can use a blank space (ie. " ") for the title:
To create one, create a UserControl in your project. That's MyTaskPane below. Then, in your ThisAddIn class, create the task pane for windows that you want it on. The following would create the task pane on every window.
You should also remove the task panes when each window closes as described in the MSDN documentation.
public partial class ThisAddIn
{
Inspectors _inspectors;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
MyTaskPane pane = new MyTaskPane();
var taskPane = this.CustomTaskPanes.Add(pane, "My Custom Task Pane");
taskPane.Visible = true;
_inspectors = Application.Inspectors;
_inspectors.NewInspector += Inspectors_NewInspector;
}
void Inspectors_NewInspector(Outlook.Inspector Inspector)
{
MyTaskPane pane = new MyTaskPane();
var taskPane = this.CustomTaskPanes.Add(pane, "My Custom Task Pane", Inspector);
taskPane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionBottom;
taskPane.Visible = true;
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#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
}
If its Form Region's you are looking for, have a look at Add-in Express. They have a product called Regions for Outlook and VSTO.
Key features
Advanced Regions provide developers several unique advantages over the traditional Outlook Form Regions available in VSTO projects:
You can add regions to practically all Outlook windows
You can create form regions for Inspector windows and view regions for Outlook Explorer windows
Your regions work for all versions of Outlook from 2003 to 2013
Your regions are shared (multiple regions can share the same area with the ability to switch between them)
Your regions can be hidden and minimized Your regions can be dragged between Outlook panes
Folder view pane regions
Reading pane regions
Task pane dock based regions

How can you add a Custom Panel in a Visio 2013 add-in?

Recently I wrote an outlook add-in which has a ribbon.xml file for an extra ribbon, context menu's, etc. I also added an extra panel docked on the right of my window.
Now I've begun some research as on how to create add-ins for Visio. The ribbon.xml is practically the same, so that's not a problem at all. However, I can't seem to find any way to add a custom panel when a Visio document is opened.
So far I have this in Visio to know if a document is opened/created/changed:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
MessageBox.Show("Visio Add-In V1");
Globals.ThisAddIn.Application.DocumentChanged += new Visio.EApplication_DocumentChangedEventHandler(docChanged);
Globals.ThisAddIn.Application.DocumentOpened += new Visio.EApplication_DocumentOpenedEventHandler(docChanged);
Globals.ThisAddIn.Application.DocumentCreated += new Visio.EApplication_DocumentCreatedEventHandler(docChanged);
}
private void docChanged(Visio.Document doc)
{
MessageBox.Show("Document loaded");
}
In outlook I would do this to add a custom panel (simplified):
MyPanel ctrl = new MyPanel();
Microsoft.Office.Tools.CustomTaskPane ctp = Globals.ThisAddIn.CustomTaskPanes.Add(ctrl, title);
ctp.Visible = true;
ctp.Width = 300;
ctp.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionRight;
Now how would I be able to do this in a Visio 2013 Add-In?
Edit:
Unfortunately this makes me think it's not possible:
http://msdn.microsoft.com/en-us/library/vstudio/bf08984t.aspx
Edit2:
The following answer should work: Are Task Panes Available in Visio VSTO?
However I can't seem to find a way to get a docked panel on my main window. Here is what I tried:
Globals.ThisAddIn.Application.Windows.Add("testpanel", VisWindowStates.visWSDockedLeft, VisWinTypes.visStencilAddon, null, null, null, 300);
This adds the window as if it were a new drawing...
Edit3:
Visio throws a COM exception on this saying I have an invalid window type.
Application.Windows.Add("testpanel", VisWindowStates.visWSDockedRight, VisWinTypes.visAnchorBarAddon, null, null, 300);
You can use Anchor Bars in Visio, not Task Panes
If you download the Visio SDK and look in the Codes Samples Library, you will find Anchor Bar Usage under User Interface.
For completeness, you may wish to review this MSDN article Windows.Add Method (Visio) - http://msdn.microsoft.com/en-us/library/office/ff767674.aspx

CustomTaskPane wont close completely

When I close a CustomTaskPane and change the worksheet, it seems that a pane wasn't removed completely. And the Worksheet area of size of CustomTaskPane becomes inactive.
Here is how I am doing in code:
ctp.Visible = false;
And in PaneVisibleChanged event:
private void PaneVisibleChanged(object sender, EventArgs e)
{
CustomTaskPane pane = (CustomTaskPane)sender;
if (!pane.Visible)
{
pane.Control.Dispose();
CustomTaskPanes.Remove(pane);
}
}
Observation: It happens when I have two CustomTaskPanes side by side and I am closing one.
I'm facing a similar issue with a custom task pane not disposing correctly in VSTO Excel 2010. The code I'm using that tries to fix the issue is simple:
var taskbar = Globals.ThisWorkbook.Application.CommandBars["Task Pane"];
taskbar.Reset();
The Visible event of the side panel does not seem to work ok for me. I usually show or hide the task pane like this:
Globals.ThisWorkbook.Application.CommandBars["Task Pane"].Visible = false;

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