When creating a custom task pane (_CustomTaskPane MSDN) and setting its DockPosition to floating, I want to specify the Top and Left properties of the window appeared. As Office COM APIs do not provide direct possibility to do this, people advise to alter the values of corresponding properties of CommandBar:
var application = (_Application)_nativeTaskPane.Application;
application.CommandBars["Task Pane Title"].Top = top;
application.CommandBars["Task Pane Title"].Left = left;
In the code above I assume that
1) _nativeTaskPane is my instance implementing _CustomTaskPane (actually it is Microsoft.Office.Core.CustomTaskPane)
2) _Application is Microsoft.Office.Interop.Excel._Application
Of course, I'm doing it after setting Visible = true. Even subscribed to task pane's VisibleStateChange to be more sure. However I'm getting a COMException with the HRESULT E_FAILED.
The thing is I can read these properties (Top & Left) when debugging, however setting them throws exception.
Looks like the issue popped up in the internet at least several times:
1) http://www.add-in-express.com/forum/read.php?FID=1&TID=5595
2) [http://aritrasaha.wordpress.com/2009/05/19/programatically-position-office-2007-floating-custom-task-pane/]
3) [http://www.visualstudiodev.com/visual-studio-tools-for-office/need-location-of-custom-task-pane-45822.shtml]
The workaround is using Windows API. However, can anyone explain what can be wrong with using CommandBar-approach? Maybe I can "reconfigure" smth for this Top/Left-setters to work without exceptions.