Exception from Word.ApplicationClass.Activedocumen

2020-02-06 09:02发布

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.

4条回答
放荡不羁爱自由
2楼-- · 2020-02-06 09:27

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.

查看更多
狗以群分
3楼-- · 2020-02-06 09:35

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.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-02-06 09:41

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
}
查看更多
我只想做你的唯一
5楼-- · 2020-02-06 09:48

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

查看更多
登录 后发表回答