Event_DocumentBeforeSave cannot be trigger in word2007,but it can be done in word2003. The source code is same! RRS feed

  • Question

  • I develop the word com add-in with VC6 + word2003.

    It is ok to test on word2003, but there is nothing happen on word2007 for Event_DocumentBeforeSave.

    Please help me!!! what should I do?


    Monday, November 21, 2011 7:26 AM

All replies

  • Hmm...I'm not quite familiar with create COM add-in by using VC6. But if you can provide some code snippet, we can do some research and consult some senior engineer.

    In addtion, I would recommend you to create Office add-in by using VSTO, which should be quite easy to learn and develop than using VC6.

    Good day,

    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, November 23, 2011 12:17 PM
  • My code, as follows, if these codes are not enough to analyze root course, I can send all project codes to you, what is your email?  Thank you for your great help!

    1 .  *.h

    class ATL_NO_VTABLE CTMEAddIn :

     public CComObjectRootEx<CComSingleThreadModel>,
     public CComCoClass<CTMEAddIn, &CLSID_TMEAddIn>,
     public IDispatchImpl<ITMEAddIn, &IID_ITMEAddIn, &LIBID_WORDTMEADDINLib>,
     public IDispatchImpl<_IDTExtensibility2, &IID__IDTExtensibility2, &LIBID_AddInDesignerObjects>,

    public IDispEventSimpleImpl<1,CTMEAddIn,&__uuidof(Office::_CommandBarButtonEvents)>,


     //word events
     public IDispEventSimpleImpl<20, CTMEAddIn, &__uuidof(Word::ApplicationEvents)>,
     public IDispEventSimpleImpl<21, CTMEAddIn, &__uuidof(Word::ApplicationEvents)>,
     public IDispEventSimpleImpl<22, CTMEAddIn, &__uuidof(Word::ApplicationEvents)>,
     public IDispEventSimpleImpl<23, CTMEAddIn, &__uuidof(Word::ApplicationEvents)>,
     public IDispEventSimpleImpl<24, CTMEAddIn, &__uuidof(Word::ApplicationEvents)>







     SINK_ENTRY_INFO(1, __uuidof(Office::_CommandBarButtonEvents),/*dispid*/ 0x01, OnClickButtonIMEShow, &OnClickButtonInfo)

     //word events
     SINK_ENTRY_INFO(20, __uuidof(Word::ApplicationEvents), /*dispid =*/ 0x03, OnDocumentChange, &OnDocumentChangeInfo)
     SINK_ENTRY_INFO(21, __uuidof(Word::ApplicationEvents), /*dispid =*/ 0x04, OnDocumentOpen, &OnDocumentOpenInfo)
     SINK_ENTRY_INFO(22, __uuidof(Word::ApplicationEvents), /*dispid =*/ 0x08, OnDocumentBeforeSave, &OnDocumentBeforeSaveInfo)
     SINK_ENTRY_INFO(23, __uuidof(Word::ApplicationEvents), /*dispid =*/ 0x06, OnDocumentBeforeClose, &OnDocumentBeforeCloseInfo)
     SINK_ENTRY_INFO(24, __uuidof(Word::ApplicationEvents), /*dispid =*/ 0x0C, OnWindowSelectionChange, &OnWindowSelectionChangeInfo)


     CComPtr < Office::_CommandBarButton> m_spCmdButtonIMEShow;

     typedef IDispEventSimpleImpl</*nID =*/ 1,CTMEAddIn, &__uuidof(Office::_CommandBarButtonEvents)> m_CommandButtonIMEShowEvents;

     typedef IDispEventSimpleImpl</*nID =*/ 20,CTMEAddIn, &__uuidof(Word::ApplicationEvents)> m_ApplicationDocChangeEvents;
     typedef IDispEventSimpleImpl</*nID =*/ 21,CTMEAddIn, &__uuidof(Word::ApplicationEvents)> m_ApplicationDocOpenEvents;
     typedef IDispEventSimpleImpl</*nID =*/ 22,CTMEAddIn, &__uuidof(Word::ApplicationEvents)> m_ApplicationDocBeforeSaveEvents;
     typedef IDispEventSimpleImpl</*nID =*/ 23,CTMEAddIn, &__uuidof(Word::ApplicationEvents)> m_ApplicationDocBeforeCloseEvents;
     typedef IDispEventSimpleImpl</*nID =*/ 24,CTMEAddIn, &__uuidof(Word::ApplicationEvents)> m_ApplicationWindowSelectionChangeEvents;

     void  __stdcall OnDocumentChange();
     void  __stdcall OnDocumentOpen(IDispatch *ptr);
     void  __stdcall OnDocumentBeforeSave(IDispatch *ptr,VARIANT_BOOL * SaveAsUI,VARIANT_BOOL * CancelDefault);
     void  __stdcall OnDocumentBeforeClose(IDispatch *ptr,VARIANT_BOOL * CancelDefault);
     void  __stdcall OnWindowSelectionChange(IDispatch *ptr);

     STDMETHOD(OnConnection)(IDispatch * Application, ext_ConnectMode ConnectMode, IDispatch * AddInInst, SAFEARRAY * * custom);
     STDMETHOD(OnDisconnection)(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom);
     STDMETHOD(OnAddInsUpdate)(SAFEARRAY * * custom);
     STDMETHOD(OnStartupComplete)(SAFEARRAY * * custom);
     STDMETHOD(OnBeginShutdown)(SAFEARRAY * * custom);


    2.  *.cpp

    _ATL_FUNC_INFO OnDocumentChangeInfo = {CC_STDCALL, VT_EMPTY, 0};
    _ATL_FUNC_INFO OnWindowSelectionChangeInfo = {CC_STDCALL, VT_EMPTY, 1,{VT_DISPATCH}};


    void __stdcall CTMEAddIn::OnDocumentOpen(IDispatch *ptr)


    void __stdcall CTMEAddIn::OnDocumentChange()

    void __stdcall CTMEAddIn::OnDocumentBeforeSave(IDispatch *ptr,VARIANT_BOOL * SaveAsUI,VARIANT_BOOL * CancelDefault)


    void __stdcall CTMEAddIn::OnDocumentBeforeClose(IDispatch *ptr,VARIANT_BOOL * CancelDefault)



    STDMETHODIMP CTMEAddIn::OnConnection(IDispatch * Application, ext_ConnectMode ConnectMode, IDispatch * AddInInst, SAFEARRAY * * custom)
      // Forge a connection to enable you to receive events

     return S_OK;// If return E_NOTIMPL , the addin will not be auto-loaded next time

    STDMETHODIMP CTMEAddIn::OnDisconnection(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom)

     // Break the connection with the event source

      return S_OK;

    STDMETHODIMP CTMEAddIn::OnAddInsUpdate(SAFEARRAY * * custom)
     //::MessageBox(NULL, L"OnAddInsUpdate", NULL, MB_OK);

     return E_NOTIMPL;

    STDMETHODIMP CTMEAddIn::OnStartupComplete(SAFEARRAY * * custom)
     //::MessageBox(NULL, L"OnStartupComplete", NULL, MB_OK);

     return E_NOTIMPL;

    STDMETHODIMP CTMEAddIn::OnBeginShutdown(SAFEARRAY * * custom)
     //::MessageBox(NULL, L"OnBeginShutdown", NULL, MB_OK);

     return E_NOTIMPL;


    Thursday, November 24, 2011 1:50 AM
  • Hi,

    Could you please provide the full project of the sample source code in zip format to so that I could investigate the issue?

    Also which version of Visual Studio are you using to compile this project?


    Tuesday, November 29, 2011 10:38 PM
  • Hi x_ugang,

    Thanks for your information, however, in order to reproduce the issue, these code is not enough. Could you package your project, upload into SkyDrive and post back a link here. We need the source project to do some further research.

    I look forward to hearing of you soon.

    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us
    Thursday, December 1, 2011 10:45 AM
  • Hi X_ugang,

    Do you have any other add-ins installed on Word 2007 machine? Try disabling all other add-ins.

    For a quick test to verify if the issue is with your code or if it is from Office perspective, Create a VSTO add-in for Word (You can use Visual studio 2008 or Visual Studio 2010) and see if you are able to handle the DocumentBeforeSave event. You can follow the steps on to create a sample VSTO add-in.

    You can also try handling the DocumentBeforeSave event in VBA to verify that it is getting fired.




    Shiv Khare
    Tuesday, December 6, 2011 4:08 PM
  • hi, Shiv Khare

    I have a test according to your suggest. It is ok!!!

    DocumentBeforeSave event in VBA can be  fired.

    But I still puzzle about my VC project, why it cannot do work normally?

    My email:,  and you?  I can send my full project to you.

    Thanks a lot!

    Xu, gang

    Wednesday, January 4, 2012 8:54 AM