-2

I have created a basic COM add-in for MS office apps like Word, PPT, Excel in Visual C++ using _IDTExtensibility2 interface.

Now i want my add-in to find if Word, PPT, Excel has any unsaved changes.

FYI: I have not chosen VSTO or Office JS add-in due to dependency/deployment reasons.

Kindly provide solution in C++.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – Adrian Mole Apr 07 '22 at 14:50

1 Answers1

0

An Office host Application instance is passed to the OnConnection method implemented in your code. Depending on the host you could use the Saved property, for example, in case of Word the Document class provides the Saved property which returns true if the specified document or template has not changed since it was last saved. False if Microsoft Word displays a prompt to save changes when the document is closed.

In case of Excel you can use the Workbook.Saved property which returns true if no changes have been made to the specified workbook since it was last saved.

In case of PowerPoint the Presentation.Saved property determines whether changes have been made to a presentation since it was last saved.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • // _IDTExtensibility2 Methods public: CComQIPtr pWordApp; CComQIPtr pWordDoc; STDMETHOD(OnConnection)(LPDISPATCH Application, ext_ConnectMode ConnectMode, LPDISPATCH AddInInst, SAFEARRAY * * custom) { pWordApp = Application; pWordApp->get_ActiveDocument(&pWordDoc); if (pWordDoc) { MessageBoxW(NULL, L"ActiveDoc present", L"My Addin", MB_OK); } else { MessageBoxW(NULL, L"ActiveDoc not present", L"My Addin", MB_OK); } return S_OK; } – Shabari Pragash Apr 08 '22 at 02:55
  • I tried using Saved property for Word, but when i open word i didn't get the active document object i.e., i get "ActiveDocument not present" message box. Maybe it is too early to call get_activedocument() as during onConnection the document is yet to be created. But i don't know how/where to call it once document is created. I tried calling it inside onStartupComplete() too, still object is NULL. I also tried calling it inside thread after sleeping for few seconds so that i create document in that sleep time, still object is NULL. Kindly guide along with some sample C++ code/projects. – Shabari Pragash Apr 08 '22 at 03:00