Programatically position Office 2007 Floating Custom Task Pane

SAME OUTSIDE OF GFW:http://aritrasaha.wordpress.com/2009/05/19/programatically-position-office-2007-floating-custom-task-pane/

 

Programatically repositioning the Custom Task Pane (CTP) created using Word / Excel 2007 is possible, when the DockPosition of the CTP is set as

MsoCTPDockPosition.msoCTPDockPositionFloating.

Since the Microsoft.Office.Tools.CustomTaskPane object does not have any top or left properties (it only provides the height and width) - I tried setting the top and left of the Task Pane Command Bar objects of the CTP, since I had earlier successfully used the Command Bar objects to retrieve the top and left position of the CTP.

if (this.CommandBars["My Task Pane"].Position == Microsoft.Office.Core.MsoBarPosition.msoBarFloating){this.CommandBars["My Task Pane"].Top = 0;    this.CommandBars["My Task Pane"].Left = 0;}
While setting the top and left property of the task pane, I got the exception :-

{"Error HRESULT E_FAIL has been returned from a call to a COM component."}

The following method using the Windows API function MoveWindow worked fine in moving the floating task pane to the desired position.

public partial class NativeMethods

{

[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "FindWindowW")]

public static extern System.IntPtr FindWindowW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]string lpClassName, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]string lpWindowName);

 

 

[System.Runtime.InteropServices.DllImportAttribute("user32.dll",EntryPoint = "MoveWindow")]
[return:System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]

public static extern bool MoveWindow([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]bool bRepaint);

}

  // Invoking the Win API Methods from the Word AddIn

IntPtr ptr = NativeMethods.FindWindowW("MsoCommandBar","My Task Pane");              NativeMethods.MoveWindow(ptr, 400, 220, 240, 360, true);

 

 


posted @ 2012-03-26 20:49  BinSys  阅读(370)  评论(0编辑  收藏  举报