Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

c# - Exception from Word.ApplicationClass.Activedocument all of a sudden

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...