Word Add-in load delay for encrypted doc

Add-in Express™ Support Service
That's what is more important than anything else

Word Add-in load delay for encrypted doc
When opening an encrypted doc directly from Explorer, Word add-in does not load until password entered 
Andrew Bruce


Guest


We have a single COM add-in for Excel / PowerPoint / Word.

One of our big features is auto-decrypt password-protected document.

This works fine for all three apps as long as the app is already opened.

However - consider opening a password-protected doc directly from Explorer (although the same problem happens if opened directly from the "Office Start" window):


  • Excel - works fine
  • PowerPoint - works fine
  • Word - auto-decrypt fails because our add-in isn't loaded until *after* the encrypted document password is entered (or ESC is pressed).


I have posted a screen recording of the problem on my personal website: https://www.softwareab.net/wordpress/wp-content/uploads/2022/01/2022-01-28-15-17-36.mkv
Posted 28 Jan, 2022 15:37:12 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello Andrew,

In my tests, the OnHostApplicationInitialized() method is triggered before the "Password" dialog box is shown. The OnRibbonBeforeCreate and AddinInitialize events are triggered after the dialog box is closed.

That is, you can override OnHostApplicationInitialized() and connect to events of the Word.Application object.

Regards from Poland (CET),

Andrei Smolin
Add-in Express Team Leader
Posted 31 Jan, 2022 06:30:20 Top
Andrew Bruce


Guest


Thanks Andrei - worked just fine. There is one downside that invoking SendMessage that early in the process is failing but I work around that. (I use SendMessage to the AddinModule so I can do my work in a background thread but perform UI interaction on the UI thread.)

Net result: All is well and I my logic works just fine for Word by overriding OnHostApplicationInitialized.
Posted 31 Jan, 2022 11:52:35 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Andrew Bruce writes:
invoking SendMessage that early in the process is failing


In what way?

Regards from Poland (CET),

Andrei Smolin
Add-in Express Team Leader
Posted 31 Jan, 2022 12:03:29 Top
Andrew Bruce


Guest


The problem is that AddinModule.Current.SendMessage only appears to work *after* a successful AddinModule_AddinInitialize.

So why does this matter?

For all my add-ins (Excel / PowerPoint / Word) I have a single background thread that tracks and auto-decrypts Office documents as necessary. This background thread works just fine, but I also like to show a status form when the auto-decrypter kicks in.

Unfortunately, Windows does not support simply creating UI objects (e.g. Forms) and showing / hiding those UI objects except on the main thread. It's humorous - try creating a Form on a background thread and calling Form.Show(). You do get *something* to show but it's not what you want or expect ;)

The process of showing a form from a background thread actually requires some initial setup. The process I use is:

a) Create a custom, unique Windows Message that AddinModule will use to dispatch back to a method in the bg thread. Example: "Tc3AutoDecrypterEvents = (int)RegisterWindowMessage(KEYS.TC3_AUTO_DECRYPTER_EVENTS.ToString());"

b) In AddinModule: Store instance variable to the bg thread.

c) In bg thread: use AddinModule.Current.SendMessage to send the custom Windows Message to AddinModule.

c) In AddminModule: within AddinModule_OnSendMessage detect the custom Windows Message and dispatch back to a method in the bg thread. We'll call this method "Notify()".

d) In the bg thread: within "Notify()" act on the passed Windows Message (or wParam or lParam as you prefer) to do your work - which is now on the main UI thread and will work Just Fine.

Sounds like a bunch of work and it is some overhead but it works perfectly.

Except for Word when I open an encrypted document directly from Explorer, that is...

In that case: although OnHostApplicationInitialized has fired, AddinModule_AddinInitialize *not yet fired*. This appears to prevent AddinModule.Current.SendMessage from working. Nothing fails when I invoke AddinModule.Current.SendMessage from the bg thread in this situation and no exceptions thrown - it's just that AddinModule_OnSendMessage is not invoked and thus I can't handle the custom Windows Message. Bummer.

It's not a total blocker for me; I can get the hModule for the encryption provider's window and basically subclass the encryption provider's WNDPROC by using SetWindowsHookEx / UnhookWindowsHookEx and then - instead of using AddinModule.Current.SendMessage - use a raw WIN32 SendMessage to the encryption provider's window. This really is pretty much the same as what I'm doing now with AddinModule.Current.SendMessage / AddinModule_OnSendMessage but it would be more convenient if I could depend on AddinModule_OnSendMessage working even before AddinModule_AddinInitialize fires.
Posted 31 Jan, 2022 14:49:54 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Thank you, Andrew.

We will think about this issue.

Regards from Poland (CET),

Andrei Smolin
Add-in Express Team Leader
Posted 01 Feb, 2022 04:03:17 Top