question

ShabariPragash-5690 avatar image
0 Votes"
ShabariPragash-5690 asked RLWA32-6355 commented

C++ COM add-in - find if MS Word, PPT, Excel has unsaved changes

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++.

c++office-excel-itprooffice-addins-devoffice-word-itprooffice-powerpoint-itpro
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered ShabariPragash-5690 commented

You should review the object models for each of Excel, Word and PowerPoint. If you look carefully you will see that the Excel workbook object, the Word document object and the PowerPoint presentation object each have a Saved property which will give you the information you want.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.



     // _IDTExtensibility2 Methods
     public:
         CComQIPtr<Word::_Application> pWordApp;
         CComQIPtr<Word::_Document> pWordDoc;
        
         STDMETHOD(OnConnection)(LPDISPATCH Application, ext_ConnectMode ConnectMode, LPDISPATCH AddInInst, SAFEARRAY * * custom)
         {
             pWordApp = Application;
        
             pWordApp->get_ActiveDocument(&pWordDoc);
        
             if (pWordDoc)
             {
                 MessageBoxW(NULL, L"ActiveDocument present", L"Native Addin", MB_OK);
             }
             else
             {
                 MessageBoxW(NULL, L"ActiveDocument not present", L"Native Addin", MB_OK);
             }
                
             VARIANT_BOOL prop = VARIANT_FALSE;
             pWordDoc->get_Saved(&prop);
             if (prop)
             {
                 MessageBoxW(NULL, L"Saved=True", L"Native Addin", MB_OK);
             }
             else
             {
                 MessageBoxW(NULL, L"Saved=False", L"Native Addin", MB_OK);
             }
        
             return S_OK;
         }



0 Votes 0 ·

I tried using Saved property for Word. The build generation went success for the following code 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() method as during onConnection the document is yet to be created. But i don't know how/where to call get_activedocument() method once document is created.

I tried calling it inside onStartupComplete() too, still object is NULL. I also tried creating a thread and call this method inside the thread after sleeping for few seconds so that i could create a new document when that thread is sleeping, still object is NULL.

I am not sure what i am missing.

Kindly guide along with some sample C++ code/projects as the MS documentation is only available in C#/VB.

FYI: I have added my code sample as comment.

0 Votes 0 ·
RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered RLWA32-6355 commented

Maybe it is too early to call get_activedocument() method as during onConnection the document is yet to be created. But i don't know how/where to call get_activedocument() method once document is created.

Yes, that's a problem. Since I previously provided you a sample Add-in for a TaskPane I updated the sample to provide some minimal functionality. All the previous comments about how it was created are still in effect. The updated sample can create a TaskPane in Excel and Word. The ActiveX control hosted by the TaskPane now includes two items - an edit control that identifies which Office application it is loaded into and a button that checks the Saved property.

For Excel -

191288-excel-clean.png

and

191344-excel-dirty.png

For Word -

191309-word-clean.png

and

191227-word-dirty.png

The updated Add-In can be obtained at s!AmnqrCFBv4nDgiLKcnfX4cf0S2a0



excel-clean.png (60.4 KiB)
excel-dirty.png (60.6 KiB)
word-clean.png (45.0 KiB)
word-dirty.png (47.7 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

  1. Your solution is like Manual click in UI -> check Saved property. But i want the reverse one. User need not have to click UI to find it. Add-in itself automatically should check Saved property periodically and if there is unsaved changes, show/update the UI (task pane) to the user.

  2. Regarding the task pane sample you shared earlier, the task pane is displaying only in the first document i open. It doesn't displaying in the subsequent documents. I think may be because we calling task pane from OnConnection(). Also i don't want a ribbon button which will open task pane on clicking, as i don't want user to use it. Automatic solution is better.

Is there any method/something which will run always when add-in is active? If it is available, i can add a something like a loop which will periodically check Saved property and show UI/update (task pane) if needed.

0 Votes 0 ·
RLWA32-6355 avatar image RLWA32-6355 ShabariPragash-5690 ·

The point of updating the sample Add-in was simply to demonstrate retrieving useful information for the Saved property. The sample was never intended to address all possible circumstances, including multiple documents.

Why do you want to continually look for unsaved changes. Don't Office applications have an AutoSave capability? Don't they prompt to save changes when a document or workbook is closed?









0 Votes 0 ·

I want to continually look for changes in order to find if user is editing. I earlier tried key press events detection, but it detects unwanted too many key press events too. So now i am thinking to use Saved property.

Is there any method/event handler/something which will run always when add-in is active?

0 Votes 0 ·
Show more comments