From 0ab8ea0b196821e499359d9c1d20f395540a6bef Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Wed, 2 Sep 2015 06:14:59 +0200 Subject: [PATCH] Do not crash on opening document when embedded. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - VERBESSERT: Kein Absturz mehr, wenn das Demo-Dokument innerhalb der ZAA geöffnet wird (Exception ID 65a5c34e). --- zaaReloaded2/Ribbon.cs | 47 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/zaaReloaded2/Ribbon.cs b/zaaReloaded2/Ribbon.cs index a556054..fb27e25 100755 --- a/zaaReloaded2/Ribbon.cs +++ b/zaaReloaded2/Ribbon.cs @@ -16,12 +16,9 @@ * limitations under the License. */ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; using System.Reflection; using System.Runtime.InteropServices; -using System.Text; using System.Windows; using System.Drawing; using System.Windows.Resources; @@ -33,7 +30,6 @@ using zaaReloaded2.Formatter; using zaaReloaded2.Controller; using Word = Microsoft.Office.Interop.Word; using Bovender.Mvvm.Actions; -using Bovender.Mvvm.Messaging; // TODO: Follow these steps to enable the Ribbon (XML) item: @@ -114,7 +110,7 @@ namespace zaaReloaded2 Globals.ThisAddIn.Application.Selection); break; case "zrlDemo": - Demo.Demo.OpenDemoDocument(); + DoLoadDemo(); break; default: throw new InvalidOperationException("No operation defined for " + control.Id); @@ -230,6 +226,47 @@ namespace zaaReloaded2 vm.InjectInto().ShowDialog(); } + /// + /// Loads the embedded demo document. + /// + /// + /// If Word is running in an embedded environment (e.g. in the ZAA), + /// adding a document causes a COMException. Unfortunately, it is + /// not trivial to test if Word is running embedded, so we use a + /// try...catch structure and catch all COMExceptions. The error + /// message might be not quite right if the exception was caused by + /// a different problem. + /// See http://davecra.com/2013/04/10/how-to-determine-if-an-excel-workbook-is-embedded-and-more + /// + void DoLoadDemo() + { + try + { + Demo.Demo.OpenDemoDocument(); + } + catch (System.Runtime.InteropServices.COMException e) + { + // HRESULT comparison according to http://stackoverflow.com/a/1426198/270712 + // Fix for exception ID 65a5c34e + if (e.ErrorCode == unchecked((int)0x800A11FD)) + { + NotificationAction a = new NotificationAction(); + a.Caption = "Kann Demo-Dokument nicht laden"; + a.Message = "Das Demo-Dokument kann nicht geladen werden, " + + "wenn Word in der Zentralen Arztbriefablage ausgeführt wird.\r" + + "Bitte Word als eigenständige Anwendung starten und dann " + + "noch einmal versuchen."; + a.OkButtonLabel = "Schließen"; + a.Invoke(); + } + else + { + throw; + } + } + + } + public void Application_WindowSelectionChange(Microsoft.Office.Interop.Word.Selection Sel) { _ribbon.Invalidate();