我用C++写了一个ATL,在WORD启动的时候跟着启动,现在想拦截(重载)WORD中的事件或命令,比如说FILEOPEN(),FILESAVE()等等,该如何实现?
回答用VBA写WORD模板的不给分。

解决方案 »

  1.   

    http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q183/5/99.asp&NoWebContent=1
    How To Catch Microsoft Word97 Application Events Using VC++
    View products that this article applies to.
    This article was previously published under Q183599
      

  2.   

    http://support.microsoft.com/default.aspx? VCBASK上面有的
      

  3.   

    http://www.codeproject.com/com/adwordaddin.aspHandling Events
    Probably in your addin, you'd also be interested in handling some of Word's events.A case in point is the Application objects DocumentOpen event, with DISPID=4, which is handled here. Word has a complex object model and you will find a host of other such events. If you use the good old OLE/COM Object Viewer to view msword9.olb, you'd find IDL like :          ....        [id(0x00000003), helpcontext(0x00061a83)]
                void DocumentChange();
                [id(0x00000004), helpcontext(0x00061a84)]
                void DocumentOpen([in] Document* Doc);        ......As before we will use ATL's IDispEventSimpleImpl<> template class to implement our sink. For brevity, only the changes necessary to the earlier code has been mentioned.extern _ATL_FUNC_INFO DocumentOpenInfo;class ATL_NO_VTABLE CAddin : 
        public CComObjectRootEx,
        public CComCoClass,
        public ISupportErrorInfo,
        public IDispatchImpl,
        public IDispatchImpl<_IDTExtensibility2, &IID__IDTExtensibility2,  
             &LIBID_AddInDesignerObjects>,
        public IDispEventSimpleImpl<1,CAddin,     
             &__uuidof(MSWord::ApplicationEvents2)>
    {
    public:
    ....    
    ....void __stdcall DocumentOpen(IDispatchPtr ptr)
    {
        CComQIPtr<_Document> spDoc(ptr);
        ATLASSERT(spDoc);
        ....
        ....
    }BEGIN_SINK_MAP(CAddin)
    SINK_ENTRY_INFO(1,__uuidof(MSWord::ApplicationEvents2),4,
        DocumentOpen,&DocumentOpenInfo)
    END_SINK_MAP()private:
    CComPtr<MSWord::_Application> m_spApp;
    };DocumentOpenInfo is defined at the top of CAddin.cpp as _ATL_FUNC_INFO DocumentOpenInfo = {CC_STDCALL,VT_EMPTY,1,
        {VT_DISPATCH|VT_BYREF}};All that remains for us to do is to add the code to setup and break down the connection. Using the ATL template class, therefore all we have to do is call DispEventAdvise() and DispEventUnadvise(). Our CAddin's OnConnection() and OnDisconnection(), needless to say, is the right place for doing this.CComQIPtr<_Application> spApp(Application);
    ATLASSERT(spApp);
    m_spApp = spApp;
    HRESULT hr = DispEventAdvise(m_spApp);
    if(FAILED(hr))
    return hr;
    and in OnDisconnection(), DispEventUnadvise(m_spApp);
    m_spApp = NULL;