|
|
|
date: Wed, 29 Aug 2007 01:59:29 -0700,
group: microsoft.public.word.vba.addins
back
VSTO Custom Task Panes
I have written an Application level addin for Word 2007 in C# using
VSTO. I have a toggle button on my ribbon which displays a custom task
pane. This all works fine.
I also have a VB6 application which creates Word documents, this
application needs to tell my AddIn to display the custom task pane. So
I exposed a method in my AddIn which creates the custom task pane. If
I use the VB editor in Word to call that method everything works fine;
but if I call the method from a different application I get an invalid
cast exception:
Message:
{"Unable to cast COM object of type 'System.__ComObject' to interface
type 'Microsoft.Office.Tools.ICustomTaskPaneSite'. This operation
failed because the QueryInterface call on the COM component for the
interface with IID '{3CA8CD11-274A-41B6-A999-28562DAB3AA2}' failed due
to the following error: No such interface supported (Exception from
HRESULT: 0x80004002 (E_NOINTERFACE))."}
Stack trace:
at
Microsoft.Office.Tools.CustomTaskPaneAdapter..ctor(IRuntimeServiceProvider
runtimeServiceProvider, UserControl control, String title, Object
window)
at
Microsoft.Office.Tools.CustomTaskPaneFactoryAdapter.Microsoft.Office.Tools.ICustomTaskPaneFactoryContract.Create(UserControl
control, String title, Object window)
at
Microsoft.Office.Tools.CustomTaskPaneCollection.AddHelper(UserControl
control, String title, Object window)
at Microsoft.Office.Tools.CustomTaskPaneCollection.Add(UserControl
control, String title, Object window)
at TestWordAddin.ThisAddIn.AddPanel(Window Window) in C:\Source\ECS
C#\TestWordAddin\TestWordAddin\ThisAddIn.cs:line 27
I have spent a while looking at this now and my theory is that because
my Custom Task Pane is a Control it must be created on the same thread
as Word, so when my Application creates the control I get an error.
That is where I am stuck, how do I invoke the correct thread (or am I
barking up the wrong tree)?
All comments gratefully received.
Dave
I have attached my AddIn code and the VB6 code which is calling my
addin's method.
My AddIn Code (simplified):
using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
namespace TestWordAddin
{
public partial class ThisAddIn
{
private ThisAddInCOM m_TestAddinUtils;
private void ThisAddIn_Startup(object sender, System.EventArgs
e)
{
}
private void ThisAddIn_Shutdown(object sender,
System.EventArgs e)
{
}
public void AddPanel(Word.Window Window)
{
// Create and add a custom task pane to the specified
window
try
{
UserControl1 panel = new UserControl1();
Microsoft.Office.Tools.CustomTaskPane ctp =
this.CustomTaskPanes.Add(panel, "TestAddIn", Window);
ctp.DockPosition =
Office.MsoCTPDockPosition.msoCTPDockPositionTop;
ctp.Height = 57;
ctp.Visible = true;
}
catch (Exception Ex)
{
MessageBox.Show(Ex.ToString());
}
}
protected override Object RequestComAddInAutomationService()
{
if (m_TestAddinUtils == null)
m_TestAddinUtils = new ThisAddInCOM(this);
return m_TestAddinUtils;
}
#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
}
/// <summary>
/// Interface for COM interop
/// </summary>
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
[System.Runtime.InteropServices.InterfaceType(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch)]
public interface IThisAddInCOM
{
void AddPanel(Word.Window Window);
}
/// <summary>
/// Class for com interop (the addin cannot be exposed directly to
COM)
/// </summary>
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public class ThisAddInCOM : IThisAddInCOM
{
private ThisAddIn AddIn;
public ThisAddInCOM(ThisAddIn AddIn)
{
this.AddIn = AddIn;
}
public void AddPanel(Word.Window Window)
{
AddIn.AddPanel(Window);
}
}
}
My VB6 Code which call my Addin's method:
Dim wordapp As Word.Application
Dim testaddin_com As Object
Dim testaddin_addin As Object
Dim activeWindow As Word.Window
'Display Custom task pane
Set wordapp = GetObject(, "Word.Application")
Set activeWindow = wordapp.activeWindow
Set testaddin_com = wordapp.COMAddIns("TestWordAddIn")
If Not testaddin_com Is Nothing Then
Set wordapp = Nothing
Set testaddin_addin = testaddin_com.object
If Not testaddin_addin Is Nothing Then
testaddin_addin.AddPanel activeWindow
End If
End If
date: Wed, 29 Aug 2007 01:59:29 -0700
author: unknown
|
|