Do not crash on opening document when embedded.

- VERBESSERT: Kein Absturz mehr, wenn das Demo-Dokument innerhalb der ZAA geöffnet wird (Exception ID 65a5c34e).
This commit is contained in:
Daniel Kraus 2015-09-02 06:14:59 +02:00
parent 607857706b
commit 0ab8ea0b19

View File

@ -16,12 +16,9 @@
* limitations under the License. * limitations under the License.
*/ */
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
using System.Windows; using System.Windows;
using System.Drawing; using System.Drawing;
using System.Windows.Resources; using System.Windows.Resources;
@ -33,7 +30,6 @@ using zaaReloaded2.Formatter;
using zaaReloaded2.Controller; using zaaReloaded2.Controller;
using Word = Microsoft.Office.Interop.Word; using Word = Microsoft.Office.Interop.Word;
using Bovender.Mvvm.Actions; using Bovender.Mvvm.Actions;
using Bovender.Mvvm.Messaging;
// TODO: Follow these steps to enable the Ribbon (XML) item: // TODO: Follow these steps to enable the Ribbon (XML) item:
@ -114,7 +110,7 @@ namespace zaaReloaded2
Globals.ThisAddIn.Application.Selection); Globals.ThisAddIn.Application.Selection);
break; break;
case "zrlDemo": case "zrlDemo":
Demo.Demo.OpenDemoDocument(); DoLoadDemo();
break; break;
default: default:
throw new InvalidOperationException("No operation defined for " + control.Id); throw new InvalidOperationException("No operation defined for " + control.Id);
@ -230,6 +226,47 @@ namespace zaaReloaded2
vm.InjectInto<SettingsRepositoryView>().ShowDialog(); vm.InjectInto<SettingsRepositoryView>().ShowDialog();
} }
/// <summary>
/// Loads the embedded demo document.
/// </summary>
/// <remarks>
/// 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
/// </remarks>
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) public void Application_WindowSelectionChange(Microsoft.Office.Interop.Word.Selection Sel)
{ {
_ribbon.Invalidate(); _ribbon.Invalidate();