Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / October 2003

Tip: Looking for answers? Try searching our database.

GetType().GetEvents(); return empty collection Word AddIn with late binding

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Wibo - 23 Oct 2003 15:07 GMT
I'm trying to attach event handlers to Word events in my AddIn, when I
use early binding there is no problem.

We want to support multiple Word versions so we want to use late
binding, then the problems start, the Word application object is
telling me there are no events.

After reading a lot of messages here, I thought the code below would
do the trick.

Does anyone know what I'm doing wrong ?

public void DocumentOpenHandler( object o )
{
}

public delegate void DelegateDocumentOpen( object o );

public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
    DelegateDocumentOpen del = new DelegateDocumentOpen(
DocumentOpenHandler );
    // this line results in an empty collection
    System.Reflection.EventInfo [] eventInfos =
application.GetType().GetEvents();
    // this line is returning null
    System.Reflection.EventInfo eventInfo =
application.GetType().GetEvent(
"ApplicationEvents3_DocumentOpenEventHandler" );
    try
    {
        eventInfo.AddEventHandler( application, del );
    }
    catch( Exception )
    {
        Type wordType = application.GetType();
        object name = wordType.InvokeMember("Name",
            BindingFlags.GetProperty, null, application, new object[] {});
        // this line writes "Microsoft Word"
        Trace.WriteLine( (string)name );
    }
Wibo - 24 Oct 2003 13:23 GMT
Tried a completely different approach, which helped me before when
events were not fired through the automatically generated COM
interops. The only difference is that I used early binding before, so
I could inherit from the original interface.

The code below works, events arrive in the class.
Now I want to get rid of the early binding:

assign the guid to the class using GuidAttribute
giving all methods there proper DispID's

it still works !!!

So now I remove the  ": Word.ApplicationEvents2" from the class
definition.
Hoping that .Net will provide the IUnknown and IDispatch.

Unfortunately the UCOMIConnectionPointContainer.Advise is now throwing
an exception (HRESULT = 0x80040200) (interface specific error code
(FACILITY_ITF == 4).)

How do I continue now, should I provide the IUnknown and IDispatch, I
have no idea how ?!?!

I have the feeling I'm really close, if this works the only thing I
have to do is replacing all the parameters that are Word type with
object and I'm in business.

Anyone ?

Here is the code:

public class Connect : Object, Extensibility.IDTExtensibility2
{
    ApplicationEvents2 appEvents = null;

    public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
    {
        appEvents = new ApplicationEvents2( application );
    }

    // omitted the rest of the wizard generated code
}

[GuidAttribute("000209Fe-0000-0000-C000-000000000046")]
// This code works, but I can't remove the interface from the class
definition
public class ApplicationEvents2 : Word.ApplicationEvents2
{
    // .Net COM stuff, thought the COM times were over
    private UCOMIConnectionPoint icp2;
    // deja vu, the cookies are back
    private int cookie2 = -1;

    IntPtr wordAppIUnknownpointer = (IntPtr)0;

    public ApplicationEvents2( object wordApplication )
    {

        try
        {
            Trace.WriteLine( "*** Connecting ApplicationEvents2" );

            // COM magic
            UCOMIConnectionPointContainer icpc;
            // Guid of the ApplicationEvents2 dispatch interface
            Guid guid = new Guid( "{000209Fe-0000-0000-C000-000000000046}" );
            //Guid guid = typeof(Word.ApplicationEvents2).GUID;

            icpc = (UCOMIConnectionPointContainer)wordApplication ;
       
            icpc.FindConnectionPoint(ref guid, out icp2);
            icp2.Advise(this, out cookie2);
            // end COM magic

            // save the IUnknown pointer for identification later on
            wordAppIUnknownpointer = Marshal.GetIUnknownForObject(
wordApplication );
        }
        catch( Exception e )
        {
            Trace.WriteLine( e.Message );
        }
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    public void Dispose()
    {
        Dispose( true );
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    public void Dispose( bool disposing )
    {
        if( disposing )
        {
            // Release event sink
            if (-1 != cookie2) icp2.Unadvise(cookie2);
            cookie2 = -1;
        }
    }
    #region ApplicationEvents2 Members

    [DispId(3)]
    public void DocumentChange()
    {
    }

    [DispId(10)]
    public void WindowActivate(Word.Document Doc, Word.Window Wn)
    {
    }

    [DispId(14)]
    public void WindowBeforeDoubleClick(Word.Selection Sel, ref bool
Cancel)
    {
    }

    [DispId(9)]
    public void NewDocument(Word.Document Doc)
    {
        Trace.WriteLine( "NewDocument" );
    }

    [DispId(7)]
    public void DocumentBeforePrint(Word.Document Doc, ref bool Cancel)
    {
    }

    [DispId(6)]
    public void DocumentBeforeClose(Word.Document Doc, ref bool Cancel)
    {
    }

    [DispId(12)]
    public void WindowSelectionChange(Word.Selection Sel)
    {
    }

    [DispId(8)]
    public void DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI,
ref bool Cancel)
    {
    }

    [DispId(1)]
    public void Startup()
    {
    }

    [DispId(13)]
    public void WindowBeforeRightClick(Word.Selection Sel, ref bool
Cancel)
    {
    }

    [DispId(11)]
    public void WindowDeactivate(Word.Document Doc, Word.Window Wn)
    {
    }

    [DispId(2)]
    public void Quit()
    {
    }

    [DispId(4)]
    public void DocumentOpen(Word.Document Doc)
    {
    }

    #endregion

}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.