Exception from Word.ApplicationClass.Activedocument all of a sudden
Asked Answered
P

4

4

I have this single line of code in a Word VSTO add-in project which used to work but suddenly doesn't anymore:

Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;

Before it just returned the Document object as it was supposed to, and I could read from and manipulate it without issue. But now it throws a System.Runtime.InteropServices.COMException exception. "Word has encountered a problem." Very helpful. The exception contains an HRESULT of 0x800A13E9, which hasn't helped me so far either. Out of memory or something? Idk.

The new behavior seems to have started around New Year's Eve (not sure since I wasn't using the add-in for the past couple of weeks), and it started while the add-in was installed, i.e. nothing in the code was changed, recompiled or reinstalled to make it happen. I've since rebuilt the project, thinking maybe a certificate had expired or something, but the error still occurs.

It's maybe worth noting that I can still access other properties of the Application instance. For instance, this line does not throw an exception:

int numDocuments = Globals.ThisAddIn.Application.Documents.Count;

But then when I iterate over the Documents collection with foreach, it just skips over the loop as if the count was zero.

The only thing I can think of is that an update to Office (365) has broken something related to VSTO. But where do I even begin debugging this?


Okay, so I tried creating a new Word VSTO add-in, changing none of the default project properties, but adding a single ribbon with a single button and the following method:

private void button1_Click(object sender, RibbonControlEventArgs e)
{
    try
    {
        var x = Globals.ThisAddIn.Application.ActiveDocument;
    }
    catch  (Exception ex)
    {
        ;
    }
}

It throws exactly the same exception on getting the ActiveDocument property. I did however notice that there are two more exceptions thrown when loading the add-in, and they're also thrown when loading the original add-in (that used to work fine):

Exception thrown: 'System.Deployment.Application.DeploymentException' in System.Deployment.dll
Exception thrown: 'System.Security.Cryptography.CryptographicException' in Microsoft.VisualStudio.Tools.Applications.Hosting.dll

They don't prevent the add-in from loading and running, but maybe they're related to the problem anyway? I don't know if they were also thrown last year before the problem appeared.

I do however have a couple of Excel add-ins that still run fine with the same version of VSTO, Visual Studio and Office, and they don't throw the above two exceptions when loading. So the issue seems to be specific to Word.


And now I tried rolling back to Office 365 version 1810, the October release, which definitely worked before, so I think it's probably not a problem introduced by an Office update. It's something else. Probably.

Not that it really helps me much, but at least I've ruled that out. Probably.

Prud answered 7/1, 2019 at 11:43 Comment(6)
So, Office 365... And version of Windows, just for the sake of thoroughness? Recent Windows 10 updates (last few months = recent) have had some negative effects on the Interop, although I, personally, haven't experienced them...Rust
Windows 10 Pro version 1809, which I believe is the most recent. I tried the six most recent versions of Office 365 already, via the cumbersome rollback method, and also completely removing it and all settings and reinstalling, but the error remains. So maybe it is related to a Windows update. I guess I'll try rolling that back next, once I have Office back up and running with all the settings. Grr.Prud
Tip: If you suspect Office try a REPAIR, not remove and re-install. REPAIR updates/corrects Registry settings, re-installing does not.Rust
The other thing to check is whether the document(s) open in the Word application are "sand-boxed" - in a protected window. This isn't something that should just be happening now as it was introduced more than five years ago, but I thought I'd better mention it...Rust
Thanks for the suggestions, but I tried repairing already. And although I think the sandboxing may have something to do with it, there is definitely a document present in the Documents collection, and it's definitely not protected view or read-only mode or some such. Also I believe the exception thrown would be different if that were the cause.Prud
Thanks for letting us know. FWIW I've voted to close as "no longer reproducible"...Rust
P
1

Well, rolling back to the previous version of Windows 10 (1803 in my case, apparently) fixed the issue, and I can't reproduce it at the moment, so I'll mark this as solved for now.

While I still have no clue what exactly the deal was, I did at least learn in my research that clicking the "Check for updates" button in Windows 10 instantly makes you a beta tester for Microsoft. As in, they deliberately put you on the unstable update train, without any sort of notice, let alone a warning that you're about to install updates that they don't consider ready for release yet.

Anyway, I hope they got some good telemetry from my many, many hours of trying everything I could think of, so that maybe the issue is fixed by the time the update is forced on me. If not, I guess I'll be back to ask the same question again. At least I'll know where to start looking for the cause.

Prud answered 8/1, 2019 at 14:59 Comment(0)
T
5

The problem seems to be triggered by the Windows 10 October 2018 Update (or November). The affected customers had special Windows regional settings: Example: "English (Switzerland)".

Solution:
Change the Windows regional settings format to "English (UK)" or "English (US)"

The VBA Editor also no longer worked correctly for the affected customers (without installed add-ins). The error "Word has encountered a problem" (Visual Basic Error 5097) occurred directly during opening. The same error that occurs in C# for Application.ActiveDocument

Toleration answered 11/1, 2019 at 12:2 Comment(0)
P
1

Well, rolling back to the previous version of Windows 10 (1803 in my case, apparently) fixed the issue, and I can't reproduce it at the moment, so I'll mark this as solved for now.

While I still have no clue what exactly the deal was, I did at least learn in my research that clicking the "Check for updates" button in Windows 10 instantly makes you a beta tester for Microsoft. As in, they deliberately put you on the unstable update train, without any sort of notice, let alone a warning that you're about to install updates that they don't consider ready for release yet.

Anyway, I hope they got some good telemetry from my many, many hours of trying everything I could think of, so that maybe the issue is fixed by the time the update is forced on me. If not, I guess I'll be back to ask the same question again. At least I'll know where to start looking for the cause.

Prud answered 8/1, 2019 at 14:59 Comment(0)
C
0

From my experience : when i was closing the Active Document at that time i was refreshing Ribbon Menu according to the active document. If the closing document was the Last document i wasn't able to read any property of

Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;

and i was facing the same issue like you.

whenever i close any document i check

if(Globals.ThisAddIn.Application.Documents.Count = 1){ 
LastActiveDocument = true;
}

And when i refresh the Ribbion Menu i check

if(!LastActiveDocument){
// then only read any property of Active document
}
Consummation answered 7/1, 2019 at 12:42 Comment(1)
I get this, and the add-in is careful to check that there is at least one document present, especially because of protected view and such. But if I add int y = Globals.ThisAddIn.Application.Documents.Count; to the minimal example above, there's clearly a document there (y=1 when running the add-in with a new, empty document, for example)Prud
O
0

Microsoft has fixed the issue
Microsoft has released an Office update to fix this issue.
With Office Version 1902 (Build 11328.20158) the issue is fixed on my side.

Orellana answered 26/3, 2019 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.