none
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
    Moderator
  • 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)>

    {

    ....

    DECLARE_REGISTRY_RESOURCEID(IDR_TMEADDIN)

    DECLARE_PROTECT_FINAL_CONSTRUCT()

    BEGIN_COM_MAP(CTMEAddIn)
     COM_INTERFACE_ENTRY(ITMEAddIn)
    //DEL  COM_INTERFACE_ENTRY(IDispatch)
     COM_INTERFACE_ENTRY2(IDispatch, ITMEAddIn)
     COM_INTERFACE_ENTRY(_IDTExtensibility2)
    END_COM_MAP()

    BEGIN_SINK_MAP(CTMEAddIn)

     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)
    END_SINK_MAP()

    private:

     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);

    public:
     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 OnDocumentOpenInfo = {CC_STDCALL, VT_EMPTY, 1,{VT_DISPATCH}};
    _ATL_FUNC_INFO OnDocumentBeforeSaveInfo = {CC_STDCALL, VT_EMPTY, 3,{VT_DISPATCH,VT_BYREF | VT_BOOL,VT_BYREF | VT_BOOL}};
    _ATL_FUNC_INFO OnDocumentBeforeCloseInfo = {CC_STDCALL, VT_EMPTY, 2,{VT_DISPATCH,VT_BYREF | VT_BOOL}};
    _ATL_FUNC_INFO OnWindowSelectionChangeInfo = {CC_STDCALL, VT_EMPTY, 1,{VT_DISPATCH}};

     

    void __stdcall CTMEAddIn::OnDocumentOpen(IDispatch *ptr)
    {
     ShowStringValueMsgBox(L"OnDocumentOpen",L"");

    }

    void __stdcall CTMEAddIn::OnDocumentChange()
    {
     ShowStringValueMsgBox(GetCurActiveDocFilePath(),L"OnDocumentChange");
    }

    void __stdcall CTMEAddIn::OnDocumentBeforeSave(IDispatch *ptr,VARIANT_BOOL * SaveAsUI,VARIANT_BOOL * CancelDefault)
    {
     ShowStringValueMsgBox(L"OnDocumentBeforeSave",L"");

    }

    void __stdcall CTMEAddIn::OnDocumentBeforeClose(IDispatch *ptr,VARIANT_BOOL * CancelDefault)
    {
     ShowIntegerValueMsgBox(*CancelDefault,L"OnDocumentBeforeClose");

     }

    ....

    STDMETHODIMP CTMEAddIn::OnConnection(IDispatch * Application, ext_ConnectMode ConnectMode, IDispatch * AddInInst, SAFEARRAY * * custom)
    {
      // Forge a connection to enable you to receive events
     m_ApplicationDocChangeEvents::DispEventAdvise(m_spApp);
     m_ApplicationDocOpenEvents::DispEventAdvise(m_spApp);
     m_ApplicationDocBeforeSaveEvents::DispEventAdvise(m_spApp);
     m_ApplicationDocBeforeCloseEvents::DispEventAdvise(m_spApp);
     m_ApplicationWindowSelectionChangeEvents::DispEventAdvise(m_spApp);

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

    STDMETHODIMP CTMEAddIn::OnDisconnection(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom)
    {
     ShowStringValueMsgBox(L"ReleaseToolBar",L"");

     // Break the connection with the event source
        m_ApplicationDocChangeEvents::DispEventUnadvise(m_spApp);
     m_ApplicationDocOpenEvents::DispEventUnadvise(m_spApp);
     m_ApplicationDocBeforeSaveEvents::DispEventUnadvise(m_spApp);
     m_ApplicationDocBeforeCloseEvents::DispEventUnadvise(m_spApp);
     m_ApplicationWindowSelectionChangeEvents::DispEventUnadvise(m_spApp);

      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 yettysoft@yahoo.com 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
    Moderator
  • 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 http://msdn.microsoft.com/en-us/library/cc442946(VS.90).aspx to create a sample VSTO add-in.


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


    Thanks,

     

     


    Shiv Khare
    Tuesday, December 6, 2011 4:08 PM
    Moderator
  • 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: x_ugang@sohu.com,  and you?  I can send my full project to you.

    Thanks a lot!

    Xu, gang

    Wednesday, January 4, 2012 8:54 AM